2011-03-22 17:34:40 -06:00
|
|
|
/*
|
|
|
|
* Load Analog Devices SigmaStudio firmware files
|
|
|
|
*
|
|
|
|
* Copyright 2009-2011 Analog Devices Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the GPL-2 or later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/crc32.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/firmware.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/i2c.h>
|
2011-11-28 01:44:20 -07:00
|
|
|
#include <linux/regmap.h>
|
2011-07-25 18:13:21 -06:00
|
|
|
#include <linux/module.h>
|
2011-11-28 01:44:17 -07:00
|
|
|
|
|
|
|
#include "sigmadsp.h"
|
2011-03-22 17:34:40 -06:00
|
|
|
|
2011-11-28 01:44:19 -07:00
|
|
|
#define SIGMA_MAGIC "ADISIGM"
|
|
|
|
|
|
|
|
struct sigma_firmware_header {
|
|
|
|
unsigned char magic[7];
|
|
|
|
u8 version;
|
|
|
|
__le32 crc;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SIGMA_ACTION_WRITEXBYTES = 0,
|
|
|
|
SIGMA_ACTION_WRITESINGLE,
|
|
|
|
SIGMA_ACTION_WRITESAFELOAD,
|
|
|
|
SIGMA_ACTION_DELAY,
|
|
|
|
SIGMA_ACTION_PLLWAIT,
|
|
|
|
SIGMA_ACTION_NOOP,
|
|
|
|
SIGMA_ACTION_END,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sigma_action {
|
|
|
|
u8 instr;
|
|
|
|
u8 len_hi;
|
|
|
|
__le16 len;
|
|
|
|
__be16 addr;
|
|
|
|
unsigned char payload[];
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct sigma_firmware {
|
|
|
|
const struct firmware *fw;
|
|
|
|
size_t pos;
|
2011-11-28 01:44:20 -07:00
|
|
|
|
|
|
|
void *control_data;
|
|
|
|
int (*write)(void *control_data, const struct sigma_action *sa,
|
|
|
|
size_t len);
|
2011-11-28 01:44:19 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline u32 sigma_action_len(struct sigma_action *sa)
|
|
|
|
{
|
|
|
|
return (sa->len_hi << 16) | le16_to_cpu(sa->len);
|
|
|
|
}
|
|
|
|
|
2011-11-28 01:44:14 -07:00
|
|
|
static size_t sigma_action_size(struct sigma_action *sa)
|
|
|
|
{
|
|
|
|
size_t payload = 0;
|
|
|
|
|
|
|
|
switch (sa->instr) {
|
|
|
|
case SIGMA_ACTION_WRITEXBYTES:
|
|
|
|
case SIGMA_ACTION_WRITESINGLE:
|
|
|
|
case SIGMA_ACTION_WRITESAFELOAD:
|
|
|
|
payload = sigma_action_len(sa);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
payload = ALIGN(payload, 2);
|
|
|
|
|
|
|
|
return payload + sizeof(struct sigma_action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a negative error value in case of an error, 0 if processing of
|
|
|
|
* the firmware should be stopped after this action, 1 otherwise.
|
|
|
|
*/
|
2011-03-22 17:34:40 -06:00
|
|
|
static int
|
2011-11-28 01:44:20 -07:00
|
|
|
process_sigma_action(struct sigma_firmware *ssfw, struct sigma_action *sa)
|
2011-03-22 17:34:40 -06:00
|
|
|
{
|
|
|
|
size_t len = sigma_action_len(sa);
|
2011-11-28 01:44:14 -07:00
|
|
|
int ret;
|
2011-03-22 17:34:40 -06:00
|
|
|
|
|
|
|
pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
|
|
|
|
sa->instr, sa->addr, len);
|
|
|
|
|
|
|
|
switch (sa->instr) {
|
|
|
|
case SIGMA_ACTION_WRITEXBYTES:
|
|
|
|
case SIGMA_ACTION_WRITESINGLE:
|
|
|
|
case SIGMA_ACTION_WRITESAFELOAD:
|
2011-11-28 01:44:20 -07:00
|
|
|
ret = ssfw->write(ssfw->control_data, sa, len);
|
2011-03-22 17:34:40 -06:00
|
|
|
if (ret < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
break;
|
|
|
|
case SIGMA_ACTION_DELAY:
|
|
|
|
udelay(len);
|
|
|
|
len = 0;
|
|
|
|
break;
|
|
|
|
case SIGMA_ACTION_END:
|
2011-11-28 01:44:14 -07:00
|
|
|
return 0;
|
2011-03-22 17:34:40 -06:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2011-11-28 01:44:14 -07:00
|
|
|
return 1;
|
2011-03-22 17:34:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-11-28 01:44:20 -07:00
|
|
|
process_sigma_actions(struct sigma_firmware *ssfw)
|
2011-03-22 17:34:40 -06:00
|
|
|
{
|
2011-11-28 01:44:14 -07:00
|
|
|
struct sigma_action *sa;
|
|
|
|
size_t size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (ssfw->pos + sizeof(*sa) <= ssfw->fw->size) {
|
|
|
|
sa = (struct sigma_action *)(ssfw->fw->data + ssfw->pos);
|
|
|
|
|
|
|
|
size = sigma_action_size(sa);
|
|
|
|
ssfw->pos += size;
|
|
|
|
if (ssfw->pos > ssfw->fw->size || size == 0)
|
|
|
|
break;
|
|
|
|
|
2011-11-28 01:44:20 -07:00
|
|
|
ret = process_sigma_action(ssfw, sa);
|
2011-03-22 17:34:40 -06:00
|
|
|
|
|
|
|
pr_debug("%s: action returned %i\n", __func__, ret);
|
2011-11-28 01:44:14 -07:00
|
|
|
|
|
|
|
if (ret <= 0)
|
2011-03-22 17:34:40 -06:00
|
|
|
return ret;
|
|
|
|
}
|
2011-11-28 01:44:14 -07:00
|
|
|
|
|
|
|
if (ssfw->pos != ssfw->fw->size)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
2011-03-22 17:34:40 -06:00
|
|
|
}
|
|
|
|
|
2011-11-28 01:44:20 -07:00
|
|
|
static int _process_sigma_firmware(struct device *dev,
|
|
|
|
struct sigma_firmware *ssfw, const char *name)
|
2011-03-22 17:34:40 -06:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sigma_firmware_header *ssfw_head;
|
|
|
|
const struct firmware *fw;
|
|
|
|
u32 crc;
|
|
|
|
|
|
|
|
pr_debug("%s: loading firmware %s\n", __func__, name);
|
|
|
|
|
|
|
|
/* first load the blob */
|
2011-11-28 01:44:20 -07:00
|
|
|
ret = request_firmware(&fw, name, dev);
|
2011-03-22 17:34:40 -06:00
|
|
|
if (ret) {
|
|
|
|
pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-11-28 01:44:20 -07:00
|
|
|
ssfw->fw = fw;
|
2011-03-22 17:34:40 -06:00
|
|
|
|
|
|
|
/* then verify the header */
|
|
|
|
ret = -EINVAL;
|
2011-11-28 01:44:14 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reject too small or unreasonable large files. The upper limit has been
|
|
|
|
* chosen a bit arbitrarily, but it should be enough for all practical
|
|
|
|
* purposes and having the limit makes it easier to avoid integer
|
|
|
|
* overflows later in the loading process.
|
|
|
|
*/
|
2011-11-28 01:44:18 -07:00
|
|
|
if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000) {
|
2011-11-28 01:44:20 -07:00
|
|
|
dev_err(dev, "Failed to load firmware: Invalid size\n");
|
2011-03-22 17:34:40 -06:00
|
|
|
goto done;
|
2011-11-28 01:44:18 -07:00
|
|
|
}
|
2011-03-22 17:34:40 -06:00
|
|
|
|
|
|
|
ssfw_head = (void *)fw->data;
|
2011-11-28 01:44:18 -07:00
|
|
|
if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic))) {
|
2011-11-28 01:44:20 -07:00
|
|
|
dev_err(dev, "Failed to load firmware: Invalid magic\n");
|
2011-03-22 17:34:40 -06:00
|
|
|
goto done;
|
2011-11-28 01:44:18 -07:00
|
|
|
}
|
2011-03-22 17:34:40 -06:00
|
|
|
|
2011-11-28 01:44:15 -07:00
|
|
|
crc = crc32(0, fw->data + sizeof(*ssfw_head),
|
|
|
|
fw->size - sizeof(*ssfw_head));
|
2011-03-22 17:34:40 -06:00
|
|
|
pr_debug("%s: crc=%x\n", __func__, crc);
|
2011-11-28 01:44:18 -07:00
|
|
|
if (crc != le32_to_cpu(ssfw_head->crc)) {
|
2011-11-28 01:44:20 -07:00
|
|
|
dev_err(dev, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
|
2011-11-28 01:44:18 -07:00
|
|
|
le32_to_cpu(ssfw_head->crc), crc);
|
2011-03-22 17:34:40 -06:00
|
|
|
goto done;
|
2011-11-28 01:44:18 -07:00
|
|
|
}
|
2011-03-22 17:34:40 -06:00
|
|
|
|
2011-11-28 01:44:20 -07:00
|
|
|
ssfw->pos = sizeof(*ssfw_head);
|
2011-03-22 17:34:40 -06:00
|
|
|
|
|
|
|
/* finally process all of the actions */
|
2011-11-28 01:44:20 -07:00
|
|
|
ret = process_sigma_actions(ssfw);
|
2011-03-22 17:34:40 -06:00
|
|
|
|
|
|
|
done:
|
|
|
|
release_firmware(fw);
|
|
|
|
|
|
|
|
pr_debug("%s: loaded %s\n", __func__, name);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2011-11-28 01:44:20 -07:00
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_I2C)
|
|
|
|
|
|
|
|
static int sigma_action_write_i2c(void *control_data,
|
|
|
|
const struct sigma_action *sa, size_t len)
|
|
|
|
{
|
|
|
|
return i2c_master_send(control_data, (const unsigned char *)&sa->addr,
|
|
|
|
len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int process_sigma_firmware(struct i2c_client *client, const char *name)
|
|
|
|
{
|
|
|
|
struct sigma_firmware ssfw;
|
|
|
|
|
|
|
|
ssfw.control_data = client;
|
|
|
|
ssfw.write = sigma_action_write_i2c;
|
|
|
|
|
|
|
|
return _process_sigma_firmware(&client->dev, &ssfw, name);
|
|
|
|
}
|
2011-03-22 17:34:40 -06:00
|
|
|
EXPORT_SYMBOL(process_sigma_firmware);
|
2011-07-25 18:13:21 -06:00
|
|
|
|
2011-11-28 01:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_REGMAP)
|
|
|
|
|
|
|
|
static int sigma_action_write_regmap(void *control_data,
|
|
|
|
const struct sigma_action *sa, size_t len)
|
|
|
|
{
|
|
|
|
return regmap_raw_write(control_data, le16_to_cpu(sa->addr),
|
|
|
|
sa->payload, len - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int process_sigma_firmware_regmap(struct device *dev, struct regmap *regmap,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
struct sigma_firmware ssfw;
|
|
|
|
|
|
|
|
ssfw.control_data = regmap;
|
|
|
|
ssfw.write = sigma_action_write_regmap;
|
|
|
|
|
|
|
|
return _process_sigma_firmware(dev, &ssfw, name);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(process_sigma_firmware_regmap);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-07-25 18:13:21 -06:00
|
|
|
MODULE_LICENSE("GPL");
|