aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2011-03-22 19:34:40 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-03-22 20:44:15 -0400
commite359dc24d32e58c795fc339cb3e89ea6330fceae (patch)
tree824561f59ab4d217bf09462021095235df825f0e
parent33ee3b2e2eb9b4b6c64dcf9ed66e2ac3124e748c (diff)
sigma-firmware: loader for Analog Devices' SigmaStudio
Analog Devices' SigmaStudio can produce firmware blobs for devices with these DSPs embedded (like some audio codecs). Allow these device drivers to easily parse and load them. Signed-off-by: Mike Frysinger <vapier@gentoo.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--drivers/firmware/Kconfig12
-rw-r--r--drivers/firmware/Makefile1
-rw-r--r--drivers/firmware/sigma.c115
-rw-r--r--include/linux/sigma.h60
4 files changed, 188 insertions, 0 deletions
diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig
index 3c56afc5eb1b..b3a25a55ba23 100644
--- a/drivers/firmware/Kconfig
+++ b/drivers/firmware/Kconfig
@@ -145,4 +145,16 @@ config ISCSI_IBFT
145 detect iSCSI boot parameters dynamically during system boot, say Y. 145 detect iSCSI boot parameters dynamically during system boot, say Y.
146 Otherwise, say N. 146 Otherwise, say N.
147 147
148config SIGMA
149 tristate "SigmaStudio firmware loader"
150 depends on I2C
151 select CRC32
152 default n
153 help
154 Enable helper functions for working with Analog Devices SigmaDSP
155 parts and binary firmwares produced by Analog Devices SigmaStudio.
156
157 If unsure, say N here. Drivers that need these helpers will select
158 this option automatically.
159
148endmenu 160endmenu
diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile
index 20c17fca1232..00bb0b80a79f 100644
--- a/drivers/firmware/Makefile
+++ b/drivers/firmware/Makefile
@@ -12,3 +12,4 @@ obj-$(CONFIG_DMIID) += dmi-id.o
12obj-$(CONFIG_ISCSI_IBFT_FIND) += iscsi_ibft_find.o 12obj-$(CONFIG_ISCSI_IBFT_FIND) += iscsi_ibft_find.o
13obj-$(CONFIG_ISCSI_IBFT) += iscsi_ibft.o 13obj-$(CONFIG_ISCSI_IBFT) += iscsi_ibft.o
14obj-$(CONFIG_FIRMWARE_MEMMAP) += memmap.o 14obj-$(CONFIG_FIRMWARE_MEMMAP) += memmap.o
15obj-$(CONFIG_SIGMA) += sigma.o
diff --git a/drivers/firmware/sigma.c b/drivers/firmware/sigma.c
new file mode 100644
index 000000000000..c19cd2c39fa6
--- /dev/null
+++ b/drivers/firmware/sigma.c
@@ -0,0 +1,115 @@
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>
14#include <linux/sigma.h>
15
16/* Return: 0==OK, <0==error, =1 ==no more actions */
17static int
18process_sigma_action(struct i2c_client *client, struct sigma_firmware *ssfw)
19{
20 struct sigma_action *sa = (void *)(ssfw->fw->data + ssfw->pos);
21 size_t len = sigma_action_len(sa);
22 int ret = 0;
23
24 pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
25 sa->instr, sa->addr, len);
26
27 switch (sa->instr) {
28 case SIGMA_ACTION_WRITEXBYTES:
29 case SIGMA_ACTION_WRITESINGLE:
30 case SIGMA_ACTION_WRITESAFELOAD:
31 if (ssfw->fw->size < ssfw->pos + len)
32 return -EINVAL;
33 ret = i2c_master_send(client, (void *)&sa->addr, len);
34 if (ret < 0)
35 return -EINVAL;
36 break;
37
38 case SIGMA_ACTION_DELAY:
39 ret = 0;
40 udelay(len);
41 len = 0;
42 break;
43
44 case SIGMA_ACTION_END:
45 return 1;
46
47 default:
48 return -EINVAL;
49 }
50
51 /* when arrive here ret=0 or sent data */
52 ssfw->pos += sigma_action_size(sa, len);
53 return ssfw->pos == ssfw->fw->size;
54}
55
56static int
57process_sigma_actions(struct i2c_client *client, struct sigma_firmware *ssfw)
58{
59 pr_debug("%s: processing %p\n", __func__, ssfw);
60
61 while (1) {
62 int ret = process_sigma_action(client, ssfw);
63 pr_debug("%s: action returned %i\n", __func__, ret);
64 if (ret == 1)
65 return 0;
66 else if (ret)
67 return ret;
68 }
69}
70
71int process_sigma_firmware(struct i2c_client *client, const char *name)
72{
73 int ret;
74 struct sigma_firmware_header *ssfw_head;
75 struct sigma_firmware ssfw;
76 const struct firmware *fw;
77 u32 crc;
78
79 pr_debug("%s: loading firmware %s\n", __func__, name);
80
81 /* first load the blob */
82 ret = request_firmware(&fw, name, &client->dev);
83 if (ret) {
84 pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
85 return ret;
86 }
87 ssfw.fw = fw;
88
89 /* then verify the header */
90 ret = -EINVAL;
91 if (fw->size < sizeof(*ssfw_head))
92 goto done;
93
94 ssfw_head = (void *)fw->data;
95 if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic)))
96 goto done;
97
98 crc = crc32(0, fw->data, fw->size);
99 pr_debug("%s: crc=%x\n", __func__, crc);
100 if (crc != ssfw_head->crc)
101 goto done;
102
103 ssfw.pos = sizeof(*ssfw_head);
104
105 /* finally process all of the actions */
106 ret = process_sigma_actions(client, &ssfw);
107
108 done:
109 release_firmware(fw);
110
111 pr_debug("%s: loaded %s\n", __func__, name);
112
113 return ret;
114}
115EXPORT_SYMBOL(process_sigma_firmware);
diff --git a/include/linux/sigma.h b/include/linux/sigma.h
new file mode 100644
index 000000000000..e2accb3164d8
--- /dev/null
+++ b/include/linux/sigma.h
@@ -0,0 +1,60 @@
1/*
2 * Load firmware files from Analog Devices SigmaStudio
3 *
4 * Copyright 2009-2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#ifndef __SIGMA_FIRMWARE_H__
10#define __SIGMA_FIRMWARE_H__
11
12#include <linux/firmware.h>
13#include <linux/types.h>
14
15struct i2c_client;
16
17#define SIGMA_MAGIC "ADISIGM"
18
19struct sigma_firmware {
20 const struct firmware *fw;
21 size_t pos;
22};
23
24struct sigma_firmware_header {
25 unsigned char magic[7];
26 u8 version;
27 u32 crc;
28};
29
30enum {
31 SIGMA_ACTION_WRITEXBYTES = 0,
32 SIGMA_ACTION_WRITESINGLE,
33 SIGMA_ACTION_WRITESAFELOAD,
34 SIGMA_ACTION_DELAY,
35 SIGMA_ACTION_PLLWAIT,
36 SIGMA_ACTION_NOOP,
37 SIGMA_ACTION_END,
38};
39
40struct sigma_action {
41 u8 instr;
42 u8 len_hi;
43 u16 len;
44 u16 addr;
45 unsigned char payload[];
46};
47
48static inline u32 sigma_action_len(struct sigma_action *sa)
49{
50 return (sa->len_hi << 16) | sa->len;
51}
52
53static inline size_t sigma_action_size(struct sigma_action *sa, u32 payload_len)
54{
55 return sizeof(*sa) + payload_len + (payload_len % 2);
56}
57
58extern int process_sigma_firmware(struct i2c_client *client, const char *name);
59
60#endif