1 // SPDX-License-Identifier: GPL-2.0-only
3 * cs_dsp.c -- Cirrus Logic DSP firmware support
5 * Based on sound/soc/codecs/wm_adsp.c
7 * Copyright 2012 Wolfson Microelectronics plc
8 * Copyright (C) 2015-2021 Cirrus Logic, Inc. and
9 * Cirrus Logic International Semiconductor Ltd.
12 #include <linux/ctype.h>
13 #include <linux/debugfs.h>
14 #include <linux/delay.h>
15 #include <linux/minmax.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/seq_file.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
22 #include <linux/firmware/cirrus/cs_dsp.h>
23 #include <linux/firmware/cirrus/wmfw.h>
25 #define cs_dsp_err(_dsp, fmt, ...) \
26 dev_err(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
27 #define cs_dsp_warn(_dsp, fmt, ...) \
28 dev_warn(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
29 #define cs_dsp_info(_dsp, fmt, ...) \
30 dev_info(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
31 #define cs_dsp_dbg(_dsp, fmt, ...) \
32 dev_dbg(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
34 #define ADSP1_CONTROL_1 0x00
35 #define ADSP1_CONTROL_2 0x02
36 #define ADSP1_CONTROL_3 0x03
37 #define ADSP1_CONTROL_4 0x04
38 #define ADSP1_CONTROL_5 0x06
39 #define ADSP1_CONTROL_6 0x07
40 #define ADSP1_CONTROL_7 0x08
41 #define ADSP1_CONTROL_8 0x09
42 #define ADSP1_CONTROL_9 0x0A
43 #define ADSP1_CONTROL_10 0x0B
44 #define ADSP1_CONTROL_11 0x0C
45 #define ADSP1_CONTROL_12 0x0D
46 #define ADSP1_CONTROL_13 0x0F
47 #define ADSP1_CONTROL_14 0x10
48 #define ADSP1_CONTROL_15 0x11
49 #define ADSP1_CONTROL_16 0x12
50 #define ADSP1_CONTROL_17 0x13
51 #define ADSP1_CONTROL_18 0x14
52 #define ADSP1_CONTROL_19 0x16
53 #define ADSP1_CONTROL_20 0x17
54 #define ADSP1_CONTROL_21 0x18
55 #define ADSP1_CONTROL_22 0x1A
56 #define ADSP1_CONTROL_23 0x1B
57 #define ADSP1_CONTROL_24 0x1C
58 #define ADSP1_CONTROL_25 0x1E
59 #define ADSP1_CONTROL_26 0x20
60 #define ADSP1_CONTROL_27 0x21
61 #define ADSP1_CONTROL_28 0x22
62 #define ADSP1_CONTROL_29 0x23
63 #define ADSP1_CONTROL_30 0x24
64 #define ADSP1_CONTROL_31 0x26
69 #define ADSP1_WDMA_BUFFER_LENGTH_MASK 0x00FF /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
70 #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT 0 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
71 #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH 8 /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
76 #define ADSP1_DBG_CLK_ENA 0x0008 /* DSP1_DBG_CLK_ENA */
77 #define ADSP1_DBG_CLK_ENA_MASK 0x0008 /* DSP1_DBG_CLK_ENA */
78 #define ADSP1_DBG_CLK_ENA_SHIFT 3 /* DSP1_DBG_CLK_ENA */
79 #define ADSP1_DBG_CLK_ENA_WIDTH 1 /* DSP1_DBG_CLK_ENA */
80 #define ADSP1_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
81 #define ADSP1_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
82 #define ADSP1_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
83 #define ADSP1_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
84 #define ADSP1_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
85 #define ADSP1_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
86 #define ADSP1_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
87 #define ADSP1_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
88 #define ADSP1_START 0x0001 /* DSP1_START */
89 #define ADSP1_START_MASK 0x0001 /* DSP1_START */
90 #define ADSP1_START_SHIFT 0 /* DSP1_START */
91 #define ADSP1_START_WIDTH 1 /* DSP1_START */
96 #define ADSP1_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
97 #define ADSP1_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
98 #define ADSP1_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
100 #define ADSP2_CONTROL 0x0
101 #define ADSP2_CLOCKING 0x1
102 #define ADSP2V2_CLOCKING 0x2
103 #define ADSP2_STATUS1 0x4
104 #define ADSP2_WDMA_CONFIG_1 0x30
105 #define ADSP2_WDMA_CONFIG_2 0x31
106 #define ADSP2V2_WDMA_CONFIG_2 0x32
107 #define ADSP2_RDMA_CONFIG_1 0x34
109 #define ADSP2_SCRATCH0 0x40
110 #define ADSP2_SCRATCH1 0x41
111 #define ADSP2_SCRATCH2 0x42
112 #define ADSP2_SCRATCH3 0x43
114 #define ADSP2V2_SCRATCH0_1 0x40
115 #define ADSP2V2_SCRATCH2_3 0x42
120 #define ADSP2_MEM_ENA 0x0010 /* DSP1_MEM_ENA */
121 #define ADSP2_MEM_ENA_MASK 0x0010 /* DSP1_MEM_ENA */
122 #define ADSP2_MEM_ENA_SHIFT 4 /* DSP1_MEM_ENA */
123 #define ADSP2_MEM_ENA_WIDTH 1 /* DSP1_MEM_ENA */
124 #define ADSP2_SYS_ENA 0x0004 /* DSP1_SYS_ENA */
125 #define ADSP2_SYS_ENA_MASK 0x0004 /* DSP1_SYS_ENA */
126 #define ADSP2_SYS_ENA_SHIFT 2 /* DSP1_SYS_ENA */
127 #define ADSP2_SYS_ENA_WIDTH 1 /* DSP1_SYS_ENA */
128 #define ADSP2_CORE_ENA 0x0002 /* DSP1_CORE_ENA */
129 #define ADSP2_CORE_ENA_MASK 0x0002 /* DSP1_CORE_ENA */
130 #define ADSP2_CORE_ENA_SHIFT 1 /* DSP1_CORE_ENA */
131 #define ADSP2_CORE_ENA_WIDTH 1 /* DSP1_CORE_ENA */
132 #define ADSP2_START 0x0001 /* DSP1_START */
133 #define ADSP2_START_MASK 0x0001 /* DSP1_START */
134 #define ADSP2_START_SHIFT 0 /* DSP1_START */
135 #define ADSP2_START_WIDTH 1 /* DSP1_START */
140 #define ADSP2_CLK_SEL_MASK 0x0007 /* CLK_SEL_ENA */
141 #define ADSP2_CLK_SEL_SHIFT 0 /* CLK_SEL_ENA */
142 #define ADSP2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
147 #define ADSP2V2_CLK_SEL_MASK 0x70000 /* CLK_SEL_ENA */
148 #define ADSP2V2_CLK_SEL_SHIFT 16 /* CLK_SEL_ENA */
149 #define ADSP2V2_CLK_SEL_WIDTH 3 /* CLK_SEL_ENA */
151 #define ADSP2V2_RATE_MASK 0x7800 /* DSP_RATE */
152 #define ADSP2V2_RATE_SHIFT 11 /* DSP_RATE */
153 #define ADSP2V2_RATE_WIDTH 4 /* DSP_RATE */
158 #define ADSP2_RAM_RDY 0x0001
159 #define ADSP2_RAM_RDY_MASK 0x0001
160 #define ADSP2_RAM_RDY_SHIFT 0
161 #define ADSP2_RAM_RDY_WIDTH 1
166 #define ADSP2_LOCK_CODE_0 0x5555
167 #define ADSP2_LOCK_CODE_1 0xAAAA
169 #define ADSP2_WATCHDOG 0x0A
170 #define ADSP2_BUS_ERR_ADDR 0x52
171 #define ADSP2_REGION_LOCK_STATUS 0x64
172 #define ADSP2_LOCK_REGION_1_LOCK_REGION_0 0x66
173 #define ADSP2_LOCK_REGION_3_LOCK_REGION_2 0x68
174 #define ADSP2_LOCK_REGION_5_LOCK_REGION_4 0x6A
175 #define ADSP2_LOCK_REGION_7_LOCK_REGION_6 0x6C
176 #define ADSP2_LOCK_REGION_9_LOCK_REGION_8 0x6E
177 #define ADSP2_LOCK_REGION_CTRL 0x7A
178 #define ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR 0x7C
180 #define ADSP2_REGION_LOCK_ERR_MASK 0x8000
181 #define ADSP2_ADDR_ERR_MASK 0x4000
182 #define ADSP2_WDT_TIMEOUT_STS_MASK 0x2000
183 #define ADSP2_CTRL_ERR_PAUSE_ENA 0x0002
184 #define ADSP2_CTRL_ERR_EINT 0x0001
186 #define ADSP2_BUS_ERR_ADDR_MASK 0x00FFFFFF
187 #define ADSP2_XMEM_ERR_ADDR_MASK 0x0000FFFF
188 #define ADSP2_PMEM_ERR_ADDR_MASK 0x7FFF0000
189 #define ADSP2_PMEM_ERR_ADDR_SHIFT 16
190 #define ADSP2_WDT_ENA_MASK 0xFFFFFFFD
192 #define ADSP2_LOCK_REGION_SHIFT 16
195 * Event control messages
197 #define CS_DSP_FW_EVENT_SHUTDOWN 0x000001
202 #define HALO_AHBM_WINDOW_DEBUG_0 0x02040
203 #define HALO_AHBM_WINDOW_DEBUG_1 0x02044
208 #define HALO_SCRATCH1 0x005c0
209 #define HALO_SCRATCH2 0x005c8
210 #define HALO_SCRATCH3 0x005d0
211 #define HALO_SCRATCH4 0x005d8
212 #define HALO_CCM_CORE_CONTROL 0x41000
213 #define HALO_CORE_SOFT_RESET 0x00010
214 #define HALO_WDT_CONTROL 0x47000
219 #define HALO_MPU_XMEM_ACCESS_0 0x43000
220 #define HALO_MPU_YMEM_ACCESS_0 0x43004
221 #define HALO_MPU_WINDOW_ACCESS_0 0x43008
222 #define HALO_MPU_XREG_ACCESS_0 0x4300C
223 #define HALO_MPU_YREG_ACCESS_0 0x43014
224 #define HALO_MPU_XMEM_ACCESS_1 0x43018
225 #define HALO_MPU_YMEM_ACCESS_1 0x4301C
226 #define HALO_MPU_WINDOW_ACCESS_1 0x43020
227 #define HALO_MPU_XREG_ACCESS_1 0x43024
228 #define HALO_MPU_YREG_ACCESS_1 0x4302C
229 #define HALO_MPU_XMEM_ACCESS_2 0x43030
230 #define HALO_MPU_YMEM_ACCESS_2 0x43034
231 #define HALO_MPU_WINDOW_ACCESS_2 0x43038
232 #define HALO_MPU_XREG_ACCESS_2 0x4303C
233 #define HALO_MPU_YREG_ACCESS_2 0x43044
234 #define HALO_MPU_XMEM_ACCESS_3 0x43048
235 #define HALO_MPU_YMEM_ACCESS_3 0x4304C
236 #define HALO_MPU_WINDOW_ACCESS_3 0x43050
237 #define HALO_MPU_XREG_ACCESS_3 0x43054
238 #define HALO_MPU_YREG_ACCESS_3 0x4305C
239 #define HALO_MPU_XM_VIO_ADDR 0x43100
240 #define HALO_MPU_XM_VIO_STATUS 0x43104
241 #define HALO_MPU_YM_VIO_ADDR 0x43108
242 #define HALO_MPU_YM_VIO_STATUS 0x4310C
243 #define HALO_MPU_PM_VIO_ADDR 0x43110
244 #define HALO_MPU_PM_VIO_STATUS 0x43114
245 #define HALO_MPU_LOCK_CONFIG 0x43140
248 * HALO_AHBM_WINDOW_DEBUG_1
250 #define HALO_AHBM_CORE_ERR_ADDR_MASK 0x0fffff00
251 #define HALO_AHBM_CORE_ERR_ADDR_SHIFT 8
252 #define HALO_AHBM_FLAGS_ERR_MASK 0x000000ff
255 * HALO_CCM_CORE_CONTROL
257 #define HALO_CORE_RESET 0x00000200
258 #define HALO_CORE_EN 0x00000001
261 * HALO_CORE_SOFT_RESET
263 #define HALO_CORE_SOFT_RESET_MASK 0x00000001
268 #define HALO_WDT_EN_MASK 0x00000001
271 * HALO_MPU_?M_VIO_STATUS
273 #define HALO_MPU_VIO_STS_MASK 0x007e0000
274 #define HALO_MPU_VIO_STS_SHIFT 17
275 #define HALO_MPU_VIO_ERR_WR_MASK 0x00008000
276 #define HALO_MPU_VIO_ERR_SRC_MASK 0x00007fff
277 #define HALO_MPU_VIO_ERR_SRC_SHIFT 0
282 #define WSEQ_OP_MAX_WORDS 3
283 #define WSEQ_END_OF_SCRIPT 0xFFFFFF
286 bool (*validate_version
)(struct cs_dsp
*dsp
, unsigned int version
);
287 unsigned int (*parse_sizes
)(struct cs_dsp
*dsp
,
288 const char * const file
,
290 const struct firmware
*firmware
);
291 int (*setup_algs
)(struct cs_dsp
*dsp
);
292 unsigned int (*region_to_reg
)(struct cs_dsp_region
const *mem
,
293 unsigned int offset
);
295 void (*show_fw_status
)(struct cs_dsp
*dsp
);
296 void (*stop_watchdog
)(struct cs_dsp
*dsp
);
298 int (*enable_memory
)(struct cs_dsp
*dsp
);
299 void (*disable_memory
)(struct cs_dsp
*dsp
);
300 int (*lock_memory
)(struct cs_dsp
*dsp
, unsigned int lock_regions
);
302 int (*enable_core
)(struct cs_dsp
*dsp
);
303 void (*disable_core
)(struct cs_dsp
*dsp
);
305 int (*start_core
)(struct cs_dsp
*dsp
);
306 void (*stop_core
)(struct cs_dsp
*dsp
);
309 static const struct cs_dsp_ops cs_dsp_adsp1_ops
;
310 static const struct cs_dsp_ops cs_dsp_adsp2_ops
[];
311 static const struct cs_dsp_ops cs_dsp_halo_ops
;
312 static const struct cs_dsp_ops cs_dsp_halo_ao_ops
;
315 struct list_head list
;
319 static struct cs_dsp_buf
*cs_dsp_buf_alloc(const void *src
, size_t len
,
320 struct list_head
*list
)
322 struct cs_dsp_buf
*buf
= kzalloc(sizeof(*buf
), GFP_KERNEL
);
327 buf
->buf
= vmalloc(len
);
332 memcpy(buf
->buf
, src
, len
);
335 list_add_tail(&buf
->list
, list
);
340 static void cs_dsp_buf_free(struct list_head
*list
)
342 while (!list_empty(list
)) {
343 struct cs_dsp_buf
*buf
= list_first_entry(list
,
346 list_del(&buf
->list
);
353 * cs_dsp_mem_region_name() - Return a name string for a memory type
354 * @type: the memory type to match
356 * Return: A const string identifying the memory region.
358 const char *cs_dsp_mem_region_name(unsigned int type
)
363 case WMFW_HALO_PM_PACKED
:
369 case WMFW_HALO_XM_PACKED
:
373 case WMFW_HALO_YM_PACKED
:
381 EXPORT_SYMBOL_NS_GPL(cs_dsp_mem_region_name
, FW_CS_DSP
);
383 #ifdef CONFIG_DEBUG_FS
384 static void cs_dsp_debugfs_save_wmfwname(struct cs_dsp
*dsp
, const char *s
)
386 char *tmp
= kasprintf(GFP_KERNEL
, "%s\n", s
);
388 kfree(dsp
->wmfw_file_name
);
389 dsp
->wmfw_file_name
= tmp
;
392 static void cs_dsp_debugfs_save_binname(struct cs_dsp
*dsp
, const char *s
)
394 char *tmp
= kasprintf(GFP_KERNEL
, "%s\n", s
);
396 kfree(dsp
->bin_file_name
);
397 dsp
->bin_file_name
= tmp
;
400 static void cs_dsp_debugfs_clear(struct cs_dsp
*dsp
)
402 kfree(dsp
->wmfw_file_name
);
403 kfree(dsp
->bin_file_name
);
404 dsp
->wmfw_file_name
= NULL
;
405 dsp
->bin_file_name
= NULL
;
408 static ssize_t
cs_dsp_debugfs_wmfw_read(struct file
*file
,
409 char __user
*user_buf
,
410 size_t count
, loff_t
*ppos
)
412 struct cs_dsp
*dsp
= file
->private_data
;
415 mutex_lock(&dsp
->pwr_lock
);
417 if (!dsp
->wmfw_file_name
|| !dsp
->booted
)
420 ret
= simple_read_from_buffer(user_buf
, count
, ppos
,
422 strlen(dsp
->wmfw_file_name
));
424 mutex_unlock(&dsp
->pwr_lock
);
428 static ssize_t
cs_dsp_debugfs_bin_read(struct file
*file
,
429 char __user
*user_buf
,
430 size_t count
, loff_t
*ppos
)
432 struct cs_dsp
*dsp
= file
->private_data
;
435 mutex_lock(&dsp
->pwr_lock
);
437 if (!dsp
->bin_file_name
|| !dsp
->booted
)
440 ret
= simple_read_from_buffer(user_buf
, count
, ppos
,
442 strlen(dsp
->bin_file_name
));
444 mutex_unlock(&dsp
->pwr_lock
);
448 static const struct {
450 const struct file_operations fops
;
451 } cs_dsp_debugfs_fops
[] = {
453 .name
= "wmfw_file_name",
456 .read
= cs_dsp_debugfs_wmfw_read
,
460 .name
= "bin_file_name",
463 .read
= cs_dsp_debugfs_bin_read
,
468 static int cs_dsp_coeff_base_reg(struct cs_dsp_coeff_ctl
*ctl
, unsigned int *reg
,
471 static int cs_dsp_debugfs_read_controls_show(struct seq_file
*s
, void *ignored
)
473 struct cs_dsp
*dsp
= s
->private;
474 struct cs_dsp_coeff_ctl
*ctl
;
477 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
478 cs_dsp_coeff_base_reg(ctl
, ®
, 0);
479 seq_printf(s
, "%22.*s: %#8zx %s:%08x %#8x %s %#8x %#4x %c%c%c%c %s %s\n",
480 ctl
->subname_len
, ctl
->subname
, ctl
->len
,
481 cs_dsp_mem_region_name(ctl
->alg_region
.type
),
482 ctl
->offset
, reg
, ctl
->fw_name
, ctl
->alg_region
.alg
, ctl
->type
,
483 ctl
->flags
& WMFW_CTL_FLAG_VOLATILE
? 'V' : '-',
484 ctl
->flags
& WMFW_CTL_FLAG_SYS
? 'S' : '-',
485 ctl
->flags
& WMFW_CTL_FLAG_READABLE
? 'R' : '-',
486 ctl
->flags
& WMFW_CTL_FLAG_WRITEABLE
? 'W' : '-',
487 ctl
->enabled
? "enabled" : "disabled",
488 ctl
->set
? "dirty" : "clean");
493 DEFINE_SHOW_ATTRIBUTE(cs_dsp_debugfs_read_controls
);
496 * cs_dsp_init_debugfs() - Create and populate DSP representation in debugfs
497 * @dsp: pointer to DSP structure
498 * @debugfs_root: pointer to debugfs directory in which to create this DSP
501 void cs_dsp_init_debugfs(struct cs_dsp
*dsp
, struct dentry
*debugfs_root
)
503 struct dentry
*root
= NULL
;
506 root
= debugfs_create_dir(dsp
->name
, debugfs_root
);
508 debugfs_create_bool("booted", 0444, root
, &dsp
->booted
);
509 debugfs_create_bool("running", 0444, root
, &dsp
->running
);
510 debugfs_create_x32("fw_id", 0444, root
, &dsp
->fw_id
);
511 debugfs_create_x32("fw_version", 0444, root
, &dsp
->fw_id_version
);
513 for (i
= 0; i
< ARRAY_SIZE(cs_dsp_debugfs_fops
); ++i
)
514 debugfs_create_file(cs_dsp_debugfs_fops
[i
].name
, 0444, root
,
515 dsp
, &cs_dsp_debugfs_fops
[i
].fops
);
517 debugfs_create_file("controls", 0444, root
, dsp
,
518 &cs_dsp_debugfs_read_controls_fops
);
520 dsp
->debugfs_root
= root
;
522 EXPORT_SYMBOL_NS_GPL(cs_dsp_init_debugfs
, FW_CS_DSP
);
525 * cs_dsp_cleanup_debugfs() - Removes DSP representation from debugfs
526 * @dsp: pointer to DSP structure
528 void cs_dsp_cleanup_debugfs(struct cs_dsp
*dsp
)
530 cs_dsp_debugfs_clear(dsp
);
531 debugfs_remove_recursive(dsp
->debugfs_root
);
532 dsp
->debugfs_root
= ERR_PTR(-ENODEV
);
534 EXPORT_SYMBOL_NS_GPL(cs_dsp_cleanup_debugfs
, FW_CS_DSP
);
536 void cs_dsp_init_debugfs(struct cs_dsp
*dsp
, struct dentry
*debugfs_root
)
539 EXPORT_SYMBOL_NS_GPL(cs_dsp_init_debugfs
, FW_CS_DSP
);
541 void cs_dsp_cleanup_debugfs(struct cs_dsp
*dsp
)
544 EXPORT_SYMBOL_NS_GPL(cs_dsp_cleanup_debugfs
, FW_CS_DSP
);
546 static inline void cs_dsp_debugfs_save_wmfwname(struct cs_dsp
*dsp
,
551 static inline void cs_dsp_debugfs_save_binname(struct cs_dsp
*dsp
,
556 static inline void cs_dsp_debugfs_clear(struct cs_dsp
*dsp
)
561 static const struct cs_dsp_region
*cs_dsp_find_region(struct cs_dsp
*dsp
,
566 for (i
= 0; i
< dsp
->num_mems
; i
++)
567 if (dsp
->mem
[i
].type
== type
)
573 static unsigned int cs_dsp_region_to_reg(struct cs_dsp_region
const *mem
,
578 return mem
->base
+ (offset
* 3);
583 return mem
->base
+ (offset
* 2);
585 WARN(1, "Unknown memory region type");
590 static unsigned int cs_dsp_halo_region_to_reg(struct cs_dsp_region
const *mem
,
596 return mem
->base
+ (offset
* 4);
597 case WMFW_HALO_XM_PACKED
:
598 case WMFW_HALO_YM_PACKED
:
599 return (mem
->base
+ (offset
* 3)) & ~0x3;
600 case WMFW_HALO_PM_PACKED
:
601 return mem
->base
+ (offset
* 5);
603 WARN(1, "Unknown memory region type");
608 static void cs_dsp_read_fw_status(struct cs_dsp
*dsp
,
609 int noffs
, unsigned int *offs
)
614 for (i
= 0; i
< noffs
; ++i
) {
615 ret
= regmap_read(dsp
->regmap
, dsp
->base
+ offs
[i
], &offs
[i
]);
617 cs_dsp_err(dsp
, "Failed to read SCRATCH%u: %d\n", i
, ret
);
623 static void cs_dsp_adsp2_show_fw_status(struct cs_dsp
*dsp
)
625 unsigned int offs
[] = {
626 ADSP2_SCRATCH0
, ADSP2_SCRATCH1
, ADSP2_SCRATCH2
, ADSP2_SCRATCH3
,
629 cs_dsp_read_fw_status(dsp
, ARRAY_SIZE(offs
), offs
);
631 cs_dsp_dbg(dsp
, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
632 offs
[0], offs
[1], offs
[2], offs
[3]);
635 static void cs_dsp_adsp2v2_show_fw_status(struct cs_dsp
*dsp
)
637 unsigned int offs
[] = { ADSP2V2_SCRATCH0_1
, ADSP2V2_SCRATCH2_3
};
639 cs_dsp_read_fw_status(dsp
, ARRAY_SIZE(offs
), offs
);
641 cs_dsp_dbg(dsp
, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
642 offs
[0] & 0xFFFF, offs
[0] >> 16,
643 offs
[1] & 0xFFFF, offs
[1] >> 16);
646 static void cs_dsp_halo_show_fw_status(struct cs_dsp
*dsp
)
648 unsigned int offs
[] = {
649 HALO_SCRATCH1
, HALO_SCRATCH2
, HALO_SCRATCH3
, HALO_SCRATCH4
,
652 cs_dsp_read_fw_status(dsp
, ARRAY_SIZE(offs
), offs
);
654 cs_dsp_dbg(dsp
, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
655 offs
[0], offs
[1], offs
[2], offs
[3]);
658 static int cs_dsp_coeff_base_reg(struct cs_dsp_coeff_ctl
*ctl
, unsigned int *reg
,
661 const struct cs_dsp_alg_region
*alg_region
= &ctl
->alg_region
;
662 struct cs_dsp
*dsp
= ctl
->dsp
;
663 const struct cs_dsp_region
*mem
;
665 mem
= cs_dsp_find_region(dsp
, alg_region
->type
);
667 cs_dsp_err(dsp
, "No base for region %x\n",
672 *reg
= dsp
->ops
->region_to_reg(mem
, ctl
->alg_region
.base
+ ctl
->offset
+ off
);
678 * cs_dsp_coeff_write_acked_control() - Sends event_id to the acked control
679 * @ctl: pointer to acked coefficient control
680 * @event_id: the value to write to the given acked control
682 * Once the value has been written to the control the function shall block
683 * until the running firmware acknowledges the write or timeout is exceeded.
685 * Must be called with pwr_lock held.
687 * Return: Zero for success, a negative number on error.
689 int cs_dsp_coeff_write_acked_control(struct cs_dsp_coeff_ctl
*ctl
, unsigned int event_id
)
691 struct cs_dsp
*dsp
= ctl
->dsp
;
692 __be32 val
= cpu_to_be32(event_id
);
696 lockdep_assert_held(&dsp
->pwr_lock
);
701 ret
= cs_dsp_coeff_base_reg(ctl
, ®
, 0);
705 cs_dsp_dbg(dsp
, "Sending 0x%x to acked control alg 0x%x %s:0x%x\n",
706 event_id
, ctl
->alg_region
.alg
,
707 cs_dsp_mem_region_name(ctl
->alg_region
.type
), ctl
->offset
);
709 ret
= regmap_raw_write(dsp
->regmap
, reg
, &val
, sizeof(val
));
711 cs_dsp_err(dsp
, "Failed to write %x: %d\n", reg
, ret
);
716 * Poll for ack, we initially poll at ~1ms intervals for firmwares
717 * that respond quickly, then go to ~10ms polls. A firmware is unlikely
718 * to ack instantly so we do the first 1ms delay before reading the
719 * control to avoid a pointless bus transaction
721 for (i
= 0; i
< CS_DSP_ACKED_CTL_TIMEOUT_MS
;) {
723 case 0 ... CS_DSP_ACKED_CTL_N_QUICKPOLLS
- 1:
724 usleep_range(1000, 2000);
728 usleep_range(10000, 20000);
733 ret
= regmap_raw_read(dsp
->regmap
, reg
, &val
, sizeof(val
));
735 cs_dsp_err(dsp
, "Failed to read %x: %d\n", reg
, ret
);
740 cs_dsp_dbg(dsp
, "Acked control ACKED at poll %u\n", i
);
745 cs_dsp_warn(dsp
, "Acked control @0x%x alg:0x%x %s:0x%x timed out\n",
746 reg
, ctl
->alg_region
.alg
,
747 cs_dsp_mem_region_name(ctl
->alg_region
.type
),
752 EXPORT_SYMBOL_NS_GPL(cs_dsp_coeff_write_acked_control
, FW_CS_DSP
);
754 static int cs_dsp_coeff_write_ctrl_raw(struct cs_dsp_coeff_ctl
*ctl
,
755 unsigned int off
, const void *buf
, size_t len
)
757 struct cs_dsp
*dsp
= ctl
->dsp
;
762 ret
= cs_dsp_coeff_base_reg(ctl
, ®
, off
);
766 scratch
= kmemdup(buf
, len
, GFP_KERNEL
| GFP_DMA
);
770 ret
= regmap_raw_write(dsp
->regmap
, reg
, scratch
,
773 cs_dsp_err(dsp
, "Failed to write %zu bytes to %x: %d\n",
778 cs_dsp_dbg(dsp
, "Wrote %zu bytes to %x\n", len
, reg
);
786 * cs_dsp_coeff_write_ctrl() - Writes the given buffer to the given coefficient control
787 * @ctl: pointer to coefficient control
788 * @off: word offset at which data should be written
789 * @buf: the buffer to write to the given control
790 * @len: the length of the buffer in bytes
792 * Must be called with pwr_lock held.
794 * Return: < 0 on error, 1 when the control value changed and 0 when it has not.
796 int cs_dsp_coeff_write_ctrl(struct cs_dsp_coeff_ctl
*ctl
,
797 unsigned int off
, const void *buf
, size_t len
)
804 lockdep_assert_held(&ctl
->dsp
->pwr_lock
);
806 if (ctl
->flags
&& !(ctl
->flags
& WMFW_CTL_FLAG_WRITEABLE
))
809 if (len
+ off
* sizeof(u32
) > ctl
->len
)
812 if (ctl
->flags
& WMFW_CTL_FLAG_VOLATILE
) {
814 } else if (buf
!= ctl
->cache
) {
815 if (memcmp(ctl
->cache
+ off
* sizeof(u32
), buf
, len
))
816 memcpy(ctl
->cache
+ off
* sizeof(u32
), buf
, len
);
822 if (ctl
->enabled
&& ctl
->dsp
->running
)
823 ret
= cs_dsp_coeff_write_ctrl_raw(ctl
, off
, buf
, len
);
830 EXPORT_SYMBOL_NS_GPL(cs_dsp_coeff_write_ctrl
, FW_CS_DSP
);
833 * cs_dsp_coeff_lock_and_write_ctrl() - Writes the given buffer to the given coefficient control
834 * @ctl: pointer to coefficient control
835 * @off: word offset at which data should be written
836 * @buf: the buffer to write to the given control
837 * @len: the length of the buffer in bytes
839 * Same as cs_dsp_coeff_write_ctrl() but takes pwr_lock.
841 * Return: A negative number on error, 1 when the control value changed and 0 when it has not.
843 int cs_dsp_coeff_lock_and_write_ctrl(struct cs_dsp_coeff_ctl
*ctl
,
844 unsigned int off
, const void *buf
, size_t len
)
846 struct cs_dsp
*dsp
= ctl
->dsp
;
849 lockdep_assert_not_held(&dsp
->pwr_lock
);
851 mutex_lock(&dsp
->pwr_lock
);
852 ret
= cs_dsp_coeff_write_ctrl(ctl
, off
, buf
, len
);
853 mutex_unlock(&dsp
->pwr_lock
);
857 EXPORT_SYMBOL_GPL(cs_dsp_coeff_lock_and_write_ctrl
);
859 static int cs_dsp_coeff_read_ctrl_raw(struct cs_dsp_coeff_ctl
*ctl
,
860 unsigned int off
, void *buf
, size_t len
)
862 struct cs_dsp
*dsp
= ctl
->dsp
;
867 ret
= cs_dsp_coeff_base_reg(ctl
, ®
, off
);
871 scratch
= kmalloc(len
, GFP_KERNEL
| GFP_DMA
);
875 ret
= regmap_raw_read(dsp
->regmap
, reg
, scratch
, len
);
877 cs_dsp_err(dsp
, "Failed to read %zu bytes from %x: %d\n",
882 cs_dsp_dbg(dsp
, "Read %zu bytes from %x\n", len
, reg
);
884 memcpy(buf
, scratch
, len
);
891 * cs_dsp_coeff_read_ctrl() - Reads the given coefficient control into the given buffer
892 * @ctl: pointer to coefficient control
893 * @off: word offset at which data should be read
894 * @buf: the buffer to store to the given control
895 * @len: the length of the buffer in bytes
897 * Must be called with pwr_lock held.
899 * Return: Zero for success, a negative number on error.
901 int cs_dsp_coeff_read_ctrl(struct cs_dsp_coeff_ctl
*ctl
,
902 unsigned int off
, void *buf
, size_t len
)
909 lockdep_assert_held(&ctl
->dsp
->pwr_lock
);
911 if (len
+ off
* sizeof(u32
) > ctl
->len
)
914 if (ctl
->flags
& WMFW_CTL_FLAG_VOLATILE
) {
915 if (ctl
->enabled
&& ctl
->dsp
->running
)
916 return cs_dsp_coeff_read_ctrl_raw(ctl
, off
, buf
, len
);
920 if (!ctl
->flags
&& ctl
->enabled
&& ctl
->dsp
->running
)
921 ret
= cs_dsp_coeff_read_ctrl_raw(ctl
, 0, ctl
->cache
, ctl
->len
);
923 if (buf
!= ctl
->cache
)
924 memcpy(buf
, ctl
->cache
+ off
* sizeof(u32
), len
);
929 EXPORT_SYMBOL_NS_GPL(cs_dsp_coeff_read_ctrl
, FW_CS_DSP
);
932 * cs_dsp_coeff_lock_and_read_ctrl() - Reads the given coefficient control into the given buffer
933 * @ctl: pointer to coefficient control
934 * @off: word offset at which data should be read
935 * @buf: the buffer to store to the given control
936 * @len: the length of the buffer in bytes
938 * Same as cs_dsp_coeff_read_ctrl() but takes pwr_lock.
940 * Return: Zero for success, a negative number on error.
942 int cs_dsp_coeff_lock_and_read_ctrl(struct cs_dsp_coeff_ctl
*ctl
,
943 unsigned int off
, void *buf
, size_t len
)
945 struct cs_dsp
*dsp
= ctl
->dsp
;
948 lockdep_assert_not_held(&dsp
->pwr_lock
);
950 mutex_lock(&dsp
->pwr_lock
);
951 ret
= cs_dsp_coeff_read_ctrl(ctl
, off
, buf
, len
);
952 mutex_unlock(&dsp
->pwr_lock
);
956 EXPORT_SYMBOL_GPL(cs_dsp_coeff_lock_and_read_ctrl
);
958 static int cs_dsp_coeff_init_control_caches(struct cs_dsp
*dsp
)
960 struct cs_dsp_coeff_ctl
*ctl
;
963 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
964 if (!ctl
->enabled
|| ctl
->set
)
966 if (ctl
->flags
& WMFW_CTL_FLAG_VOLATILE
)
970 * For readable controls populate the cache from the DSP memory.
971 * For non-readable controls the cache was zero-filled when
972 * created so we don't need to do anything.
974 if (!ctl
->flags
|| (ctl
->flags
& WMFW_CTL_FLAG_READABLE
)) {
975 ret
= cs_dsp_coeff_read_ctrl_raw(ctl
, 0, ctl
->cache
, ctl
->len
);
984 static int cs_dsp_coeff_sync_controls(struct cs_dsp
*dsp
)
986 struct cs_dsp_coeff_ctl
*ctl
;
989 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
992 if (ctl
->set
&& !(ctl
->flags
& WMFW_CTL_FLAG_VOLATILE
)) {
993 ret
= cs_dsp_coeff_write_ctrl_raw(ctl
, 0, ctl
->cache
,
1003 static void cs_dsp_signal_event_controls(struct cs_dsp
*dsp
,
1006 struct cs_dsp_coeff_ctl
*ctl
;
1009 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
1010 if (ctl
->type
!= WMFW_CTL_TYPE_HOSTEVENT
)
1016 ret
= cs_dsp_coeff_write_acked_control(ctl
, event
);
1019 "Failed to send 0x%x event to alg 0x%x (%d)\n",
1020 event
, ctl
->alg_region
.alg
, ret
);
1024 static void cs_dsp_free_ctl_blk(struct cs_dsp_coeff_ctl
*ctl
)
1027 kfree(ctl
->subname
);
1031 static int cs_dsp_create_control(struct cs_dsp
*dsp
,
1032 const struct cs_dsp_alg_region
*alg_region
,
1033 unsigned int offset
, unsigned int len
,
1034 const char *subname
, unsigned int subname_len
,
1035 unsigned int flags
, unsigned int type
)
1037 struct cs_dsp_coeff_ctl
*ctl
;
1040 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
1041 if (ctl
->fw_name
== dsp
->fw_name
&&
1042 ctl
->alg_region
.alg
== alg_region
->alg
&&
1043 ctl
->alg_region
.type
== alg_region
->type
) {
1044 if ((!subname
&& !ctl
->subname
) ||
1045 (subname
&& (ctl
->subname_len
== subname_len
) &&
1046 !strncmp(ctl
->subname
, subname
, ctl
->subname_len
))) {
1054 ctl
= kzalloc(sizeof(*ctl
), GFP_KERNEL
);
1058 ctl
->fw_name
= dsp
->fw_name
;
1059 ctl
->alg_region
= *alg_region
;
1060 if (subname
&& dsp
->wmfw_ver
>= 2) {
1061 ctl
->subname_len
= subname_len
;
1062 ctl
->subname
= kasprintf(GFP_KERNEL
, "%.*s", subname_len
, subname
);
1063 if (!ctl
->subname
) {
1074 ctl
->offset
= offset
;
1076 ctl
->cache
= kzalloc(ctl
->len
, GFP_KERNEL
);
1079 goto err_ctl_subname
;
1082 list_add(&ctl
->list
, &dsp
->ctl_list
);
1084 if (dsp
->client_ops
->control_add
) {
1085 ret
= dsp
->client_ops
->control_add(ctl
);
1093 list_del(&ctl
->list
);
1096 kfree(ctl
->subname
);
1103 struct cs_dsp_coeff_parsed_alg
{
1110 struct cs_dsp_coeff_parsed_coeff
{
1115 unsigned int ctl_type
;
1120 static int cs_dsp_coeff_parse_string(int bytes
, const u8
**pos
, unsigned int avail
,
1123 int length
, total_field_len
;
1125 /* String fields are at least one __le32 */
1126 if (sizeof(__le32
) > avail
) {
1136 length
= le16_to_cpu(*((__le16
*)*pos
));
1142 total_field_len
= ((length
+ bytes
) + 3) & ~0x03;
1143 if ((unsigned int)total_field_len
> avail
) {
1149 *str
= *pos
+ bytes
;
1151 *pos
+= total_field_len
;
1156 static int cs_dsp_coeff_parse_int(int bytes
, const u8
**pos
)
1162 val
= le16_to_cpu(*((__le16
*)*pos
));
1165 val
= le32_to_cpu(*((__le32
*)*pos
));
1176 static int cs_dsp_coeff_parse_alg(struct cs_dsp
*dsp
,
1177 const struct wmfw_region
*region
,
1178 struct cs_dsp_coeff_parsed_alg
*blk
)
1180 const struct wmfw_adsp_alg_data
*raw
;
1181 unsigned int data_len
= le32_to_cpu(region
->len
);
1185 raw
= (const struct wmfw_adsp_alg_data
*)region
->data
;
1187 switch (dsp
->wmfw_ver
) {
1190 if (sizeof(*raw
) > data_len
)
1193 blk
->id
= le32_to_cpu(raw
->id
);
1194 blk
->name
= raw
->name
;
1195 blk
->name_len
= strnlen(raw
->name
, ARRAY_SIZE(raw
->name
));
1196 blk
->ncoeff
= le32_to_cpu(raw
->ncoeff
);
1201 if (sizeof(raw
->id
) > data_len
)
1205 blk
->id
= cs_dsp_coeff_parse_int(sizeof(raw
->id
), &tmp
);
1206 pos
= tmp
- region
->data
;
1208 tmp
= ®ion
->data
[pos
];
1209 blk
->name_len
= cs_dsp_coeff_parse_string(sizeof(u8
), &tmp
, data_len
- pos
,
1214 pos
= tmp
- region
->data
;
1215 cs_dsp_coeff_parse_string(sizeof(u16
), &tmp
, data_len
- pos
, NULL
);
1219 pos
= tmp
- region
->data
;
1220 if (sizeof(raw
->ncoeff
) > (data_len
- pos
))
1223 blk
->ncoeff
= cs_dsp_coeff_parse_int(sizeof(raw
->ncoeff
), &tmp
);
1224 pos
+= sizeof(raw
->ncoeff
);
1228 if ((int)blk
->ncoeff
< 0)
1231 cs_dsp_dbg(dsp
, "Algorithm ID: %#x\n", blk
->id
);
1232 cs_dsp_dbg(dsp
, "Algorithm name: %.*s\n", blk
->name_len
, blk
->name
);
1233 cs_dsp_dbg(dsp
, "# of coefficient descriptors: %#x\n", blk
->ncoeff
);
1238 static int cs_dsp_coeff_parse_coeff(struct cs_dsp
*dsp
,
1239 const struct wmfw_region
*region
,
1241 struct cs_dsp_coeff_parsed_coeff
*blk
)
1243 const struct wmfw_adsp_coeff_data
*raw
;
1244 unsigned int data_len
= le32_to_cpu(region
->len
);
1245 unsigned int blk_len
, blk_end_pos
;
1248 raw
= (const struct wmfw_adsp_coeff_data
*)®ion
->data
[pos
];
1249 if (sizeof(raw
->hdr
) > (data_len
- pos
))
1252 blk_len
= le32_to_cpu(raw
->hdr
.size
);
1253 if (blk_len
> S32_MAX
)
1256 if (blk_len
> (data_len
- pos
- sizeof(raw
->hdr
)))
1259 blk_end_pos
= pos
+ sizeof(raw
->hdr
) + blk_len
;
1261 blk
->offset
= le16_to_cpu(raw
->hdr
.offset
);
1262 blk
->mem_type
= le16_to_cpu(raw
->hdr
.type
);
1264 switch (dsp
->wmfw_ver
) {
1267 if (sizeof(*raw
) > (data_len
- pos
))
1270 blk
->name
= raw
->name
;
1271 blk
->name_len
= strnlen(raw
->name
, ARRAY_SIZE(raw
->name
));
1272 blk
->ctl_type
= le16_to_cpu(raw
->ctl_type
);
1273 blk
->flags
= le16_to_cpu(raw
->flags
);
1274 blk
->len
= le32_to_cpu(raw
->len
);
1277 pos
+= sizeof(raw
->hdr
);
1278 tmp
= ®ion
->data
[pos
];
1279 blk
->name_len
= cs_dsp_coeff_parse_string(sizeof(u8
), &tmp
, data_len
- pos
,
1284 pos
= tmp
- region
->data
;
1285 cs_dsp_coeff_parse_string(sizeof(u8
), &tmp
, data_len
- pos
, NULL
);
1289 pos
= tmp
- region
->data
;
1290 cs_dsp_coeff_parse_string(sizeof(u16
), &tmp
, data_len
- pos
, NULL
);
1294 pos
= tmp
- region
->data
;
1295 if (sizeof(raw
->ctl_type
) + sizeof(raw
->flags
) + sizeof(raw
->len
) >
1299 blk
->ctl_type
= cs_dsp_coeff_parse_int(sizeof(raw
->ctl_type
), &tmp
);
1300 pos
+= sizeof(raw
->ctl_type
);
1301 blk
->flags
= cs_dsp_coeff_parse_int(sizeof(raw
->flags
), &tmp
);
1302 pos
+= sizeof(raw
->flags
);
1303 blk
->len
= cs_dsp_coeff_parse_int(sizeof(raw
->len
), &tmp
);
1307 cs_dsp_dbg(dsp
, "\tCoefficient type: %#x\n", blk
->mem_type
);
1308 cs_dsp_dbg(dsp
, "\tCoefficient offset: %#x\n", blk
->offset
);
1309 cs_dsp_dbg(dsp
, "\tCoefficient name: %.*s\n", blk
->name_len
, blk
->name
);
1310 cs_dsp_dbg(dsp
, "\tCoefficient flags: %#x\n", blk
->flags
);
1311 cs_dsp_dbg(dsp
, "\tALSA control type: %#x\n", blk
->ctl_type
);
1312 cs_dsp_dbg(dsp
, "\tALSA control len: %#x\n", blk
->len
);
1317 static int cs_dsp_check_coeff_flags(struct cs_dsp
*dsp
,
1318 const struct cs_dsp_coeff_parsed_coeff
*coeff_blk
,
1319 unsigned int f_required
,
1320 unsigned int f_illegal
)
1322 if ((coeff_blk
->flags
& f_illegal
) ||
1323 ((coeff_blk
->flags
& f_required
) != f_required
)) {
1324 cs_dsp_err(dsp
, "Illegal flags 0x%x for control type 0x%x\n",
1325 coeff_blk
->flags
, coeff_blk
->ctl_type
);
1332 static int cs_dsp_parse_coeff(struct cs_dsp
*dsp
,
1333 const struct wmfw_region
*region
)
1335 struct cs_dsp_alg_region alg_region
= {};
1336 struct cs_dsp_coeff_parsed_alg alg_blk
;
1337 struct cs_dsp_coeff_parsed_coeff coeff_blk
;
1340 pos
= cs_dsp_coeff_parse_alg(dsp
, region
, &alg_blk
);
1344 for (i
= 0; i
< alg_blk
.ncoeff
; i
++) {
1345 pos
= cs_dsp_coeff_parse_coeff(dsp
, region
, pos
, &coeff_blk
);
1349 switch (coeff_blk
.ctl_type
) {
1350 case WMFW_CTL_TYPE_BYTES
:
1352 case WMFW_CTL_TYPE_ACKED
:
1353 if (coeff_blk
.flags
& WMFW_CTL_FLAG_SYS
)
1354 continue; /* ignore */
1356 ret
= cs_dsp_check_coeff_flags(dsp
, &coeff_blk
,
1357 WMFW_CTL_FLAG_VOLATILE
|
1358 WMFW_CTL_FLAG_WRITEABLE
|
1359 WMFW_CTL_FLAG_READABLE
,
1364 case WMFW_CTL_TYPE_HOSTEVENT
:
1365 case WMFW_CTL_TYPE_FWEVENT
:
1366 ret
= cs_dsp_check_coeff_flags(dsp
, &coeff_blk
,
1368 WMFW_CTL_FLAG_VOLATILE
|
1369 WMFW_CTL_FLAG_WRITEABLE
|
1370 WMFW_CTL_FLAG_READABLE
,
1375 case WMFW_CTL_TYPE_HOST_BUFFER
:
1376 ret
= cs_dsp_check_coeff_flags(dsp
, &coeff_blk
,
1378 WMFW_CTL_FLAG_VOLATILE
|
1379 WMFW_CTL_FLAG_READABLE
,
1385 cs_dsp_err(dsp
, "Unknown control type: %d\n",
1386 coeff_blk
.ctl_type
);
1390 alg_region
.type
= coeff_blk
.mem_type
;
1391 alg_region
.alg
= alg_blk
.id
;
1393 ret
= cs_dsp_create_control(dsp
, &alg_region
,
1399 coeff_blk
.ctl_type
);
1401 cs_dsp_err(dsp
, "Failed to create control: %.*s, %d\n",
1402 coeff_blk
.name_len
, coeff_blk
.name
, ret
);
1408 static unsigned int cs_dsp_adsp1_parse_sizes(struct cs_dsp
*dsp
,
1409 const char * const file
,
1411 const struct firmware
*firmware
)
1413 const struct wmfw_adsp1_sizes
*adsp1_sizes
;
1415 adsp1_sizes
= (void *)&firmware
->data
[pos
];
1416 if (sizeof(*adsp1_sizes
) > firmware
->size
- pos
) {
1417 cs_dsp_err(dsp
, "%s: file truncated\n", file
);
1421 cs_dsp_dbg(dsp
, "%s: %d DM, %d PM, %d ZM\n", file
,
1422 le32_to_cpu(adsp1_sizes
->dm
), le32_to_cpu(adsp1_sizes
->pm
),
1423 le32_to_cpu(adsp1_sizes
->zm
));
1425 return pos
+ sizeof(*adsp1_sizes
);
1428 static unsigned int cs_dsp_adsp2_parse_sizes(struct cs_dsp
*dsp
,
1429 const char * const file
,
1431 const struct firmware
*firmware
)
1433 const struct wmfw_adsp2_sizes
*adsp2_sizes
;
1435 adsp2_sizes
= (void *)&firmware
->data
[pos
];
1436 if (sizeof(*adsp2_sizes
) > firmware
->size
- pos
) {
1437 cs_dsp_err(dsp
, "%s: file truncated\n", file
);
1441 cs_dsp_dbg(dsp
, "%s: %d XM, %d YM %d PM, %d ZM\n", file
,
1442 le32_to_cpu(adsp2_sizes
->xm
), le32_to_cpu(adsp2_sizes
->ym
),
1443 le32_to_cpu(adsp2_sizes
->pm
), le32_to_cpu(adsp2_sizes
->zm
));
1445 return pos
+ sizeof(*adsp2_sizes
);
1448 static bool cs_dsp_validate_version(struct cs_dsp
*dsp
, unsigned int version
)
1452 cs_dsp_warn(dsp
, "Deprecated file format %d\n", version
);
1462 static bool cs_dsp_halo_validate_version(struct cs_dsp
*dsp
, unsigned int version
)
1472 static int cs_dsp_load(struct cs_dsp
*dsp
, const struct firmware
*firmware
,
1475 LIST_HEAD(buf_list
);
1476 struct regmap
*regmap
= dsp
->regmap
;
1477 unsigned int pos
= 0;
1478 const struct wmfw_header
*header
;
1479 const struct wmfw_footer
*footer
;
1480 const struct wmfw_region
*region
;
1481 const struct cs_dsp_region
*mem
;
1482 const char *region_name
;
1483 struct cs_dsp_buf
*buf
;
1486 int ret
, offset
, type
;
1493 if (sizeof(*header
) >= firmware
->size
) {
1498 header
= (void *)&firmware
->data
[0];
1500 if (memcmp(&header
->magic
[0], "WMFW", 4) != 0) {
1501 cs_dsp_err(dsp
, "%s: invalid magic\n", file
);
1505 if (!dsp
->ops
->validate_version(dsp
, header
->ver
)) {
1506 cs_dsp_err(dsp
, "%s: unknown file format %d\n",
1511 dsp
->wmfw_ver
= header
->ver
;
1513 if (header
->core
!= dsp
->type
) {
1514 cs_dsp_err(dsp
, "%s: invalid core %d != %d\n",
1515 file
, header
->core
, dsp
->type
);
1519 pos
= sizeof(*header
);
1520 pos
= dsp
->ops
->parse_sizes(dsp
, file
, pos
, firmware
);
1521 if ((pos
== 0) || (sizeof(*footer
) > firmware
->size
- pos
)) {
1526 footer
= (void *)&firmware
->data
[pos
];
1527 pos
+= sizeof(*footer
);
1529 if (le32_to_cpu(header
->len
) != pos
) {
1534 cs_dsp_info(dsp
, "%s: format %d timestamp %#llx\n", file
, header
->ver
,
1535 le64_to_cpu(footer
->timestamp
));
1537 while (pos
< firmware
->size
) {
1538 /* Is there enough data for a complete block header? */
1539 if (sizeof(*region
) > firmware
->size
- pos
) {
1544 region
= (void *)&(firmware
->data
[pos
]);
1546 if (le32_to_cpu(region
->len
) > firmware
->size
- pos
- sizeof(*region
)) {
1551 region_name
= "Unknown";
1553 offset
= le32_to_cpu(region
->offset
) & 0xffffff;
1554 type
= be32_to_cpu(region
->type
) & 0xff;
1557 case WMFW_INFO_TEXT
:
1558 case WMFW_NAME_TEXT
:
1559 region_name
= "Info/Name";
1560 cs_dsp_info(dsp
, "%s: %.*s\n", file
,
1561 min(le32_to_cpu(region
->len
), 100), region
->data
);
1563 case WMFW_ALGORITHM_DATA
:
1564 region_name
= "Algorithm";
1565 ret
= cs_dsp_parse_coeff(dsp
, region
);
1570 region_name
= "Absolute";
1578 case WMFW_HALO_PM_PACKED
:
1579 case WMFW_HALO_XM_PACKED
:
1580 case WMFW_HALO_YM_PACKED
:
1581 mem
= cs_dsp_find_region(dsp
, type
);
1583 cs_dsp_err(dsp
, "No region of type: %x\n", type
);
1588 region_name
= cs_dsp_mem_region_name(type
);
1589 reg
= dsp
->ops
->region_to_reg(mem
, offset
);
1593 "%s.%d: Unknown region type %x at %d(%x)\n",
1594 file
, regions
, type
, pos
, pos
);
1598 cs_dsp_dbg(dsp
, "%s.%d: %d bytes at %d in %s\n", file
,
1599 regions
, le32_to_cpu(region
->len
), offset
,
1603 buf
= cs_dsp_buf_alloc(region
->data
,
1604 le32_to_cpu(region
->len
),
1607 cs_dsp_err(dsp
, "Out of memory\n");
1612 ret
= regmap_raw_write_async(regmap
, reg
, buf
->buf
,
1613 le32_to_cpu(region
->len
));
1616 "%s.%d: Failed to write %d bytes at %d in %s: %d\n",
1618 le32_to_cpu(region
->len
), offset
,
1624 pos
+= le32_to_cpu(region
->len
) + sizeof(*region
);
1628 ret
= regmap_async_complete(regmap
);
1630 cs_dsp_err(dsp
, "Failed to complete async write: %d\n", ret
);
1634 if (pos
> firmware
->size
)
1635 cs_dsp_warn(dsp
, "%s.%d: %zu bytes at end of file\n",
1636 file
, regions
, pos
- firmware
->size
);
1638 cs_dsp_debugfs_save_wmfwname(dsp
, file
);
1641 regmap_async_complete(regmap
);
1642 cs_dsp_buf_free(&buf_list
);
1644 if (ret
== -EOVERFLOW
)
1645 cs_dsp_err(dsp
, "%s: file content overflows file data\n", file
);
1651 * cs_dsp_get_ctl() - Finds a matching coefficient control
1652 * @dsp: pointer to DSP structure
1653 * @name: pointer to string to match with a control's subname
1654 * @type: the algorithm type to match
1655 * @alg: the algorithm id to match
1657 * Find cs_dsp_coeff_ctl with input name as its subname
1659 * Return: pointer to the control on success, NULL if not found
1661 struct cs_dsp_coeff_ctl
*cs_dsp_get_ctl(struct cs_dsp
*dsp
, const char *name
, int type
,
1664 struct cs_dsp_coeff_ctl
*pos
, *rslt
= NULL
;
1666 lockdep_assert_held(&dsp
->pwr_lock
);
1668 list_for_each_entry(pos
, &dsp
->ctl_list
, list
) {
1671 if (strncmp(pos
->subname
, name
, pos
->subname_len
) == 0 &&
1672 pos
->fw_name
== dsp
->fw_name
&&
1673 pos
->alg_region
.alg
== alg
&&
1674 pos
->alg_region
.type
== type
) {
1682 EXPORT_SYMBOL_NS_GPL(cs_dsp_get_ctl
, FW_CS_DSP
);
1684 static void cs_dsp_ctl_fixup_base(struct cs_dsp
*dsp
,
1685 const struct cs_dsp_alg_region
*alg_region
)
1687 struct cs_dsp_coeff_ctl
*ctl
;
1689 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
) {
1690 if (ctl
->fw_name
== dsp
->fw_name
&&
1691 alg_region
->alg
== ctl
->alg_region
.alg
&&
1692 alg_region
->type
== ctl
->alg_region
.type
) {
1693 ctl
->alg_region
.base
= alg_region
->base
;
1698 static void *cs_dsp_read_algs(struct cs_dsp
*dsp
, size_t n_algs
,
1699 const struct cs_dsp_region
*mem
,
1700 unsigned int pos
, unsigned int len
)
1708 cs_dsp_err(dsp
, "No algorithms\n");
1709 return ERR_PTR(-EINVAL
);
1712 if (n_algs
> 1024) {
1713 cs_dsp_err(dsp
, "Algorithm count %zx excessive\n", n_algs
);
1714 return ERR_PTR(-EINVAL
);
1717 /* Read the terminator first to validate the length */
1718 reg
= dsp
->ops
->region_to_reg(mem
, pos
+ len
);
1720 ret
= regmap_raw_read(dsp
->regmap
, reg
, &val
, sizeof(val
));
1722 cs_dsp_err(dsp
, "Failed to read algorithm list end: %d\n",
1724 return ERR_PTR(ret
);
1727 if (be32_to_cpu(val
) != 0xbedead)
1728 cs_dsp_warn(dsp
, "Algorithm list end %x 0x%x != 0xbedead\n",
1729 reg
, be32_to_cpu(val
));
1731 /* Convert length from DSP words to bytes */
1734 alg
= kzalloc(len
, GFP_KERNEL
| GFP_DMA
);
1736 return ERR_PTR(-ENOMEM
);
1738 reg
= dsp
->ops
->region_to_reg(mem
, pos
);
1740 ret
= regmap_raw_read(dsp
->regmap
, reg
, alg
, len
);
1742 cs_dsp_err(dsp
, "Failed to read algorithm list: %d\n", ret
);
1744 return ERR_PTR(ret
);
1751 * cs_dsp_find_alg_region() - Finds a matching algorithm region
1752 * @dsp: pointer to DSP structure
1753 * @type: the algorithm type to match
1754 * @id: the algorithm id to match
1756 * Return: Pointer to matching algorithm region, or NULL if not found.
1758 struct cs_dsp_alg_region
*cs_dsp_find_alg_region(struct cs_dsp
*dsp
,
1759 int type
, unsigned int id
)
1761 struct cs_dsp_alg_region
*alg_region
;
1763 lockdep_assert_held(&dsp
->pwr_lock
);
1765 list_for_each_entry(alg_region
, &dsp
->alg_regions
, list
) {
1766 if (id
== alg_region
->alg
&& type
== alg_region
->type
)
1772 EXPORT_SYMBOL_NS_GPL(cs_dsp_find_alg_region
, FW_CS_DSP
);
1774 static struct cs_dsp_alg_region
*cs_dsp_create_region(struct cs_dsp
*dsp
,
1775 int type
, __be32 id
,
1776 __be32 ver
, __be32 base
)
1778 struct cs_dsp_alg_region
*alg_region
;
1780 alg_region
= kzalloc(sizeof(*alg_region
), GFP_KERNEL
);
1782 return ERR_PTR(-ENOMEM
);
1784 alg_region
->type
= type
;
1785 alg_region
->alg
= be32_to_cpu(id
);
1786 alg_region
->ver
= be32_to_cpu(ver
);
1787 alg_region
->base
= be32_to_cpu(base
);
1789 list_add_tail(&alg_region
->list
, &dsp
->alg_regions
);
1791 if (dsp
->wmfw_ver
> 0)
1792 cs_dsp_ctl_fixup_base(dsp
, alg_region
);
1797 static void cs_dsp_free_alg_regions(struct cs_dsp
*dsp
)
1799 struct cs_dsp_alg_region
*alg_region
;
1801 while (!list_empty(&dsp
->alg_regions
)) {
1802 alg_region
= list_first_entry(&dsp
->alg_regions
,
1803 struct cs_dsp_alg_region
,
1805 list_del(&alg_region
->list
);
1810 static void cs_dsp_parse_wmfw_id_header(struct cs_dsp
*dsp
,
1811 struct wmfw_id_hdr
*fw
, int nalgs
)
1813 dsp
->fw_id
= be32_to_cpu(fw
->id
);
1814 dsp
->fw_id_version
= be32_to_cpu(fw
->ver
);
1816 cs_dsp_info(dsp
, "Firmware: %x v%d.%d.%d, %d algorithms\n",
1817 dsp
->fw_id
, (dsp
->fw_id_version
& 0xff0000) >> 16,
1818 (dsp
->fw_id_version
& 0xff00) >> 8, dsp
->fw_id_version
& 0xff,
1822 static void cs_dsp_parse_wmfw_v3_id_header(struct cs_dsp
*dsp
,
1823 struct wmfw_v3_id_hdr
*fw
, int nalgs
)
1825 dsp
->fw_id
= be32_to_cpu(fw
->id
);
1826 dsp
->fw_id_version
= be32_to_cpu(fw
->ver
);
1827 dsp
->fw_vendor_id
= be32_to_cpu(fw
->vendor_id
);
1829 cs_dsp_info(dsp
, "Firmware: %x vendor: 0x%x v%d.%d.%d, %d algorithms\n",
1830 dsp
->fw_id
, dsp
->fw_vendor_id
,
1831 (dsp
->fw_id_version
& 0xff0000) >> 16,
1832 (dsp
->fw_id_version
& 0xff00) >> 8, dsp
->fw_id_version
& 0xff,
1836 static int cs_dsp_create_regions(struct cs_dsp
*dsp
, __be32 id
, __be32 ver
,
1837 int nregions
, const int *type
, __be32
*base
)
1839 struct cs_dsp_alg_region
*alg_region
;
1842 for (i
= 0; i
< nregions
; i
++) {
1843 alg_region
= cs_dsp_create_region(dsp
, type
[i
], id
, ver
, base
[i
]);
1844 if (IS_ERR(alg_region
))
1845 return PTR_ERR(alg_region
);
1851 static int cs_dsp_adsp1_setup_algs(struct cs_dsp
*dsp
)
1853 struct wmfw_adsp1_id_hdr adsp1_id
;
1854 struct wmfw_adsp1_alg_hdr
*adsp1_alg
;
1855 struct cs_dsp_alg_region
*alg_region
;
1856 const struct cs_dsp_region
*mem
;
1857 unsigned int pos
, len
;
1861 mem
= cs_dsp_find_region(dsp
, WMFW_ADSP1_DM
);
1865 ret
= regmap_raw_read(dsp
->regmap
, mem
->base
, &adsp1_id
,
1868 cs_dsp_err(dsp
, "Failed to read algorithm info: %d\n",
1873 n_algs
= be32_to_cpu(adsp1_id
.n_algs
);
1875 cs_dsp_parse_wmfw_id_header(dsp
, &adsp1_id
.fw
, n_algs
);
1877 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP1_ZM
,
1878 adsp1_id
.fw
.id
, adsp1_id
.fw
.ver
,
1880 if (IS_ERR(alg_region
))
1881 return PTR_ERR(alg_region
);
1883 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP1_DM
,
1884 adsp1_id
.fw
.id
, adsp1_id
.fw
.ver
,
1886 if (IS_ERR(alg_region
))
1887 return PTR_ERR(alg_region
);
1889 /* Calculate offset and length in DSP words */
1890 pos
= sizeof(adsp1_id
) / sizeof(u32
);
1891 len
= (sizeof(*adsp1_alg
) * n_algs
) / sizeof(u32
);
1893 adsp1_alg
= cs_dsp_read_algs(dsp
, n_algs
, mem
, pos
, len
);
1894 if (IS_ERR(adsp1_alg
))
1895 return PTR_ERR(adsp1_alg
);
1897 for (i
= 0; i
< n_algs
; i
++) {
1898 cs_dsp_info(dsp
, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
1899 i
, be32_to_cpu(adsp1_alg
[i
].alg
.id
),
1900 (be32_to_cpu(adsp1_alg
[i
].alg
.ver
) & 0xff0000) >> 16,
1901 (be32_to_cpu(adsp1_alg
[i
].alg
.ver
) & 0xff00) >> 8,
1902 be32_to_cpu(adsp1_alg
[i
].alg
.ver
) & 0xff,
1903 be32_to_cpu(adsp1_alg
[i
].dm
),
1904 be32_to_cpu(adsp1_alg
[i
].zm
));
1906 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP1_DM
,
1907 adsp1_alg
[i
].alg
.id
,
1908 adsp1_alg
[i
].alg
.ver
,
1910 if (IS_ERR(alg_region
)) {
1911 ret
= PTR_ERR(alg_region
);
1914 if (dsp
->wmfw_ver
== 0) {
1915 if (i
+ 1 < n_algs
) {
1916 len
= be32_to_cpu(adsp1_alg
[i
+ 1].dm
);
1917 len
-= be32_to_cpu(adsp1_alg
[i
].dm
);
1919 cs_dsp_create_control(dsp
, alg_region
, 0,
1921 WMFW_CTL_TYPE_BYTES
);
1923 cs_dsp_warn(dsp
, "Missing length info for region DM with ID %x\n",
1924 be32_to_cpu(adsp1_alg
[i
].alg
.id
));
1928 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP1_ZM
,
1929 adsp1_alg
[i
].alg
.id
,
1930 adsp1_alg
[i
].alg
.ver
,
1932 if (IS_ERR(alg_region
)) {
1933 ret
= PTR_ERR(alg_region
);
1936 if (dsp
->wmfw_ver
== 0) {
1937 if (i
+ 1 < n_algs
) {
1938 len
= be32_to_cpu(adsp1_alg
[i
+ 1].zm
);
1939 len
-= be32_to_cpu(adsp1_alg
[i
].zm
);
1941 cs_dsp_create_control(dsp
, alg_region
, 0,
1943 WMFW_CTL_TYPE_BYTES
);
1945 cs_dsp_warn(dsp
, "Missing length info for region ZM with ID %x\n",
1946 be32_to_cpu(adsp1_alg
[i
].alg
.id
));
1956 static int cs_dsp_adsp2_setup_algs(struct cs_dsp
*dsp
)
1958 struct wmfw_adsp2_id_hdr adsp2_id
;
1959 struct wmfw_adsp2_alg_hdr
*adsp2_alg
;
1960 struct cs_dsp_alg_region
*alg_region
;
1961 const struct cs_dsp_region
*mem
;
1962 unsigned int pos
, len
;
1966 mem
= cs_dsp_find_region(dsp
, WMFW_ADSP2_XM
);
1970 ret
= regmap_raw_read(dsp
->regmap
, mem
->base
, &adsp2_id
,
1973 cs_dsp_err(dsp
, "Failed to read algorithm info: %d\n",
1978 n_algs
= be32_to_cpu(adsp2_id
.n_algs
);
1980 cs_dsp_parse_wmfw_id_header(dsp
, &adsp2_id
.fw
, n_algs
);
1982 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP2_XM
,
1983 adsp2_id
.fw
.id
, adsp2_id
.fw
.ver
,
1985 if (IS_ERR(alg_region
))
1986 return PTR_ERR(alg_region
);
1988 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP2_YM
,
1989 adsp2_id
.fw
.id
, adsp2_id
.fw
.ver
,
1991 if (IS_ERR(alg_region
))
1992 return PTR_ERR(alg_region
);
1994 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP2_ZM
,
1995 adsp2_id
.fw
.id
, adsp2_id
.fw
.ver
,
1997 if (IS_ERR(alg_region
))
1998 return PTR_ERR(alg_region
);
2000 /* Calculate offset and length in DSP words */
2001 pos
= sizeof(adsp2_id
) / sizeof(u32
);
2002 len
= (sizeof(*adsp2_alg
) * n_algs
) / sizeof(u32
);
2004 adsp2_alg
= cs_dsp_read_algs(dsp
, n_algs
, mem
, pos
, len
);
2005 if (IS_ERR(adsp2_alg
))
2006 return PTR_ERR(adsp2_alg
);
2008 for (i
= 0; i
< n_algs
; i
++) {
2010 "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
2011 i
, be32_to_cpu(adsp2_alg
[i
].alg
.id
),
2012 (be32_to_cpu(adsp2_alg
[i
].alg
.ver
) & 0xff0000) >> 16,
2013 (be32_to_cpu(adsp2_alg
[i
].alg
.ver
) & 0xff00) >> 8,
2014 be32_to_cpu(adsp2_alg
[i
].alg
.ver
) & 0xff,
2015 be32_to_cpu(adsp2_alg
[i
].xm
),
2016 be32_to_cpu(adsp2_alg
[i
].ym
),
2017 be32_to_cpu(adsp2_alg
[i
].zm
));
2019 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP2_XM
,
2020 adsp2_alg
[i
].alg
.id
,
2021 adsp2_alg
[i
].alg
.ver
,
2023 if (IS_ERR(alg_region
)) {
2024 ret
= PTR_ERR(alg_region
);
2027 if (dsp
->wmfw_ver
== 0) {
2028 if (i
+ 1 < n_algs
) {
2029 len
= be32_to_cpu(adsp2_alg
[i
+ 1].xm
);
2030 len
-= be32_to_cpu(adsp2_alg
[i
].xm
);
2032 cs_dsp_create_control(dsp
, alg_region
, 0,
2034 WMFW_CTL_TYPE_BYTES
);
2036 cs_dsp_warn(dsp
, "Missing length info for region XM with ID %x\n",
2037 be32_to_cpu(adsp2_alg
[i
].alg
.id
));
2041 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP2_YM
,
2042 adsp2_alg
[i
].alg
.id
,
2043 adsp2_alg
[i
].alg
.ver
,
2045 if (IS_ERR(alg_region
)) {
2046 ret
= PTR_ERR(alg_region
);
2049 if (dsp
->wmfw_ver
== 0) {
2050 if (i
+ 1 < n_algs
) {
2051 len
= be32_to_cpu(adsp2_alg
[i
+ 1].ym
);
2052 len
-= be32_to_cpu(adsp2_alg
[i
].ym
);
2054 cs_dsp_create_control(dsp
, alg_region
, 0,
2056 WMFW_CTL_TYPE_BYTES
);
2058 cs_dsp_warn(dsp
, "Missing length info for region YM with ID %x\n",
2059 be32_to_cpu(adsp2_alg
[i
].alg
.id
));
2063 alg_region
= cs_dsp_create_region(dsp
, WMFW_ADSP2_ZM
,
2064 adsp2_alg
[i
].alg
.id
,
2065 adsp2_alg
[i
].alg
.ver
,
2067 if (IS_ERR(alg_region
)) {
2068 ret
= PTR_ERR(alg_region
);
2071 if (dsp
->wmfw_ver
== 0) {
2072 if (i
+ 1 < n_algs
) {
2073 len
= be32_to_cpu(adsp2_alg
[i
+ 1].zm
);
2074 len
-= be32_to_cpu(adsp2_alg
[i
].zm
);
2076 cs_dsp_create_control(dsp
, alg_region
, 0,
2078 WMFW_CTL_TYPE_BYTES
);
2080 cs_dsp_warn(dsp
, "Missing length info for region ZM with ID %x\n",
2081 be32_to_cpu(adsp2_alg
[i
].alg
.id
));
2091 static int cs_dsp_halo_create_regions(struct cs_dsp
*dsp
, __be32 id
, __be32 ver
,
2092 __be32 xm_base
, __be32 ym_base
)
2094 static const int types
[] = {
2095 WMFW_ADSP2_XM
, WMFW_HALO_XM_PACKED
,
2096 WMFW_ADSP2_YM
, WMFW_HALO_YM_PACKED
2098 __be32 bases
[] = { xm_base
, xm_base
, ym_base
, ym_base
};
2100 return cs_dsp_create_regions(dsp
, id
, ver
, ARRAY_SIZE(types
), types
, bases
);
2103 static int cs_dsp_halo_setup_algs(struct cs_dsp
*dsp
)
2105 struct wmfw_halo_id_hdr halo_id
;
2106 struct wmfw_halo_alg_hdr
*halo_alg
;
2107 const struct cs_dsp_region
*mem
;
2108 unsigned int pos
, len
;
2112 mem
= cs_dsp_find_region(dsp
, WMFW_ADSP2_XM
);
2116 ret
= regmap_raw_read(dsp
->regmap
, mem
->base
, &halo_id
,
2119 cs_dsp_err(dsp
, "Failed to read algorithm info: %d\n",
2124 n_algs
= be32_to_cpu(halo_id
.n_algs
);
2126 cs_dsp_parse_wmfw_v3_id_header(dsp
, &halo_id
.fw
, n_algs
);
2128 ret
= cs_dsp_halo_create_regions(dsp
, halo_id
.fw
.id
, halo_id
.fw
.ver
,
2129 halo_id
.xm_base
, halo_id
.ym_base
);
2133 /* Calculate offset and length in DSP words */
2134 pos
= sizeof(halo_id
) / sizeof(u32
);
2135 len
= (sizeof(*halo_alg
) * n_algs
) / sizeof(u32
);
2137 halo_alg
= cs_dsp_read_algs(dsp
, n_algs
, mem
, pos
, len
);
2138 if (IS_ERR(halo_alg
))
2139 return PTR_ERR(halo_alg
);
2141 for (i
= 0; i
< n_algs
; i
++) {
2143 "%d: ID %x v%d.%d.%d XM@%x YM@%x\n",
2144 i
, be32_to_cpu(halo_alg
[i
].alg
.id
),
2145 (be32_to_cpu(halo_alg
[i
].alg
.ver
) & 0xff0000) >> 16,
2146 (be32_to_cpu(halo_alg
[i
].alg
.ver
) & 0xff00) >> 8,
2147 be32_to_cpu(halo_alg
[i
].alg
.ver
) & 0xff,
2148 be32_to_cpu(halo_alg
[i
].xm_base
),
2149 be32_to_cpu(halo_alg
[i
].ym_base
));
2151 ret
= cs_dsp_halo_create_regions(dsp
, halo_alg
[i
].alg
.id
,
2152 halo_alg
[i
].alg
.ver
,
2153 halo_alg
[i
].xm_base
,
2154 halo_alg
[i
].ym_base
);
2164 static int cs_dsp_load_coeff(struct cs_dsp
*dsp
, const struct firmware
*firmware
,
2167 LIST_HEAD(buf_list
);
2168 struct regmap
*regmap
= dsp
->regmap
;
2169 struct wmfw_coeff_hdr
*hdr
;
2170 struct wmfw_coeff_item
*blk
;
2171 const struct cs_dsp_region
*mem
;
2172 struct cs_dsp_alg_region
*alg_region
;
2173 const char *region_name
;
2174 int ret
, pos
, blocks
, type
, offset
, reg
, version
;
2175 struct cs_dsp_buf
*buf
;
2182 if (sizeof(*hdr
) >= firmware
->size
) {
2183 cs_dsp_err(dsp
, "%s: coefficient file too short, %zu bytes\n",
2184 file
, firmware
->size
);
2188 hdr
= (void *)&firmware
->data
[0];
2189 if (memcmp(hdr
->magic
, "WMDR", 4) != 0) {
2190 cs_dsp_err(dsp
, "%s: invalid coefficient magic\n", file
);
2194 switch (be32_to_cpu(hdr
->rev
) & 0xff) {
2199 cs_dsp_err(dsp
, "%s: Unsupported coefficient file format %d\n",
2200 file
, be32_to_cpu(hdr
->rev
) & 0xff);
2205 cs_dsp_info(dsp
, "%s: v%d.%d.%d\n", file
,
2206 (le32_to_cpu(hdr
->ver
) >> 16) & 0xff,
2207 (le32_to_cpu(hdr
->ver
) >> 8) & 0xff,
2208 le32_to_cpu(hdr
->ver
) & 0xff);
2210 pos
= le32_to_cpu(hdr
->len
);
2213 while (pos
< firmware
->size
) {
2214 /* Is there enough data for a complete block header? */
2215 if (sizeof(*blk
) > firmware
->size
- pos
) {
2220 blk
= (void *)(&firmware
->data
[pos
]);
2222 if (le32_to_cpu(blk
->len
) > firmware
->size
- pos
- sizeof(*blk
)) {
2227 type
= le16_to_cpu(blk
->type
);
2228 offset
= le16_to_cpu(blk
->offset
);
2229 version
= le32_to_cpu(blk
->ver
) >> 8;
2231 cs_dsp_dbg(dsp
, "%s.%d: %x v%d.%d.%d\n",
2232 file
, blocks
, le32_to_cpu(blk
->id
),
2233 (le32_to_cpu(blk
->ver
) >> 16) & 0xff,
2234 (le32_to_cpu(blk
->ver
) >> 8) & 0xff,
2235 le32_to_cpu(blk
->ver
) & 0xff);
2236 cs_dsp_dbg(dsp
, "%s.%d: %d bytes at 0x%x in %x\n",
2237 file
, blocks
, le32_to_cpu(blk
->len
), offset
, type
);
2240 region_name
= "Unknown";
2242 case (WMFW_NAME_TEXT
<< 8):
2243 cs_dsp_info(dsp
, "%s: %.*s\n", dsp
->fw_name
,
2244 min(le32_to_cpu(blk
->len
), 100), blk
->data
);
2246 case (WMFW_INFO_TEXT
<< 8):
2247 case (WMFW_METADATA
<< 8):
2249 case (WMFW_ABSOLUTE
<< 8):
2251 * Old files may use this for global
2254 if (le32_to_cpu(blk
->id
) == dsp
->fw_id
&&
2256 region_name
= "global coefficients";
2257 mem
= cs_dsp_find_region(dsp
, type
);
2259 cs_dsp_err(dsp
, "No ZM\n");
2262 reg
= dsp
->ops
->region_to_reg(mem
, 0);
2265 region_name
= "register";
2274 case WMFW_HALO_XM_PACKED
:
2275 case WMFW_HALO_YM_PACKED
:
2276 case WMFW_HALO_PM_PACKED
:
2277 cs_dsp_dbg(dsp
, "%s.%d: %d bytes in %x for %x\n",
2278 file
, blocks
, le32_to_cpu(blk
->len
),
2279 type
, le32_to_cpu(blk
->id
));
2281 region_name
= cs_dsp_mem_region_name(type
);
2282 mem
= cs_dsp_find_region(dsp
, type
);
2284 cs_dsp_err(dsp
, "No base for region %x\n", type
);
2288 alg_region
= cs_dsp_find_alg_region(dsp
, type
,
2289 le32_to_cpu(blk
->id
));
2291 if (version
!= alg_region
->ver
)
2293 "Algorithm coefficient version %d.%d.%d but expected %d.%d.%d\n",
2294 (version
>> 16) & 0xFF,
2295 (version
>> 8) & 0xFF,
2297 (alg_region
->ver
>> 16) & 0xFF,
2298 (alg_region
->ver
>> 8) & 0xFF,
2299 alg_region
->ver
& 0xFF);
2301 reg
= alg_region
->base
;
2302 reg
= dsp
->ops
->region_to_reg(mem
, reg
);
2305 cs_dsp_err(dsp
, "No %s for algorithm %x\n",
2306 region_name
, le32_to_cpu(blk
->id
));
2311 cs_dsp_err(dsp
, "%s.%d: Unknown region type %x at %d\n",
2312 file
, blocks
, type
, pos
);
2317 buf
= cs_dsp_buf_alloc(blk
->data
,
2318 le32_to_cpu(blk
->len
),
2321 cs_dsp_err(dsp
, "Out of memory\n");
2326 cs_dsp_dbg(dsp
, "%s.%d: Writing %d bytes at %x\n",
2327 file
, blocks
, le32_to_cpu(blk
->len
),
2329 ret
= regmap_raw_write_async(regmap
, reg
, buf
->buf
,
2330 le32_to_cpu(blk
->len
));
2333 "%s.%d: Failed to write to %x in %s: %d\n",
2334 file
, blocks
, reg
, region_name
, ret
);
2338 pos
+= (le32_to_cpu(blk
->len
) + sizeof(*blk
) + 3) & ~0x03;
2342 ret
= regmap_async_complete(regmap
);
2344 cs_dsp_err(dsp
, "Failed to complete async write: %d\n", ret
);
2346 if (pos
> firmware
->size
)
2347 cs_dsp_warn(dsp
, "%s.%d: %zu bytes at end of file\n",
2348 file
, blocks
, pos
- firmware
->size
);
2350 cs_dsp_debugfs_save_binname(dsp
, file
);
2353 regmap_async_complete(regmap
);
2354 cs_dsp_buf_free(&buf_list
);
2356 if (ret
== -EOVERFLOW
)
2357 cs_dsp_err(dsp
, "%s: file content overflows file data\n", file
);
2362 static int cs_dsp_create_name(struct cs_dsp
*dsp
)
2365 dsp
->name
= devm_kasprintf(dsp
->dev
, GFP_KERNEL
, "DSP%d",
2374 static int cs_dsp_common_init(struct cs_dsp
*dsp
)
2378 ret
= cs_dsp_create_name(dsp
);
2382 INIT_LIST_HEAD(&dsp
->alg_regions
);
2383 INIT_LIST_HEAD(&dsp
->ctl_list
);
2385 mutex_init(&dsp
->pwr_lock
);
2387 #ifdef CONFIG_DEBUG_FS
2388 /* Ensure this is invalid if client never provides a debugfs root */
2389 dsp
->debugfs_root
= ERR_PTR(-ENODEV
);
2396 * cs_dsp_adsp1_init() - Initialise a cs_dsp structure representing a ADSP1 device
2397 * @dsp: pointer to DSP structure
2399 * Return: Zero for success, a negative number on error.
2401 int cs_dsp_adsp1_init(struct cs_dsp
*dsp
)
2403 dsp
->ops
= &cs_dsp_adsp1_ops
;
2405 return cs_dsp_common_init(dsp
);
2407 EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp1_init
, FW_CS_DSP
);
2410 * cs_dsp_adsp1_power_up() - Load and start the named firmware
2411 * @dsp: pointer to DSP structure
2412 * @wmfw_firmware: the firmware to be sent
2413 * @wmfw_filename: file name of firmware to be sent
2414 * @coeff_firmware: the coefficient data to be sent
2415 * @coeff_filename: file name of coefficient to data be sent
2416 * @fw_name: the user-friendly firmware name
2418 * Return: Zero for success, a negative number on error.
2420 int cs_dsp_adsp1_power_up(struct cs_dsp
*dsp
,
2421 const struct firmware
*wmfw_firmware
, const char *wmfw_filename
,
2422 const struct firmware
*coeff_firmware
, const char *coeff_filename
,
2423 const char *fw_name
)
2428 mutex_lock(&dsp
->pwr_lock
);
2430 dsp
->fw_name
= fw_name
;
2432 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
2433 ADSP1_SYS_ENA
, ADSP1_SYS_ENA
);
2436 * For simplicity set the DSP clock rate to be the
2437 * SYSCLK rate rather than making it configurable.
2439 if (dsp
->sysclk_reg
) {
2440 ret
= regmap_read(dsp
->regmap
, dsp
->sysclk_reg
, &val
);
2442 cs_dsp_err(dsp
, "Failed to read SYSCLK state: %d\n", ret
);
2446 val
= (val
& dsp
->sysclk_mask
) >> dsp
->sysclk_shift
;
2448 ret
= regmap_update_bits(dsp
->regmap
,
2449 dsp
->base
+ ADSP1_CONTROL_31
,
2450 ADSP1_CLK_SEL_MASK
, val
);
2452 cs_dsp_err(dsp
, "Failed to set clock rate: %d\n", ret
);
2457 ret
= cs_dsp_load(dsp
, wmfw_firmware
, wmfw_filename
);
2461 ret
= cs_dsp_adsp1_setup_algs(dsp
);
2465 ret
= cs_dsp_load_coeff(dsp
, coeff_firmware
, coeff_filename
);
2469 /* Initialize caches for enabled and unset controls */
2470 ret
= cs_dsp_coeff_init_control_caches(dsp
);
2474 /* Sync set controls */
2475 ret
= cs_dsp_coeff_sync_controls(dsp
);
2481 /* Start the core running */
2482 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
2483 ADSP1_CORE_ENA
| ADSP1_START
,
2484 ADSP1_CORE_ENA
| ADSP1_START
);
2486 dsp
->running
= true;
2488 mutex_unlock(&dsp
->pwr_lock
);
2493 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
2496 mutex_unlock(&dsp
->pwr_lock
);
2499 EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp1_power_up
, FW_CS_DSP
);
2502 * cs_dsp_adsp1_power_down() - Halts the DSP
2503 * @dsp: pointer to DSP structure
2505 void cs_dsp_adsp1_power_down(struct cs_dsp
*dsp
)
2507 struct cs_dsp_coeff_ctl
*ctl
;
2509 mutex_lock(&dsp
->pwr_lock
);
2511 dsp
->running
= false;
2512 dsp
->booted
= false;
2515 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
2516 ADSP1_CORE_ENA
| ADSP1_START
, 0);
2518 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_19
,
2519 ADSP1_WDMA_BUFFER_LENGTH_MASK
, 0);
2521 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP1_CONTROL_30
,
2524 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
)
2527 cs_dsp_free_alg_regions(dsp
);
2529 mutex_unlock(&dsp
->pwr_lock
);
2531 EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp1_power_down
, FW_CS_DSP
);
2533 static int cs_dsp_adsp2v2_enable_core(struct cs_dsp
*dsp
)
2538 /* Wait for the RAM to start, should be near instantaneous */
2539 for (count
= 0; count
< 10; ++count
) {
2540 ret
= regmap_read(dsp
->regmap
, dsp
->base
+ ADSP2_STATUS1
, &val
);
2544 if (val
& ADSP2_RAM_RDY
)
2547 usleep_range(250, 500);
2550 if (!(val
& ADSP2_RAM_RDY
)) {
2551 cs_dsp_err(dsp
, "Failed to start DSP RAM\n");
2555 cs_dsp_dbg(dsp
, "RAM ready after %d polls\n", count
);
2560 static int cs_dsp_adsp2_enable_core(struct cs_dsp
*dsp
)
2564 ret
= regmap_update_bits_async(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2565 ADSP2_SYS_ENA
, ADSP2_SYS_ENA
);
2569 return cs_dsp_adsp2v2_enable_core(dsp
);
2572 static int cs_dsp_adsp2_lock(struct cs_dsp
*dsp
, unsigned int lock_regions
)
2574 struct regmap
*regmap
= dsp
->regmap
;
2575 unsigned int code0
, code1
, lock_reg
;
2577 if (!(lock_regions
& CS_ADSP2_REGION_ALL
))
2580 lock_regions
&= CS_ADSP2_REGION_ALL
;
2581 lock_reg
= dsp
->base
+ ADSP2_LOCK_REGION_1_LOCK_REGION_0
;
2583 while (lock_regions
) {
2585 if (lock_regions
& BIT(0)) {
2586 code0
= ADSP2_LOCK_CODE_0
;
2587 code1
= ADSP2_LOCK_CODE_1
;
2589 if (lock_regions
& BIT(1)) {
2590 code0
|= ADSP2_LOCK_CODE_0
<< ADSP2_LOCK_REGION_SHIFT
;
2591 code1
|= ADSP2_LOCK_CODE_1
<< ADSP2_LOCK_REGION_SHIFT
;
2593 regmap_write(regmap
, lock_reg
, code0
);
2594 regmap_write(regmap
, lock_reg
, code1
);
2602 static int cs_dsp_adsp2_enable_memory(struct cs_dsp
*dsp
)
2604 return regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2605 ADSP2_MEM_ENA
, ADSP2_MEM_ENA
);
2608 static void cs_dsp_adsp2_disable_memory(struct cs_dsp
*dsp
)
2610 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2614 static void cs_dsp_adsp2_disable_core(struct cs_dsp
*dsp
)
2616 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_RDMA_CONFIG_1
, 0);
2617 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_WDMA_CONFIG_1
, 0);
2618 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_WDMA_CONFIG_2
, 0);
2620 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2624 static void cs_dsp_adsp2v2_disable_core(struct cs_dsp
*dsp
)
2626 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_RDMA_CONFIG_1
, 0);
2627 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2_WDMA_CONFIG_1
, 0);
2628 regmap_write(dsp
->regmap
, dsp
->base
+ ADSP2V2_WDMA_CONFIG_2
, 0);
2631 static int cs_dsp_halo_configure_mpu(struct cs_dsp
*dsp
, unsigned int lock_regions
)
2633 struct reg_sequence config
[] = {
2634 { dsp
->base
+ HALO_MPU_LOCK_CONFIG
, 0x5555 },
2635 { dsp
->base
+ HALO_MPU_LOCK_CONFIG
, 0xAAAA },
2636 { dsp
->base
+ HALO_MPU_XMEM_ACCESS_0
, 0xFFFFFFFF },
2637 { dsp
->base
+ HALO_MPU_YMEM_ACCESS_0
, 0xFFFFFFFF },
2638 { dsp
->base
+ HALO_MPU_WINDOW_ACCESS_0
, lock_regions
},
2639 { dsp
->base
+ HALO_MPU_XREG_ACCESS_0
, lock_regions
},
2640 { dsp
->base
+ HALO_MPU_YREG_ACCESS_0
, lock_regions
},
2641 { dsp
->base
+ HALO_MPU_XMEM_ACCESS_1
, 0xFFFFFFFF },
2642 { dsp
->base
+ HALO_MPU_YMEM_ACCESS_1
, 0xFFFFFFFF },
2643 { dsp
->base
+ HALO_MPU_WINDOW_ACCESS_1
, lock_regions
},
2644 { dsp
->base
+ HALO_MPU_XREG_ACCESS_1
, lock_regions
},
2645 { dsp
->base
+ HALO_MPU_YREG_ACCESS_1
, lock_regions
},
2646 { dsp
->base
+ HALO_MPU_XMEM_ACCESS_2
, 0xFFFFFFFF },
2647 { dsp
->base
+ HALO_MPU_YMEM_ACCESS_2
, 0xFFFFFFFF },
2648 { dsp
->base
+ HALO_MPU_WINDOW_ACCESS_2
, lock_regions
},
2649 { dsp
->base
+ HALO_MPU_XREG_ACCESS_2
, lock_regions
},
2650 { dsp
->base
+ HALO_MPU_YREG_ACCESS_2
, lock_regions
},
2651 { dsp
->base
+ HALO_MPU_XMEM_ACCESS_3
, 0xFFFFFFFF },
2652 { dsp
->base
+ HALO_MPU_YMEM_ACCESS_3
, 0xFFFFFFFF },
2653 { dsp
->base
+ HALO_MPU_WINDOW_ACCESS_3
, lock_regions
},
2654 { dsp
->base
+ HALO_MPU_XREG_ACCESS_3
, lock_regions
},
2655 { dsp
->base
+ HALO_MPU_YREG_ACCESS_3
, lock_regions
},
2656 { dsp
->base
+ HALO_MPU_LOCK_CONFIG
, 0 },
2659 return regmap_multi_reg_write(dsp
->regmap
, config
, ARRAY_SIZE(config
));
2663 * cs_dsp_set_dspclk() - Applies the given frequency to the given cs_dsp
2664 * @dsp: pointer to DSP structure
2665 * @freq: clock rate to set
2667 * This is only for use on ADSP2 cores.
2669 * Return: Zero for success, a negative number on error.
2671 int cs_dsp_set_dspclk(struct cs_dsp
*dsp
, unsigned int freq
)
2675 ret
= regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CLOCKING
,
2677 freq
<< ADSP2_CLK_SEL_SHIFT
);
2679 cs_dsp_err(dsp
, "Failed to set clock rate: %d\n", ret
);
2683 EXPORT_SYMBOL_NS_GPL(cs_dsp_set_dspclk
, FW_CS_DSP
);
2685 static void cs_dsp_stop_watchdog(struct cs_dsp
*dsp
)
2687 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_WATCHDOG
,
2688 ADSP2_WDT_ENA_MASK
, 0);
2691 static void cs_dsp_halo_stop_watchdog(struct cs_dsp
*dsp
)
2693 regmap_update_bits(dsp
->regmap
, dsp
->base
+ HALO_WDT_CONTROL
,
2694 HALO_WDT_EN_MASK
, 0);
2698 * cs_dsp_power_up() - Downloads firmware to the DSP
2699 * @dsp: pointer to DSP structure
2700 * @wmfw_firmware: the firmware to be sent
2701 * @wmfw_filename: file name of firmware to be sent
2702 * @coeff_firmware: the coefficient data to be sent
2703 * @coeff_filename: file name of coefficient to data be sent
2704 * @fw_name: the user-friendly firmware name
2706 * This function is used on ADSP2 and Halo DSP cores, it powers-up the DSP core
2707 * and downloads the firmware but does not start the firmware running. The
2708 * cs_dsp booted flag will be set once completed and if the core has a low-power
2709 * memory retention mode it will be put into this state after the firmware is
2712 * Return: Zero for success, a negative number on error.
2714 int cs_dsp_power_up(struct cs_dsp
*dsp
,
2715 const struct firmware
*wmfw_firmware
, const char *wmfw_filename
,
2716 const struct firmware
*coeff_firmware
, const char *coeff_filename
,
2717 const char *fw_name
)
2721 mutex_lock(&dsp
->pwr_lock
);
2723 dsp
->fw_name
= fw_name
;
2725 if (dsp
->ops
->enable_memory
) {
2726 ret
= dsp
->ops
->enable_memory(dsp
);
2731 if (dsp
->ops
->enable_core
) {
2732 ret
= dsp
->ops
->enable_core(dsp
);
2737 ret
= cs_dsp_load(dsp
, wmfw_firmware
, wmfw_filename
);
2741 ret
= dsp
->ops
->setup_algs(dsp
);
2745 ret
= cs_dsp_load_coeff(dsp
, coeff_firmware
, coeff_filename
);
2749 /* Initialize caches for enabled and unset controls */
2750 ret
= cs_dsp_coeff_init_control_caches(dsp
);
2754 if (dsp
->ops
->disable_core
)
2755 dsp
->ops
->disable_core(dsp
);
2759 mutex_unlock(&dsp
->pwr_lock
);
2763 if (dsp
->ops
->disable_core
)
2764 dsp
->ops
->disable_core(dsp
);
2766 if (dsp
->ops
->disable_memory
)
2767 dsp
->ops
->disable_memory(dsp
);
2769 mutex_unlock(&dsp
->pwr_lock
);
2773 EXPORT_SYMBOL_NS_GPL(cs_dsp_power_up
, FW_CS_DSP
);
2776 * cs_dsp_power_down() - Powers-down the DSP
2777 * @dsp: pointer to DSP structure
2779 * cs_dsp_stop() must have been called before this function. The core will be
2780 * fully powered down and so the memory will not be retained.
2782 void cs_dsp_power_down(struct cs_dsp
*dsp
)
2784 struct cs_dsp_coeff_ctl
*ctl
;
2786 mutex_lock(&dsp
->pwr_lock
);
2788 cs_dsp_debugfs_clear(dsp
);
2791 dsp
->fw_id_version
= 0;
2793 dsp
->booted
= false;
2795 if (dsp
->ops
->disable_memory
)
2796 dsp
->ops
->disable_memory(dsp
);
2798 list_for_each_entry(ctl
, &dsp
->ctl_list
, list
)
2801 cs_dsp_free_alg_regions(dsp
);
2803 mutex_unlock(&dsp
->pwr_lock
);
2805 cs_dsp_dbg(dsp
, "Shutdown complete\n");
2807 EXPORT_SYMBOL_NS_GPL(cs_dsp_power_down
, FW_CS_DSP
);
2809 static int cs_dsp_adsp2_start_core(struct cs_dsp
*dsp
)
2811 return regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2812 ADSP2_CORE_ENA
| ADSP2_START
,
2813 ADSP2_CORE_ENA
| ADSP2_START
);
2816 static void cs_dsp_adsp2_stop_core(struct cs_dsp
*dsp
)
2818 regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2819 ADSP2_CORE_ENA
| ADSP2_START
, 0);
2823 * cs_dsp_run() - Starts the firmware running
2824 * @dsp: pointer to DSP structure
2826 * cs_dsp_power_up() must have previously been called successfully.
2828 * Return: Zero for success, a negative number on error.
2830 int cs_dsp_run(struct cs_dsp
*dsp
)
2834 mutex_lock(&dsp
->pwr_lock
);
2841 if (dsp
->ops
->enable_core
) {
2842 ret
= dsp
->ops
->enable_core(dsp
);
2847 if (dsp
->client_ops
->pre_run
) {
2848 ret
= dsp
->client_ops
->pre_run(dsp
);
2853 /* Sync set controls */
2854 ret
= cs_dsp_coeff_sync_controls(dsp
);
2858 if (dsp
->ops
->lock_memory
) {
2859 ret
= dsp
->ops
->lock_memory(dsp
, dsp
->lock_regions
);
2861 cs_dsp_err(dsp
, "Error configuring MPU: %d\n", ret
);
2866 if (dsp
->ops
->start_core
) {
2867 ret
= dsp
->ops
->start_core(dsp
);
2872 dsp
->running
= true;
2874 if (dsp
->client_ops
->post_run
) {
2875 ret
= dsp
->client_ops
->post_run(dsp
);
2880 mutex_unlock(&dsp
->pwr_lock
);
2885 if (dsp
->ops
->stop_core
)
2886 dsp
->ops
->stop_core(dsp
);
2887 if (dsp
->ops
->disable_core
)
2888 dsp
->ops
->disable_core(dsp
);
2889 mutex_unlock(&dsp
->pwr_lock
);
2893 EXPORT_SYMBOL_NS_GPL(cs_dsp_run
, FW_CS_DSP
);
2896 * cs_dsp_stop() - Stops the firmware
2897 * @dsp: pointer to DSP structure
2899 * Memory will not be disabled so firmware will remain loaded.
2901 void cs_dsp_stop(struct cs_dsp
*dsp
)
2903 /* Tell the firmware to cleanup */
2904 cs_dsp_signal_event_controls(dsp
, CS_DSP_FW_EVENT_SHUTDOWN
);
2906 if (dsp
->ops
->stop_watchdog
)
2907 dsp
->ops
->stop_watchdog(dsp
);
2909 /* Log firmware state, it can be useful for analysis */
2910 if (dsp
->ops
->show_fw_status
)
2911 dsp
->ops
->show_fw_status(dsp
);
2913 mutex_lock(&dsp
->pwr_lock
);
2915 if (dsp
->client_ops
->pre_stop
)
2916 dsp
->client_ops
->pre_stop(dsp
);
2918 dsp
->running
= false;
2920 if (dsp
->ops
->stop_core
)
2921 dsp
->ops
->stop_core(dsp
);
2922 if (dsp
->ops
->disable_core
)
2923 dsp
->ops
->disable_core(dsp
);
2925 if (dsp
->client_ops
->post_stop
)
2926 dsp
->client_ops
->post_stop(dsp
);
2928 mutex_unlock(&dsp
->pwr_lock
);
2930 cs_dsp_dbg(dsp
, "Execution stopped\n");
2932 EXPORT_SYMBOL_NS_GPL(cs_dsp_stop
, FW_CS_DSP
);
2934 static int cs_dsp_halo_start_core(struct cs_dsp
*dsp
)
2938 ret
= regmap_update_bits(dsp
->regmap
, dsp
->base
+ HALO_CCM_CORE_CONTROL
,
2939 HALO_CORE_RESET
| HALO_CORE_EN
,
2940 HALO_CORE_RESET
| HALO_CORE_EN
);
2944 return regmap_update_bits(dsp
->regmap
, dsp
->base
+ HALO_CCM_CORE_CONTROL
,
2945 HALO_CORE_RESET
, 0);
2948 static void cs_dsp_halo_stop_core(struct cs_dsp
*dsp
)
2950 regmap_update_bits(dsp
->regmap
, dsp
->base
+ HALO_CCM_CORE_CONTROL
,
2953 /* reset halo core with CORE_SOFT_RESET */
2954 regmap_update_bits(dsp
->regmap
, dsp
->base
+ HALO_CORE_SOFT_RESET
,
2955 HALO_CORE_SOFT_RESET_MASK
, 1);
2959 * cs_dsp_adsp2_init() - Initialise a cs_dsp structure representing a ADSP2 core
2960 * @dsp: pointer to DSP structure
2962 * Return: Zero for success, a negative number on error.
2964 int cs_dsp_adsp2_init(struct cs_dsp
*dsp
)
2971 * Disable the DSP memory by default when in reset for a small
2974 ret
= regmap_update_bits(dsp
->regmap
, dsp
->base
+ ADSP2_CONTROL
,
2978 "Failed to clear memory retention: %d\n", ret
);
2982 dsp
->ops
= &cs_dsp_adsp2_ops
[0];
2985 dsp
->ops
= &cs_dsp_adsp2_ops
[1];
2988 dsp
->ops
= &cs_dsp_adsp2_ops
[2];
2992 return cs_dsp_common_init(dsp
);
2994 EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp2_init
, FW_CS_DSP
);
2997 * cs_dsp_halo_init() - Initialise a cs_dsp structure representing a HALO Core DSP
2998 * @dsp: pointer to DSP structure
3000 * Return: Zero for success, a negative number on error.
3002 int cs_dsp_halo_init(struct cs_dsp
*dsp
)
3004 if (dsp
->no_core_startstop
)
3005 dsp
->ops
= &cs_dsp_halo_ao_ops
;
3007 dsp
->ops
= &cs_dsp_halo_ops
;
3009 return cs_dsp_common_init(dsp
);
3011 EXPORT_SYMBOL_NS_GPL(cs_dsp_halo_init
, FW_CS_DSP
);
3014 * cs_dsp_remove() - Clean a cs_dsp before deletion
3015 * @dsp: pointer to DSP structure
3017 void cs_dsp_remove(struct cs_dsp
*dsp
)
3019 struct cs_dsp_coeff_ctl
*ctl
;
3021 while (!list_empty(&dsp
->ctl_list
)) {
3022 ctl
= list_first_entry(&dsp
->ctl_list
, struct cs_dsp_coeff_ctl
, list
);
3024 if (dsp
->client_ops
->control_remove
)
3025 dsp
->client_ops
->control_remove(ctl
);
3027 list_del(&ctl
->list
);
3028 cs_dsp_free_ctl_blk(ctl
);
3031 EXPORT_SYMBOL_NS_GPL(cs_dsp_remove
, FW_CS_DSP
);
3034 * cs_dsp_read_raw_data_block() - Reads a block of data from DSP memory
3035 * @dsp: pointer to DSP structure
3036 * @mem_type: the type of DSP memory containing the data to be read
3037 * @mem_addr: the address of the data within the memory region
3038 * @num_words: the length of the data to read
3039 * @data: a buffer to store the fetched data
3041 * If this is used to read unpacked 24-bit memory, each 24-bit DSP word will
3042 * occupy 32-bits in data (MSbyte will be 0). This padding can be removed using
3043 * cs_dsp_remove_padding()
3045 * Return: Zero for success, a negative number on error.
3047 int cs_dsp_read_raw_data_block(struct cs_dsp
*dsp
, int mem_type
, unsigned int mem_addr
,
3048 unsigned int num_words
, __be32
*data
)
3050 struct cs_dsp_region
const *mem
= cs_dsp_find_region(dsp
, mem_type
);
3054 lockdep_assert_held(&dsp
->pwr_lock
);
3059 reg
= dsp
->ops
->region_to_reg(mem
, mem_addr
);
3061 ret
= regmap_raw_read(dsp
->regmap
, reg
, data
,
3062 sizeof(*data
) * num_words
);
3068 EXPORT_SYMBOL_NS_GPL(cs_dsp_read_raw_data_block
, FW_CS_DSP
);
3071 * cs_dsp_read_data_word() - Reads a word from DSP memory
3072 * @dsp: pointer to DSP structure
3073 * @mem_type: the type of DSP memory containing the data to be read
3074 * @mem_addr: the address of the data within the memory region
3075 * @data: a buffer to store the fetched data
3077 * Return: Zero for success, a negative number on error.
3079 int cs_dsp_read_data_word(struct cs_dsp
*dsp
, int mem_type
, unsigned int mem_addr
, u32
*data
)
3084 ret
= cs_dsp_read_raw_data_block(dsp
, mem_type
, mem_addr
, 1, &raw
);
3088 *data
= be32_to_cpu(raw
) & 0x00ffffffu
;
3092 EXPORT_SYMBOL_NS_GPL(cs_dsp_read_data_word
, FW_CS_DSP
);
3095 * cs_dsp_write_data_word() - Writes a word to DSP memory
3096 * @dsp: pointer to DSP structure
3097 * @mem_type: the type of DSP memory containing the data to be written
3098 * @mem_addr: the address of the data within the memory region
3099 * @data: the data to be written
3101 * Return: Zero for success, a negative number on error.
3103 int cs_dsp_write_data_word(struct cs_dsp
*dsp
, int mem_type
, unsigned int mem_addr
, u32 data
)
3105 struct cs_dsp_region
const *mem
= cs_dsp_find_region(dsp
, mem_type
);
3106 __be32 val
= cpu_to_be32(data
& 0x00ffffffu
);
3109 lockdep_assert_held(&dsp
->pwr_lock
);
3114 reg
= dsp
->ops
->region_to_reg(mem
, mem_addr
);
3116 return regmap_raw_write(dsp
->regmap
, reg
, &val
, sizeof(val
));
3118 EXPORT_SYMBOL_NS_GPL(cs_dsp_write_data_word
, FW_CS_DSP
);
3121 * cs_dsp_remove_padding() - Convert unpacked words to packed bytes
3122 * @buf: buffer containing DSP words read from DSP memory
3123 * @nwords: number of words to convert
3125 * DSP words from the register map have pad bytes and the data bytes
3126 * are in swapped order. This swaps to the native endian order and
3127 * strips the pad bytes.
3129 void cs_dsp_remove_padding(u32
*buf
, int nwords
)
3131 const __be32
*pack_in
= (__be32
*)buf
;
3132 u8
*pack_out
= (u8
*)buf
;
3135 for (i
= 0; i
< nwords
; i
++) {
3136 u32 word
= be32_to_cpu(*pack_in
++);
3137 *pack_out
++ = (u8
)word
;
3138 *pack_out
++ = (u8
)(word
>> 8);
3139 *pack_out
++ = (u8
)(word
>> 16);
3142 EXPORT_SYMBOL_NS_GPL(cs_dsp_remove_padding
, FW_CS_DSP
);
3145 * cs_dsp_adsp2_bus_error() - Handle a DSP bus error interrupt
3146 * @dsp: pointer to DSP structure
3148 * The firmware and DSP state will be logged for future analysis.
3150 void cs_dsp_adsp2_bus_error(struct cs_dsp
*dsp
)
3153 struct regmap
*regmap
= dsp
->regmap
;
3156 mutex_lock(&dsp
->pwr_lock
);
3158 ret
= regmap_read(regmap
, dsp
->base
+ ADSP2_LOCK_REGION_CTRL
, &val
);
3161 "Failed to read Region Lock Ctrl register: %d\n", ret
);
3165 if (val
& ADSP2_WDT_TIMEOUT_STS_MASK
) {
3166 cs_dsp_err(dsp
, "watchdog timeout error\n");
3167 dsp
->ops
->stop_watchdog(dsp
);
3168 if (dsp
->client_ops
->watchdog_expired
)
3169 dsp
->client_ops
->watchdog_expired(dsp
);
3172 if (val
& (ADSP2_ADDR_ERR_MASK
| ADSP2_REGION_LOCK_ERR_MASK
)) {
3173 if (val
& ADSP2_ADDR_ERR_MASK
)
3174 cs_dsp_err(dsp
, "bus error: address error\n");
3176 cs_dsp_err(dsp
, "bus error: region lock error\n");
3178 ret
= regmap_read(regmap
, dsp
->base
+ ADSP2_BUS_ERR_ADDR
, &val
);
3181 "Failed to read Bus Err Addr register: %d\n",
3186 cs_dsp_err(dsp
, "bus error address = 0x%x\n",
3187 val
& ADSP2_BUS_ERR_ADDR_MASK
);
3189 ret
= regmap_read(regmap
,
3190 dsp
->base
+ ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR
,
3194 "Failed to read Pmem Xmem Err Addr register: %d\n",
3199 cs_dsp_err(dsp
, "xmem error address = 0x%x\n",
3200 val
& ADSP2_XMEM_ERR_ADDR_MASK
);
3201 cs_dsp_err(dsp
, "pmem error address = 0x%x\n",
3202 (val
& ADSP2_PMEM_ERR_ADDR_MASK
) >>
3203 ADSP2_PMEM_ERR_ADDR_SHIFT
);
3206 regmap_update_bits(regmap
, dsp
->base
+ ADSP2_LOCK_REGION_CTRL
,
3207 ADSP2_CTRL_ERR_EINT
, ADSP2_CTRL_ERR_EINT
);
3210 mutex_unlock(&dsp
->pwr_lock
);
3212 EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp2_bus_error
, FW_CS_DSP
);
3215 * cs_dsp_halo_bus_error() - Handle a DSP bus error interrupt
3216 * @dsp: pointer to DSP structure
3218 * The firmware and DSP state will be logged for future analysis.
3220 void cs_dsp_halo_bus_error(struct cs_dsp
*dsp
)
3222 struct regmap
*regmap
= dsp
->regmap
;
3223 unsigned int fault
[6];
3224 struct reg_sequence clear
[] = {
3225 { dsp
->base
+ HALO_MPU_XM_VIO_STATUS
, 0x0 },
3226 { dsp
->base
+ HALO_MPU_YM_VIO_STATUS
, 0x0 },
3227 { dsp
->base
+ HALO_MPU_PM_VIO_STATUS
, 0x0 },
3231 mutex_lock(&dsp
->pwr_lock
);
3233 ret
= regmap_read(regmap
, dsp
->base_sysinfo
+ HALO_AHBM_WINDOW_DEBUG_1
,
3236 cs_dsp_warn(dsp
, "Failed to read AHB DEBUG_1: %d\n", ret
);
3240 cs_dsp_warn(dsp
, "AHB: STATUS: 0x%x ADDR: 0x%x\n",
3241 *fault
& HALO_AHBM_FLAGS_ERR_MASK
,
3242 (*fault
& HALO_AHBM_CORE_ERR_ADDR_MASK
) >>
3243 HALO_AHBM_CORE_ERR_ADDR_SHIFT
);
3245 ret
= regmap_read(regmap
, dsp
->base_sysinfo
+ HALO_AHBM_WINDOW_DEBUG_0
,
3248 cs_dsp_warn(dsp
, "Failed to read AHB DEBUG_0: %d\n", ret
);
3252 cs_dsp_warn(dsp
, "AHB: SYS_ADDR: 0x%x\n", *fault
);
3254 ret
= regmap_bulk_read(regmap
, dsp
->base
+ HALO_MPU_XM_VIO_ADDR
,
3255 fault
, ARRAY_SIZE(fault
));
3257 cs_dsp_warn(dsp
, "Failed to read MPU fault info: %d\n", ret
);
3261 cs_dsp_warn(dsp
, "XM: STATUS:0x%x ADDR:0x%x\n", fault
[1], fault
[0]);
3262 cs_dsp_warn(dsp
, "YM: STATUS:0x%x ADDR:0x%x\n", fault
[3], fault
[2]);
3263 cs_dsp_warn(dsp
, "PM: STATUS:0x%x ADDR:0x%x\n", fault
[5], fault
[4]);
3265 ret
= regmap_multi_reg_write(dsp
->regmap
, clear
, ARRAY_SIZE(clear
));
3267 cs_dsp_warn(dsp
, "Failed to clear MPU status: %d\n", ret
);
3270 mutex_unlock(&dsp
->pwr_lock
);
3272 EXPORT_SYMBOL_NS_GPL(cs_dsp_halo_bus_error
, FW_CS_DSP
);
3275 * cs_dsp_halo_wdt_expire() - Handle DSP watchdog expiry
3276 * @dsp: pointer to DSP structure
3278 * This is logged for future analysis.
3280 void cs_dsp_halo_wdt_expire(struct cs_dsp
*dsp
)
3282 mutex_lock(&dsp
->pwr_lock
);
3284 cs_dsp_warn(dsp
, "WDT Expiry Fault\n");
3286 dsp
->ops
->stop_watchdog(dsp
);
3287 if (dsp
->client_ops
->watchdog_expired
)
3288 dsp
->client_ops
->watchdog_expired(dsp
);
3290 mutex_unlock(&dsp
->pwr_lock
);
3292 EXPORT_SYMBOL_NS_GPL(cs_dsp_halo_wdt_expire
, FW_CS_DSP
);
3294 static const struct cs_dsp_ops cs_dsp_adsp1_ops
= {
3295 .validate_version
= cs_dsp_validate_version
,
3296 .parse_sizes
= cs_dsp_adsp1_parse_sizes
,
3297 .region_to_reg
= cs_dsp_region_to_reg
,
3300 static const struct cs_dsp_ops cs_dsp_adsp2_ops
[] = {
3302 .parse_sizes
= cs_dsp_adsp2_parse_sizes
,
3303 .validate_version
= cs_dsp_validate_version
,
3304 .setup_algs
= cs_dsp_adsp2_setup_algs
,
3305 .region_to_reg
= cs_dsp_region_to_reg
,
3307 .show_fw_status
= cs_dsp_adsp2_show_fw_status
,
3309 .enable_memory
= cs_dsp_adsp2_enable_memory
,
3310 .disable_memory
= cs_dsp_adsp2_disable_memory
,
3312 .enable_core
= cs_dsp_adsp2_enable_core
,
3313 .disable_core
= cs_dsp_adsp2_disable_core
,
3315 .start_core
= cs_dsp_adsp2_start_core
,
3316 .stop_core
= cs_dsp_adsp2_stop_core
,
3320 .parse_sizes
= cs_dsp_adsp2_parse_sizes
,
3321 .validate_version
= cs_dsp_validate_version
,
3322 .setup_algs
= cs_dsp_adsp2_setup_algs
,
3323 .region_to_reg
= cs_dsp_region_to_reg
,
3325 .show_fw_status
= cs_dsp_adsp2v2_show_fw_status
,
3327 .enable_memory
= cs_dsp_adsp2_enable_memory
,
3328 .disable_memory
= cs_dsp_adsp2_disable_memory
,
3329 .lock_memory
= cs_dsp_adsp2_lock
,
3331 .enable_core
= cs_dsp_adsp2v2_enable_core
,
3332 .disable_core
= cs_dsp_adsp2v2_disable_core
,
3334 .start_core
= cs_dsp_adsp2_start_core
,
3335 .stop_core
= cs_dsp_adsp2_stop_core
,
3338 .parse_sizes
= cs_dsp_adsp2_parse_sizes
,
3339 .validate_version
= cs_dsp_validate_version
,
3340 .setup_algs
= cs_dsp_adsp2_setup_algs
,
3341 .region_to_reg
= cs_dsp_region_to_reg
,
3343 .show_fw_status
= cs_dsp_adsp2v2_show_fw_status
,
3344 .stop_watchdog
= cs_dsp_stop_watchdog
,
3346 .enable_memory
= cs_dsp_adsp2_enable_memory
,
3347 .disable_memory
= cs_dsp_adsp2_disable_memory
,
3348 .lock_memory
= cs_dsp_adsp2_lock
,
3350 .enable_core
= cs_dsp_adsp2v2_enable_core
,
3351 .disable_core
= cs_dsp_adsp2v2_disable_core
,
3353 .start_core
= cs_dsp_adsp2_start_core
,
3354 .stop_core
= cs_dsp_adsp2_stop_core
,
3358 static const struct cs_dsp_ops cs_dsp_halo_ops
= {
3359 .parse_sizes
= cs_dsp_adsp2_parse_sizes
,
3360 .validate_version
= cs_dsp_halo_validate_version
,
3361 .setup_algs
= cs_dsp_halo_setup_algs
,
3362 .region_to_reg
= cs_dsp_halo_region_to_reg
,
3364 .show_fw_status
= cs_dsp_halo_show_fw_status
,
3365 .stop_watchdog
= cs_dsp_halo_stop_watchdog
,
3367 .lock_memory
= cs_dsp_halo_configure_mpu
,
3369 .start_core
= cs_dsp_halo_start_core
,
3370 .stop_core
= cs_dsp_halo_stop_core
,
3373 static const struct cs_dsp_ops cs_dsp_halo_ao_ops
= {
3374 .parse_sizes
= cs_dsp_adsp2_parse_sizes
,
3375 .validate_version
= cs_dsp_halo_validate_version
,
3376 .setup_algs
= cs_dsp_halo_setup_algs
,
3377 .region_to_reg
= cs_dsp_halo_region_to_reg
,
3378 .show_fw_status
= cs_dsp_halo_show_fw_status
,
3382 * cs_dsp_chunk_write() - Format data to a DSP memory chunk
3383 * @ch: Pointer to the chunk structure
3384 * @nbits: Number of bits to write
3385 * @val: Value to write
3387 * This function sequentially writes values into the format required for DSP
3388 * memory, it handles both inserting of the padding bytes and converting to
3389 * big endian. Note that data is only committed to the chunk when a whole DSP
3390 * words worth of data is available.
3392 * Return: Zero for success, a negative number on error.
3394 int cs_dsp_chunk_write(struct cs_dsp_chunk
*ch
, int nbits
, u32 val
)
3398 nwrite
= min(CS_DSP_DATA_WORD_BITS
- ch
->cachebits
, nbits
);
3400 ch
->cache
<<= nwrite
;
3401 ch
->cache
|= val
>> (nbits
- nwrite
);
3402 ch
->cachebits
+= nwrite
;
3405 if (ch
->cachebits
== CS_DSP_DATA_WORD_BITS
) {
3406 if (cs_dsp_chunk_end(ch
))
3409 ch
->cache
&= 0xFFFFFF;
3410 for (i
= 0; i
< sizeof(ch
->cache
); i
++, ch
->cache
<<= BITS_PER_BYTE
)
3411 *ch
->data
++ = (ch
->cache
& 0xFF000000) >> CS_DSP_DATA_WORD_BITS
;
3413 ch
->bytes
+= sizeof(ch
->cache
);
3418 return cs_dsp_chunk_write(ch
, nbits
, val
);
3422 EXPORT_SYMBOL_NS_GPL(cs_dsp_chunk_write
, FW_CS_DSP
);
3425 * cs_dsp_chunk_flush() - Pad remaining data with zero and commit to chunk
3426 * @ch: Pointer to the chunk structure
3428 * As cs_dsp_chunk_write only writes data when a whole DSP word is ready to
3429 * be written out it is possible that some data will remain in the cache, this
3430 * function will pad that data with zeros upto a whole DSP word and write out.
3432 * Return: Zero for success, a negative number on error.
3434 int cs_dsp_chunk_flush(struct cs_dsp_chunk
*ch
)
3439 return cs_dsp_chunk_write(ch
, CS_DSP_DATA_WORD_BITS
- ch
->cachebits
, 0);
3441 EXPORT_SYMBOL_NS_GPL(cs_dsp_chunk_flush
, FW_CS_DSP
);
3444 * cs_dsp_chunk_read() - Parse data from a DSP memory chunk
3445 * @ch: Pointer to the chunk structure
3446 * @nbits: Number of bits to read
3448 * This function sequentially reads values from a DSP memory formatted buffer,
3449 * it handles both removing of the padding bytes and converting from big endian.
3451 * Return: A negative number is returned on error, otherwise the read value.
3453 int cs_dsp_chunk_read(struct cs_dsp_chunk
*ch
, int nbits
)
3458 if (!ch
->cachebits
) {
3459 if (cs_dsp_chunk_end(ch
))
3463 ch
->cachebits
= CS_DSP_DATA_WORD_BITS
;
3465 for (i
= 0; i
< sizeof(ch
->cache
); i
++, ch
->cache
<<= BITS_PER_BYTE
)
3466 ch
->cache
|= *ch
->data
++;
3468 ch
->bytes
+= sizeof(ch
->cache
);
3471 nread
= min(ch
->cachebits
, nbits
);
3474 result
= ch
->cache
>> ((sizeof(ch
->cache
) * BITS_PER_BYTE
) - nread
);
3475 ch
->cache
<<= nread
;
3476 ch
->cachebits
-= nread
;
3479 result
= (result
<< nbits
) | cs_dsp_chunk_read(ch
, nbits
);
3483 EXPORT_SYMBOL_NS_GPL(cs_dsp_chunk_read
, FW_CS_DSP
);
3486 struct cs_dsp_wseq_op
{
3487 struct list_head list
;
3494 static void cs_dsp_wseq_clear(struct cs_dsp
*dsp
, struct cs_dsp_wseq
*wseq
)
3496 struct cs_dsp_wseq_op
*op
, *op_tmp
;
3498 list_for_each_entry_safe(op
, op_tmp
, &wseq
->ops
, list
) {
3499 list_del(&op
->list
);
3500 devm_kfree(dsp
->dev
, op
);
3504 static int cs_dsp_populate_wseq(struct cs_dsp
*dsp
, struct cs_dsp_wseq
*wseq
)
3506 struct cs_dsp_wseq_op
*op
= NULL
;
3507 struct cs_dsp_chunk chunk
;
3512 cs_dsp_err(dsp
, "No control for write sequence\n");
3516 words
= kzalloc(wseq
->ctl
->len
, GFP_KERNEL
);
3520 ret
= cs_dsp_coeff_read_ctrl(wseq
->ctl
, 0, words
, wseq
->ctl
->len
);
3522 cs_dsp_err(dsp
, "Failed to read %s: %d\n", wseq
->ctl
->subname
, ret
);
3526 INIT_LIST_HEAD(&wseq
->ops
);
3528 chunk
= cs_dsp_chunk(words
, wseq
->ctl
->len
);
3530 while (!cs_dsp_chunk_end(&chunk
)) {
3531 op
= devm_kzalloc(dsp
->dev
, sizeof(*op
), GFP_KERNEL
);
3537 op
->offset
= cs_dsp_chunk_bytes(&chunk
);
3538 op
->operation
= cs_dsp_chunk_read(&chunk
, 8);
3540 switch (op
->operation
) {
3541 case CS_DSP_WSEQ_END
:
3542 op
->data
= WSEQ_END_OF_SCRIPT
;
3544 case CS_DSP_WSEQ_UNLOCK
:
3545 op
->data
= cs_dsp_chunk_read(&chunk
, 16);
3547 case CS_DSP_WSEQ_ADDR8
:
3548 op
->address
= cs_dsp_chunk_read(&chunk
, 8);
3549 op
->data
= cs_dsp_chunk_read(&chunk
, 32);
3551 case CS_DSP_WSEQ_H16
:
3552 case CS_DSP_WSEQ_L16
:
3553 op
->address
= cs_dsp_chunk_read(&chunk
, 24);
3554 op
->data
= cs_dsp_chunk_read(&chunk
, 16);
3556 case CS_DSP_WSEQ_FULL
:
3557 op
->address
= cs_dsp_chunk_read(&chunk
, 32);
3558 op
->data
= cs_dsp_chunk_read(&chunk
, 32);
3562 cs_dsp_err(dsp
, "Unsupported op: %X\n", op
->operation
);
3563 devm_kfree(dsp
->dev
, op
);
3567 list_add_tail(&op
->list
, &wseq
->ops
);
3569 if (op
->operation
== CS_DSP_WSEQ_END
)
3573 if (op
&& op
->operation
!= CS_DSP_WSEQ_END
) {
3574 cs_dsp_err(dsp
, "%s missing end terminator\n", wseq
->ctl
->subname
);
3585 * cs_dsp_wseq_init() - Initialize write sequences contained within the loaded DSP firmware
3586 * @dsp: Pointer to DSP structure
3587 * @wseqs: List of write sequences to initialize
3588 * @num_wseqs: Number of write sequences to initialize
3590 * Return: Zero for success, a negative number on error.
3592 int cs_dsp_wseq_init(struct cs_dsp
*dsp
, struct cs_dsp_wseq
*wseqs
, unsigned int num_wseqs
)
3596 lockdep_assert_held(&dsp
->pwr_lock
);
3598 for (i
= 0; i
< num_wseqs
; i
++) {
3599 ret
= cs_dsp_populate_wseq(dsp
, &wseqs
[i
]);
3601 cs_dsp_wseq_clear(dsp
, &wseqs
[i
]);
3608 EXPORT_SYMBOL_NS_GPL(cs_dsp_wseq_init
, FW_CS_DSP
);
3610 static struct cs_dsp_wseq_op
*cs_dsp_wseq_find_op(u32 addr
, u8 op_code
,
3611 struct list_head
*wseq_ops
)
3613 struct cs_dsp_wseq_op
*op
;
3615 list_for_each_entry(op
, wseq_ops
, list
) {
3616 if (op
->operation
== op_code
&& op
->address
== addr
)
3624 * cs_dsp_wseq_write() - Add or update an entry in a write sequence
3625 * @dsp: Pointer to a DSP structure
3626 * @wseq: Write sequence to write to
3627 * @addr: Address of the register to be written to
3628 * @data: Data to be written
3629 * @op_code: The type of operation of the new entry
3630 * @update: If true, searches for the first entry in the write sequence with
3631 * the same address and op_code, and replaces it. If false, creates a new entry
3634 * This function formats register address and value pairs into the format
3635 * required for write sequence entries, and either updates or adds the
3636 * new entry into the write sequence.
3638 * If update is set to true and no matching entry is found, it will add a new entry.
3640 * Return: Zero for success, a negative number on error.
3642 int cs_dsp_wseq_write(struct cs_dsp
*dsp
, struct cs_dsp_wseq
*wseq
,
3643 u32 addr
, u32 data
, u8 op_code
, bool update
)
3645 struct cs_dsp_wseq_op
*op_end
, *op_new
= NULL
;
3646 u32 words
[WSEQ_OP_MAX_WORDS
];
3647 struct cs_dsp_chunk chunk
;
3648 int new_op_size
, ret
;
3651 op_new
= cs_dsp_wseq_find_op(addr
, op_code
, &wseq
->ops
);
3653 /* If entry to update is not found, treat it as a new operation */
3655 op_end
= cs_dsp_wseq_find_op(0, CS_DSP_WSEQ_END
, &wseq
->ops
);
3657 cs_dsp_err(dsp
, "Missing terminator for %s\n", wseq
->ctl
->subname
);
3661 op_new
= devm_kzalloc(dsp
->dev
, sizeof(*op_new
), GFP_KERNEL
);
3665 op_new
->operation
= op_code
;
3666 op_new
->address
= addr
;
3667 op_new
->offset
= op_end
->offset
;
3671 op_new
->data
= data
;
3673 chunk
= cs_dsp_chunk(words
, sizeof(words
));
3674 cs_dsp_chunk_write(&chunk
, 8, op_new
->operation
);
3677 case CS_DSP_WSEQ_FULL
:
3678 cs_dsp_chunk_write(&chunk
, 32, op_new
->address
);
3679 cs_dsp_chunk_write(&chunk
, 32, op_new
->data
);
3681 case CS_DSP_WSEQ_L16
:
3682 case CS_DSP_WSEQ_H16
:
3683 cs_dsp_chunk_write(&chunk
, 24, op_new
->address
);
3684 cs_dsp_chunk_write(&chunk
, 16, op_new
->data
);
3688 cs_dsp_err(dsp
, "Operation %X not supported\n", op_code
);
3692 new_op_size
= cs_dsp_chunk_bytes(&chunk
);
3695 if (wseq
->ctl
->len
- op_end
->offset
< new_op_size
) {
3696 cs_dsp_err(dsp
, "Not enough memory in %s for entry\n", wseq
->ctl
->subname
);
3701 op_end
->offset
+= new_op_size
;
3703 ret
= cs_dsp_coeff_write_ctrl(wseq
->ctl
, op_end
->offset
/ sizeof(u32
),
3704 &op_end
->data
, sizeof(u32
));
3708 list_add_tail(&op_new
->list
, &op_end
->list
);
3711 ret
= cs_dsp_coeff_write_ctrl(wseq
->ctl
, op_new
->offset
/ sizeof(u32
),
3712 words
, new_op_size
);
3719 devm_kfree(dsp
->dev
, op_new
);
3723 EXPORT_SYMBOL_NS_GPL(cs_dsp_wseq_write
, FW_CS_DSP
);
3726 * cs_dsp_wseq_multi_write() - Add or update multiple entries in a write sequence
3727 * @dsp: Pointer to a DSP structure
3728 * @wseq: Write sequence to write to
3729 * @reg_seq: List of address-data pairs
3730 * @num_regs: Number of address-data pairs
3731 * @op_code: The types of operations of the new entries
3732 * @update: If true, searches for the first entry in the write sequence with
3733 * the same address and op_code, and replaces it. If false, creates a new entry
3736 * This function calls cs_dsp_wseq_write() for multiple address-data pairs.
3738 * Return: Zero for success, a negative number on error.
3740 int cs_dsp_wseq_multi_write(struct cs_dsp
*dsp
, struct cs_dsp_wseq
*wseq
,
3741 const struct reg_sequence
*reg_seq
, int num_regs
,
3742 u8 op_code
, bool update
)
3746 for (i
= 0; i
< num_regs
; i
++) {
3747 ret
= cs_dsp_wseq_write(dsp
, wseq
, reg_seq
[i
].reg
,
3748 reg_seq
[i
].def
, op_code
, update
);
3755 EXPORT_SYMBOL_NS_GPL(cs_dsp_wseq_multi_write
, FW_CS_DSP
);
3757 MODULE_DESCRIPTION("Cirrus Logic DSP Support");
3758 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
3759 MODULE_LICENSE("GPL v2");