sigmadsp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Load Analog Devices SigmaStudio firmware files
  3. *
  4. * Copyright 2009-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/crc32.h>
  9. #include <linux/delay.h>
  10. #include <linux/firmware.h>
  11. #include <linux/kernel.h>
  12. #include <linux/i2c.h>
  13. #include <linux/regmap.h>
  14. #include <linux/module.h>
  15. #include "sigmadsp.h"
  16. #define SIGMA_MAGIC "ADISIGM"
  17. struct sigma_firmware_header {
  18. unsigned char magic[7];
  19. u8 version;
  20. __le32 crc;
  21. } __packed;
  22. enum {
  23. SIGMA_ACTION_WRITEXBYTES = 0,
  24. SIGMA_ACTION_WRITESINGLE,
  25. SIGMA_ACTION_WRITESAFELOAD,
  26. SIGMA_ACTION_DELAY,
  27. SIGMA_ACTION_PLLWAIT,
  28. SIGMA_ACTION_NOOP,
  29. SIGMA_ACTION_END,
  30. };
  31. struct sigma_action {
  32. u8 instr;
  33. u8 len_hi;
  34. __le16 len;
  35. __be16 addr;
  36. unsigned char payload[];
  37. } __packed;
  38. struct sigma_firmware {
  39. const struct firmware *fw;
  40. size_t pos;
  41. void *control_data;
  42. int (*write)(void *control_data, const struct sigma_action *sa,
  43. size_t len);
  44. };
  45. static inline u32 sigma_action_len(struct sigma_action *sa)
  46. {
  47. return (sa->len_hi << 16) | le16_to_cpu(sa->len);
  48. }
  49. static size_t sigma_action_size(struct sigma_action *sa)
  50. {
  51. size_t payload = 0;
  52. switch (sa->instr) {
  53. case SIGMA_ACTION_WRITEXBYTES:
  54. case SIGMA_ACTION_WRITESINGLE:
  55. case SIGMA_ACTION_WRITESAFELOAD:
  56. payload = sigma_action_len(sa);
  57. break;
  58. default:
  59. break;
  60. }
  61. payload = ALIGN(payload, 2);
  62. return payload + sizeof(struct sigma_action);
  63. }
  64. /*
  65. * Returns a negative error value in case of an error, 0 if processing of
  66. * the firmware should be stopped after this action, 1 otherwise.
  67. */
  68. static int
  69. process_sigma_action(struct sigma_firmware *ssfw, struct sigma_action *sa)
  70. {
  71. size_t len = sigma_action_len(sa);
  72. int ret;
  73. pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
  74. sa->instr, sa->addr, len);
  75. switch (sa->instr) {
  76. case SIGMA_ACTION_WRITEXBYTES:
  77. case SIGMA_ACTION_WRITESINGLE:
  78. case SIGMA_ACTION_WRITESAFELOAD:
  79. ret = ssfw->write(ssfw->control_data, sa, len);
  80. if (ret < 0)
  81. return -EINVAL;
  82. break;
  83. case SIGMA_ACTION_DELAY:
  84. udelay(len);
  85. len = 0;
  86. break;
  87. case SIGMA_ACTION_END:
  88. return 0;
  89. default:
  90. return -EINVAL;
  91. }
  92. return 1;
  93. }
  94. static int
  95. process_sigma_actions(struct sigma_firmware *ssfw)
  96. {
  97. struct sigma_action *sa;
  98. size_t size;
  99. int ret;
  100. while (ssfw->pos + sizeof(*sa) <= ssfw->fw->size) {
  101. sa = (struct sigma_action *)(ssfw->fw->data + ssfw->pos);
  102. size = sigma_action_size(sa);
  103. ssfw->pos += size;
  104. if (ssfw->pos > ssfw->fw->size || size == 0)
  105. break;
  106. ret = process_sigma_action(ssfw, sa);
  107. pr_debug("%s: action returned %i\n", __func__, ret);
  108. if (ret <= 0)
  109. return ret;
  110. }
  111. if (ssfw->pos != ssfw->fw->size)
  112. return -EINVAL;
  113. return 0;
  114. }
  115. static int _process_sigma_firmware(struct device *dev,
  116. struct sigma_firmware *ssfw, const char *name)
  117. {
  118. int ret;
  119. struct sigma_firmware_header *ssfw_head;
  120. const struct firmware *fw;
  121. u32 crc;
  122. pr_debug("%s: loading firmware %s\n", __func__, name);
  123. /* first load the blob */
  124. ret = request_firmware(&fw, name, dev);
  125. if (ret) {
  126. pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
  127. return ret;
  128. }
  129. ssfw->fw = fw;
  130. /* then verify the header */
  131. ret = -EINVAL;
  132. /*
  133. * Reject too small or unreasonable large files. The upper limit has been
  134. * chosen a bit arbitrarily, but it should be enough for all practical
  135. * purposes and having the limit makes it easier to avoid integer
  136. * overflows later in the loading process.
  137. */
  138. if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000) {
  139. dev_err(dev, "Failed to load firmware: Invalid size\n");
  140. goto done;
  141. }
  142. ssfw_head = (void *)fw->data;
  143. if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic))) {
  144. dev_err(dev, "Failed to load firmware: Invalid magic\n");
  145. goto done;
  146. }
  147. if (ssfw_head->version != 1) {
  148. dev_err(dev,
  149. "Failed to load firmware: Invalid version %d. Supported firmware versions: 1\n",
  150. ssfw_head->version);
  151. goto done;
  152. }
  153. crc = crc32(0, fw->data + sizeof(*ssfw_head),
  154. fw->size - sizeof(*ssfw_head));
  155. pr_debug("%s: crc=%x\n", __func__, crc);
  156. if (crc != le32_to_cpu(ssfw_head->crc)) {
  157. dev_err(dev, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
  158. le32_to_cpu(ssfw_head->crc), crc);
  159. goto done;
  160. }
  161. ssfw->pos = sizeof(*ssfw_head);
  162. /* finally process all of the actions */
  163. ret = process_sigma_actions(ssfw);
  164. done:
  165. release_firmware(fw);
  166. pr_debug("%s: loaded %s\n", __func__, name);
  167. return ret;
  168. }
  169. #if IS_ENABLED(CONFIG_I2C)
  170. static int sigma_action_write_i2c(void *control_data,
  171. const struct sigma_action *sa, size_t len)
  172. {
  173. return i2c_master_send(control_data, (const unsigned char *)&sa->addr,
  174. len);
  175. }
  176. int process_sigma_firmware(struct i2c_client *client, const char *name)
  177. {
  178. struct sigma_firmware ssfw;
  179. ssfw.control_data = client;
  180. ssfw.write = sigma_action_write_i2c;
  181. return _process_sigma_firmware(&client->dev, &ssfw, name);
  182. }
  183. EXPORT_SYMBOL(process_sigma_firmware);
  184. #endif
  185. #if IS_ENABLED(CONFIG_REGMAP)
  186. static int sigma_action_write_regmap(void *control_data,
  187. const struct sigma_action *sa, size_t len)
  188. {
  189. return regmap_raw_write(control_data, be16_to_cpu(sa->addr),
  190. sa->payload, len - 2);
  191. }
  192. int process_sigma_firmware_regmap(struct device *dev, struct regmap *regmap,
  193. const char *name)
  194. {
  195. struct sigma_firmware ssfw;
  196. ssfw.control_data = regmap;
  197. ssfw.write = sigma_action_write_regmap;
  198. return _process_sigma_firmware(dev, &ssfw, name);
  199. }
  200. EXPORT_SYMBOL(process_sigma_firmware_regmap);
  201. #endif
  202. MODULE_LICENSE("GPL");