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