2 * Load Analog Devices SigmaStudio firmware files
4 * Copyright 2009-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
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>
14 #include <linux/regmap.h>
15 #include <linux/module.h>
19 #define SIGMA_MAGIC "ADISIGM"
21 struct sigma_firmware_header
{
22 unsigned char magic
[7];
28 SIGMA_ACTION_WRITEXBYTES
= 0,
29 SIGMA_ACTION_WRITESINGLE
,
30 SIGMA_ACTION_WRITESAFELOAD
,
37 static inline u32
sigma_action_len(struct sigma_action
*sa
)
39 return (sa
->len_hi
<< 16) | le16_to_cpu(sa
->len
);
42 static size_t sigma_action_size(struct sigma_action
*sa
)
47 case SIGMA_ACTION_WRITEXBYTES
:
48 case SIGMA_ACTION_WRITESINGLE
:
49 case SIGMA_ACTION_WRITESAFELOAD
:
50 payload
= sigma_action_len(sa
);
56 payload
= ALIGN(payload
, 2);
58 return payload
+ sizeof(struct sigma_action
);
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.
66 process_sigma_action(struct sigma_firmware
*ssfw
, struct sigma_action
*sa
)
68 size_t len
= sigma_action_len(sa
);
71 pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__
,
72 sa
->instr
, sa
->addr
, len
);
75 case SIGMA_ACTION_WRITEXBYTES
:
76 case SIGMA_ACTION_WRITESINGLE
:
77 case SIGMA_ACTION_WRITESAFELOAD
:
78 ret
= ssfw
->write(ssfw
->control_data
, sa
, len
);
82 case SIGMA_ACTION_DELAY
:
86 case SIGMA_ACTION_END
:
96 process_sigma_actions(struct sigma_firmware
*ssfw
)
98 struct sigma_action
*sa
;
102 while (ssfw
->pos
+ sizeof(*sa
) <= ssfw
->fw
->size
) {
103 sa
= (struct sigma_action
*)(ssfw
->fw
->data
+ ssfw
->pos
);
105 size
= sigma_action_size(sa
);
107 if (ssfw
->pos
> ssfw
->fw
->size
|| size
== 0)
110 ret
= process_sigma_action(ssfw
, sa
);
112 pr_debug("%s: action returned %i\n", __func__
, ret
);
118 if (ssfw
->pos
!= ssfw
->fw
->size
)
124 int _process_sigma_firmware(struct device
*dev
,
125 struct sigma_firmware
*ssfw
, const char *name
)
128 struct sigma_firmware_header
*ssfw_head
;
129 const struct firmware
*fw
;
132 pr_debug("%s: loading firmware %s\n", __func__
, name
);
134 /* first load the blob */
135 ret
= request_firmware(&fw
, name
, dev
);
137 pr_debug("%s: request_firmware() failed with %i\n", __func__
, ret
);
142 /* then verify the header */
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.
151 if (fw
->size
< sizeof(*ssfw_head
) || fw
->size
>= 0x4000000) {
152 dev_err(dev
, "Failed to load firmware: Invalid size\n");
156 ssfw_head
= (void *)fw
->data
;
157 if (memcmp(ssfw_head
->magic
, SIGMA_MAGIC
, ARRAY_SIZE(ssfw_head
->magic
))) {
158 dev_err(dev
, "Failed to load firmware: Invalid magic\n");
162 if (ssfw_head
->version
!= 1) {
164 "Failed to load firmware: Invalid version %d. Supported firmware versions: 1\n",
169 crc
= crc32(0, fw
->data
+ sizeof(*ssfw_head
),
170 fw
->size
- sizeof(*ssfw_head
));
171 pr_debug("%s: crc=%x\n", __func__
, crc
);
172 if (crc
!= le32_to_cpu(ssfw_head
->crc
)) {
173 dev_err(dev
, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
174 le32_to_cpu(ssfw_head
->crc
), crc
);
178 ssfw
->pos
= sizeof(*ssfw_head
);
180 /* finally process all of the actions */
181 ret
= process_sigma_actions(ssfw
);
184 release_firmware(fw
);
186 pr_debug("%s: loaded %s\n", __func__
, name
);
190 EXPORT_SYMBOL_GPL(_process_sigma_firmware
);
192 MODULE_LICENSE("GPL");