xref: /linux/drivers/firmware/cirrus/cs_dsp.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1f6bc909eSSimon Trimmer // SPDX-License-Identifier: GPL-2.0-only
2f6bc909eSSimon Trimmer /*
3f6bc909eSSimon Trimmer  * cs_dsp.c  --  Cirrus Logic DSP firmware support
4f6bc909eSSimon Trimmer  *
5f6bc909eSSimon Trimmer  * Based on sound/soc/codecs/wm_adsp.c
6f6bc909eSSimon Trimmer  *
7f6bc909eSSimon Trimmer  * Copyright 2012 Wolfson Microelectronics plc
8f6bc909eSSimon Trimmer  * Copyright (C) 2015-2021 Cirrus Logic, Inc. and
9f6bc909eSSimon Trimmer  *                         Cirrus Logic International Semiconductor Ltd.
10f6bc909eSSimon Trimmer  */
11f6bc909eSSimon Trimmer 
12f6bc909eSSimon Trimmer #include <linux/ctype.h>
13f6bc909eSSimon Trimmer #include <linux/debugfs.h>
14f6bc909eSSimon Trimmer #include <linux/delay.h>
15bff92858SRichard Fitzgerald #include <linux/minmax.h>
16f6bc909eSSimon Trimmer #include <linux/module.h>
17f6bc909eSSimon Trimmer #include <linux/moduleparam.h>
187a3f924cSSimon Trimmer #include <linux/seq_file.h>
19f6bc909eSSimon Trimmer #include <linux/slab.h>
20f6bc909eSSimon Trimmer #include <linux/vmalloc.h>
21f6bc909eSSimon Trimmer 
22f6bc909eSSimon Trimmer #include <linux/firmware/cirrus/cs_dsp.h>
23f6bc909eSSimon Trimmer #include <linux/firmware/cirrus/wmfw.h>
24f6bc909eSSimon Trimmer 
25f6bc909eSSimon Trimmer #define cs_dsp_err(_dsp, fmt, ...) \
26f6bc909eSSimon Trimmer 	dev_err(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
27f6bc909eSSimon Trimmer #define cs_dsp_warn(_dsp, fmt, ...) \
28f6bc909eSSimon Trimmer 	dev_warn(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
29f6bc909eSSimon Trimmer #define cs_dsp_info(_dsp, fmt, ...) \
30f6bc909eSSimon Trimmer 	dev_info(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
31f6bc909eSSimon Trimmer #define cs_dsp_dbg(_dsp, fmt, ...) \
32f6bc909eSSimon Trimmer 	dev_dbg(_dsp->dev, "%s: " fmt, _dsp->name, ##__VA_ARGS__)
33f6bc909eSSimon Trimmer 
34f6bc909eSSimon Trimmer #define ADSP1_CONTROL_1                   0x00
35f6bc909eSSimon Trimmer #define ADSP1_CONTROL_2                   0x02
36f6bc909eSSimon Trimmer #define ADSP1_CONTROL_3                   0x03
37f6bc909eSSimon Trimmer #define ADSP1_CONTROL_4                   0x04
38f6bc909eSSimon Trimmer #define ADSP1_CONTROL_5                   0x06
39f6bc909eSSimon Trimmer #define ADSP1_CONTROL_6                   0x07
40f6bc909eSSimon Trimmer #define ADSP1_CONTROL_7                   0x08
41f6bc909eSSimon Trimmer #define ADSP1_CONTROL_8                   0x09
42f6bc909eSSimon Trimmer #define ADSP1_CONTROL_9                   0x0A
43f6bc909eSSimon Trimmer #define ADSP1_CONTROL_10                  0x0B
44f6bc909eSSimon Trimmer #define ADSP1_CONTROL_11                  0x0C
45f6bc909eSSimon Trimmer #define ADSP1_CONTROL_12                  0x0D
46f6bc909eSSimon Trimmer #define ADSP1_CONTROL_13                  0x0F
47f6bc909eSSimon Trimmer #define ADSP1_CONTROL_14                  0x10
48f6bc909eSSimon Trimmer #define ADSP1_CONTROL_15                  0x11
49f6bc909eSSimon Trimmer #define ADSP1_CONTROL_16                  0x12
50f6bc909eSSimon Trimmer #define ADSP1_CONTROL_17                  0x13
51f6bc909eSSimon Trimmer #define ADSP1_CONTROL_18                  0x14
52f6bc909eSSimon Trimmer #define ADSP1_CONTROL_19                  0x16
53f6bc909eSSimon Trimmer #define ADSP1_CONTROL_20                  0x17
54f6bc909eSSimon Trimmer #define ADSP1_CONTROL_21                  0x18
55f6bc909eSSimon Trimmer #define ADSP1_CONTROL_22                  0x1A
56f6bc909eSSimon Trimmer #define ADSP1_CONTROL_23                  0x1B
57f6bc909eSSimon Trimmer #define ADSP1_CONTROL_24                  0x1C
58f6bc909eSSimon Trimmer #define ADSP1_CONTROL_25                  0x1E
59f6bc909eSSimon Trimmer #define ADSP1_CONTROL_26                  0x20
60f6bc909eSSimon Trimmer #define ADSP1_CONTROL_27                  0x21
61f6bc909eSSimon Trimmer #define ADSP1_CONTROL_28                  0x22
62f6bc909eSSimon Trimmer #define ADSP1_CONTROL_29                  0x23
63f6bc909eSSimon Trimmer #define ADSP1_CONTROL_30                  0x24
64f6bc909eSSimon Trimmer #define ADSP1_CONTROL_31                  0x26
65f6bc909eSSimon Trimmer 
66f6bc909eSSimon Trimmer /*
67f6bc909eSSimon Trimmer  * ADSP1 Control 19
68f6bc909eSSimon Trimmer  */
69f6bc909eSSimon Trimmer #define ADSP1_WDMA_BUFFER_LENGTH_MASK     0x00FF  /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
70f6bc909eSSimon Trimmer #define ADSP1_WDMA_BUFFER_LENGTH_SHIFT         0  /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
71f6bc909eSSimon Trimmer #define ADSP1_WDMA_BUFFER_LENGTH_WIDTH         8  /* DSP1_WDMA_BUFFER_LENGTH - [7:0] */
72f6bc909eSSimon Trimmer 
73f6bc909eSSimon Trimmer /*
74f6bc909eSSimon Trimmer  * ADSP1 Control 30
75f6bc909eSSimon Trimmer  */
76f6bc909eSSimon Trimmer #define ADSP1_DBG_CLK_ENA                 0x0008  /* DSP1_DBG_CLK_ENA */
77f6bc909eSSimon Trimmer #define ADSP1_DBG_CLK_ENA_MASK            0x0008  /* DSP1_DBG_CLK_ENA */
78f6bc909eSSimon Trimmer #define ADSP1_DBG_CLK_ENA_SHIFT                3  /* DSP1_DBG_CLK_ENA */
79f6bc909eSSimon Trimmer #define ADSP1_DBG_CLK_ENA_WIDTH                1  /* DSP1_DBG_CLK_ENA */
80f6bc909eSSimon Trimmer #define ADSP1_SYS_ENA                     0x0004  /* DSP1_SYS_ENA */
81f6bc909eSSimon Trimmer #define ADSP1_SYS_ENA_MASK                0x0004  /* DSP1_SYS_ENA */
82f6bc909eSSimon Trimmer #define ADSP1_SYS_ENA_SHIFT                    2  /* DSP1_SYS_ENA */
83f6bc909eSSimon Trimmer #define ADSP1_SYS_ENA_WIDTH                    1  /* DSP1_SYS_ENA */
84f6bc909eSSimon Trimmer #define ADSP1_CORE_ENA                    0x0002  /* DSP1_CORE_ENA */
85f6bc909eSSimon Trimmer #define ADSP1_CORE_ENA_MASK               0x0002  /* DSP1_CORE_ENA */
86f6bc909eSSimon Trimmer #define ADSP1_CORE_ENA_SHIFT                   1  /* DSP1_CORE_ENA */
87f6bc909eSSimon Trimmer #define ADSP1_CORE_ENA_WIDTH                   1  /* DSP1_CORE_ENA */
88f6bc909eSSimon Trimmer #define ADSP1_START                       0x0001  /* DSP1_START */
89f6bc909eSSimon Trimmer #define ADSP1_START_MASK                  0x0001  /* DSP1_START */
90f6bc909eSSimon Trimmer #define ADSP1_START_SHIFT                      0  /* DSP1_START */
91f6bc909eSSimon Trimmer #define ADSP1_START_WIDTH                      1  /* DSP1_START */
92f6bc909eSSimon Trimmer 
93f6bc909eSSimon Trimmer /*
94f6bc909eSSimon Trimmer  * ADSP1 Control 31
95f6bc909eSSimon Trimmer  */
96f6bc909eSSimon Trimmer #define ADSP1_CLK_SEL_MASK                0x0007  /* CLK_SEL_ENA */
97f6bc909eSSimon Trimmer #define ADSP1_CLK_SEL_SHIFT                    0  /* CLK_SEL_ENA */
98f6bc909eSSimon Trimmer #define ADSP1_CLK_SEL_WIDTH                    3  /* CLK_SEL_ENA */
99f6bc909eSSimon Trimmer 
100f6bc909eSSimon Trimmer #define ADSP2_CONTROL                     0x0
101f6bc909eSSimon Trimmer #define ADSP2_CLOCKING                    0x1
102f6bc909eSSimon Trimmer #define ADSP2V2_CLOCKING                  0x2
103f6bc909eSSimon Trimmer #define ADSP2_STATUS1                     0x4
104f6bc909eSSimon Trimmer #define ADSP2_WDMA_CONFIG_1               0x30
105f6bc909eSSimon Trimmer #define ADSP2_WDMA_CONFIG_2               0x31
106f6bc909eSSimon Trimmer #define ADSP2V2_WDMA_CONFIG_2             0x32
107f6bc909eSSimon Trimmer #define ADSP2_RDMA_CONFIG_1               0x34
108f6bc909eSSimon Trimmer 
109f6bc909eSSimon Trimmer #define ADSP2_SCRATCH0                    0x40
110f6bc909eSSimon Trimmer #define ADSP2_SCRATCH1                    0x41
111f6bc909eSSimon Trimmer #define ADSP2_SCRATCH2                    0x42
112f6bc909eSSimon Trimmer #define ADSP2_SCRATCH3                    0x43
113f6bc909eSSimon Trimmer 
114f6bc909eSSimon Trimmer #define ADSP2V2_SCRATCH0_1                0x40
115f6bc909eSSimon Trimmer #define ADSP2V2_SCRATCH2_3                0x42
116f6bc909eSSimon Trimmer 
117f6bc909eSSimon Trimmer /*
118f6bc909eSSimon Trimmer  * ADSP2 Control
119f6bc909eSSimon Trimmer  */
120f6bc909eSSimon Trimmer #define ADSP2_MEM_ENA                     0x0010  /* DSP1_MEM_ENA */
121f6bc909eSSimon Trimmer #define ADSP2_MEM_ENA_MASK                0x0010  /* DSP1_MEM_ENA */
122f6bc909eSSimon Trimmer #define ADSP2_MEM_ENA_SHIFT                    4  /* DSP1_MEM_ENA */
123f6bc909eSSimon Trimmer #define ADSP2_MEM_ENA_WIDTH                    1  /* DSP1_MEM_ENA */
124f6bc909eSSimon Trimmer #define ADSP2_SYS_ENA                     0x0004  /* DSP1_SYS_ENA */
125f6bc909eSSimon Trimmer #define ADSP2_SYS_ENA_MASK                0x0004  /* DSP1_SYS_ENA */
126f6bc909eSSimon Trimmer #define ADSP2_SYS_ENA_SHIFT                    2  /* DSP1_SYS_ENA */
127f6bc909eSSimon Trimmer #define ADSP2_SYS_ENA_WIDTH                    1  /* DSP1_SYS_ENA */
128f6bc909eSSimon Trimmer #define ADSP2_CORE_ENA                    0x0002  /* DSP1_CORE_ENA */
129f6bc909eSSimon Trimmer #define ADSP2_CORE_ENA_MASK               0x0002  /* DSP1_CORE_ENA */
130f6bc909eSSimon Trimmer #define ADSP2_CORE_ENA_SHIFT                   1  /* DSP1_CORE_ENA */
131f6bc909eSSimon Trimmer #define ADSP2_CORE_ENA_WIDTH                   1  /* DSP1_CORE_ENA */
132f6bc909eSSimon Trimmer #define ADSP2_START                       0x0001  /* DSP1_START */
133f6bc909eSSimon Trimmer #define ADSP2_START_MASK                  0x0001  /* DSP1_START */
134f6bc909eSSimon Trimmer #define ADSP2_START_SHIFT                      0  /* DSP1_START */
135f6bc909eSSimon Trimmer #define ADSP2_START_WIDTH                      1  /* DSP1_START */
136f6bc909eSSimon Trimmer 
137f6bc909eSSimon Trimmer /*
138f6bc909eSSimon Trimmer  * ADSP2 clocking
139f6bc909eSSimon Trimmer  */
140f6bc909eSSimon Trimmer #define ADSP2_CLK_SEL_MASK                0x0007  /* CLK_SEL_ENA */
141f6bc909eSSimon Trimmer #define ADSP2_CLK_SEL_SHIFT                    0  /* CLK_SEL_ENA */
142f6bc909eSSimon Trimmer #define ADSP2_CLK_SEL_WIDTH                    3  /* CLK_SEL_ENA */
143f6bc909eSSimon Trimmer 
144f6bc909eSSimon Trimmer /*
145f6bc909eSSimon Trimmer  * ADSP2V2 clocking
146f6bc909eSSimon Trimmer  */
147f6bc909eSSimon Trimmer #define ADSP2V2_CLK_SEL_MASK             0x70000  /* CLK_SEL_ENA */
148f6bc909eSSimon Trimmer #define ADSP2V2_CLK_SEL_SHIFT                 16  /* CLK_SEL_ENA */
149f6bc909eSSimon Trimmer #define ADSP2V2_CLK_SEL_WIDTH                  3  /* CLK_SEL_ENA */
150f6bc909eSSimon Trimmer 
151f6bc909eSSimon Trimmer #define ADSP2V2_RATE_MASK                 0x7800  /* DSP_RATE */
152f6bc909eSSimon Trimmer #define ADSP2V2_RATE_SHIFT                    11  /* DSP_RATE */
153f6bc909eSSimon Trimmer #define ADSP2V2_RATE_WIDTH                     4  /* DSP_RATE */
154f6bc909eSSimon Trimmer 
155f6bc909eSSimon Trimmer /*
156f6bc909eSSimon Trimmer  * ADSP2 Status 1
157f6bc909eSSimon Trimmer  */
158f6bc909eSSimon Trimmer #define ADSP2_RAM_RDY                     0x0001
159f6bc909eSSimon Trimmer #define ADSP2_RAM_RDY_MASK                0x0001
160f6bc909eSSimon Trimmer #define ADSP2_RAM_RDY_SHIFT                    0
161f6bc909eSSimon Trimmer #define ADSP2_RAM_RDY_WIDTH                    1
162f6bc909eSSimon Trimmer 
163f6bc909eSSimon Trimmer /*
164f6bc909eSSimon Trimmer  * ADSP2 Lock support
165f6bc909eSSimon Trimmer  */
166f6bc909eSSimon Trimmer #define ADSP2_LOCK_CODE_0                    0x5555
167f6bc909eSSimon Trimmer #define ADSP2_LOCK_CODE_1                    0xAAAA
168f6bc909eSSimon Trimmer 
169f6bc909eSSimon Trimmer #define ADSP2_WATCHDOG                       0x0A
170f6bc909eSSimon Trimmer #define ADSP2_BUS_ERR_ADDR                   0x52
171f6bc909eSSimon Trimmer #define ADSP2_REGION_LOCK_STATUS             0x64
172f6bc909eSSimon Trimmer #define ADSP2_LOCK_REGION_1_LOCK_REGION_0    0x66
173f6bc909eSSimon Trimmer #define ADSP2_LOCK_REGION_3_LOCK_REGION_2    0x68
174f6bc909eSSimon Trimmer #define ADSP2_LOCK_REGION_5_LOCK_REGION_4    0x6A
175f6bc909eSSimon Trimmer #define ADSP2_LOCK_REGION_7_LOCK_REGION_6    0x6C
176f6bc909eSSimon Trimmer #define ADSP2_LOCK_REGION_9_LOCK_REGION_8    0x6E
177f6bc909eSSimon Trimmer #define ADSP2_LOCK_REGION_CTRL               0x7A
178f6bc909eSSimon Trimmer #define ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR    0x7C
179f6bc909eSSimon Trimmer 
180f6bc909eSSimon Trimmer #define ADSP2_REGION_LOCK_ERR_MASK           0x8000
181f6bc909eSSimon Trimmer #define ADSP2_ADDR_ERR_MASK                  0x4000
182f6bc909eSSimon Trimmer #define ADSP2_WDT_TIMEOUT_STS_MASK           0x2000
183f6bc909eSSimon Trimmer #define ADSP2_CTRL_ERR_PAUSE_ENA             0x0002
184f6bc909eSSimon Trimmer #define ADSP2_CTRL_ERR_EINT                  0x0001
185f6bc909eSSimon Trimmer 
186f6bc909eSSimon Trimmer #define ADSP2_BUS_ERR_ADDR_MASK              0x00FFFFFF
187f6bc909eSSimon Trimmer #define ADSP2_XMEM_ERR_ADDR_MASK             0x0000FFFF
188f6bc909eSSimon Trimmer #define ADSP2_PMEM_ERR_ADDR_MASK             0x7FFF0000
189f6bc909eSSimon Trimmer #define ADSP2_PMEM_ERR_ADDR_SHIFT            16
190f6bc909eSSimon Trimmer #define ADSP2_WDT_ENA_MASK                   0xFFFFFFFD
191f6bc909eSSimon Trimmer 
192f6bc909eSSimon Trimmer #define ADSP2_LOCK_REGION_SHIFT              16
193f6bc909eSSimon Trimmer 
194f6bc909eSSimon Trimmer /*
195f6bc909eSSimon Trimmer  * Event control messages
196f6bc909eSSimon Trimmer  */
197f6bc909eSSimon Trimmer #define CS_DSP_FW_EVENT_SHUTDOWN             0x000001
198f6bc909eSSimon Trimmer 
199f6bc909eSSimon Trimmer /*
200f6bc909eSSimon Trimmer  * HALO system info
201f6bc909eSSimon Trimmer  */
202f6bc909eSSimon Trimmer #define HALO_AHBM_WINDOW_DEBUG_0             0x02040
203f6bc909eSSimon Trimmer #define HALO_AHBM_WINDOW_DEBUG_1             0x02044
204f6bc909eSSimon Trimmer 
205f6bc909eSSimon Trimmer /*
206f6bc909eSSimon Trimmer  * HALO core
207f6bc909eSSimon Trimmer  */
208f6bc909eSSimon Trimmer #define HALO_SCRATCH1                        0x005c0
209f6bc909eSSimon Trimmer #define HALO_SCRATCH2                        0x005c8
210f6bc909eSSimon Trimmer #define HALO_SCRATCH3                        0x005d0
211f6bc909eSSimon Trimmer #define HALO_SCRATCH4                        0x005d8
212f6bc909eSSimon Trimmer #define HALO_CCM_CORE_CONTROL                0x41000
213f6bc909eSSimon Trimmer #define HALO_CORE_SOFT_RESET                 0x00010
214f6bc909eSSimon Trimmer #define HALO_WDT_CONTROL                     0x47000
215f6bc909eSSimon Trimmer 
216f6bc909eSSimon Trimmer /*
217f6bc909eSSimon Trimmer  * HALO MPU banks
218f6bc909eSSimon Trimmer  */
219f6bc909eSSimon Trimmer #define HALO_MPU_XMEM_ACCESS_0               0x43000
220f6bc909eSSimon Trimmer #define HALO_MPU_YMEM_ACCESS_0               0x43004
221f6bc909eSSimon Trimmer #define HALO_MPU_WINDOW_ACCESS_0             0x43008
222f6bc909eSSimon Trimmer #define HALO_MPU_XREG_ACCESS_0               0x4300C
223f6bc909eSSimon Trimmer #define HALO_MPU_YREG_ACCESS_0               0x43014
224f6bc909eSSimon Trimmer #define HALO_MPU_XMEM_ACCESS_1               0x43018
225f6bc909eSSimon Trimmer #define HALO_MPU_YMEM_ACCESS_1               0x4301C
226f6bc909eSSimon Trimmer #define HALO_MPU_WINDOW_ACCESS_1             0x43020
227f6bc909eSSimon Trimmer #define HALO_MPU_XREG_ACCESS_1               0x43024
228f6bc909eSSimon Trimmer #define HALO_MPU_YREG_ACCESS_1               0x4302C
229f6bc909eSSimon Trimmer #define HALO_MPU_XMEM_ACCESS_2               0x43030
230f6bc909eSSimon Trimmer #define HALO_MPU_YMEM_ACCESS_2               0x43034
231f6bc909eSSimon Trimmer #define HALO_MPU_WINDOW_ACCESS_2             0x43038
232f6bc909eSSimon Trimmer #define HALO_MPU_XREG_ACCESS_2               0x4303C
233f6bc909eSSimon Trimmer #define HALO_MPU_YREG_ACCESS_2               0x43044
234f6bc909eSSimon Trimmer #define HALO_MPU_XMEM_ACCESS_3               0x43048
235f6bc909eSSimon Trimmer #define HALO_MPU_YMEM_ACCESS_3               0x4304C
236f6bc909eSSimon Trimmer #define HALO_MPU_WINDOW_ACCESS_3             0x43050
237f6bc909eSSimon Trimmer #define HALO_MPU_XREG_ACCESS_3               0x43054
238f6bc909eSSimon Trimmer #define HALO_MPU_YREG_ACCESS_3               0x4305C
239f6bc909eSSimon Trimmer #define HALO_MPU_XM_VIO_ADDR                 0x43100
240f6bc909eSSimon Trimmer #define HALO_MPU_XM_VIO_STATUS               0x43104
241f6bc909eSSimon Trimmer #define HALO_MPU_YM_VIO_ADDR                 0x43108
242f6bc909eSSimon Trimmer #define HALO_MPU_YM_VIO_STATUS               0x4310C
243f6bc909eSSimon Trimmer #define HALO_MPU_PM_VIO_ADDR                 0x43110
244f6bc909eSSimon Trimmer #define HALO_MPU_PM_VIO_STATUS               0x43114
245f6bc909eSSimon Trimmer #define HALO_MPU_LOCK_CONFIG                 0x43140
246f6bc909eSSimon Trimmer 
247f6bc909eSSimon Trimmer /*
248f6bc909eSSimon Trimmer  * HALO_AHBM_WINDOW_DEBUG_1
249f6bc909eSSimon Trimmer  */
250f6bc909eSSimon Trimmer #define HALO_AHBM_CORE_ERR_ADDR_MASK         0x0fffff00
251f6bc909eSSimon Trimmer #define HALO_AHBM_CORE_ERR_ADDR_SHIFT                 8
252f6bc909eSSimon Trimmer #define HALO_AHBM_FLAGS_ERR_MASK             0x000000ff
253f6bc909eSSimon Trimmer 
254f6bc909eSSimon Trimmer /*
255f6bc909eSSimon Trimmer  * HALO_CCM_CORE_CONTROL
256f6bc909eSSimon Trimmer  */
257f6bc909eSSimon Trimmer #define HALO_CORE_RESET                     0x00000200
258f6bc909eSSimon Trimmer #define HALO_CORE_EN                        0x00000001
259f6bc909eSSimon Trimmer 
260f6bc909eSSimon Trimmer /*
261f6bc909eSSimon Trimmer  * HALO_CORE_SOFT_RESET
262f6bc909eSSimon Trimmer  */
263f6bc909eSSimon Trimmer #define HALO_CORE_SOFT_RESET_MASK           0x00000001
264f6bc909eSSimon Trimmer 
265f6bc909eSSimon Trimmer /*
266f6bc909eSSimon Trimmer  * HALO_WDT_CONTROL
267f6bc909eSSimon Trimmer  */
268f6bc909eSSimon Trimmer #define HALO_WDT_EN_MASK                    0x00000001
269f6bc909eSSimon Trimmer 
270f6bc909eSSimon Trimmer /*
271f6bc909eSSimon Trimmer  * HALO_MPU_?M_VIO_STATUS
272f6bc909eSSimon Trimmer  */
273f6bc909eSSimon Trimmer #define HALO_MPU_VIO_STS_MASK               0x007e0000
274f6bc909eSSimon Trimmer #define HALO_MPU_VIO_STS_SHIFT                      17
275f6bc909eSSimon Trimmer #define HALO_MPU_VIO_ERR_WR_MASK            0x00008000
276f6bc909eSSimon Trimmer #define HALO_MPU_VIO_ERR_SRC_MASK           0x00007fff
277f6bc909eSSimon Trimmer #define HALO_MPU_VIO_ERR_SRC_SHIFT                   0
278f6bc909eSSimon Trimmer 
279205fdba5SJames Ogletree /*
280205fdba5SJames Ogletree  * Write Sequence
281205fdba5SJames Ogletree  */
282205fdba5SJames Ogletree #define WSEQ_OP_MAX_WORDS	3
283205fdba5SJames Ogletree #define WSEQ_END_OF_SCRIPT	0xFFFFFF
284205fdba5SJames Ogletree 
285f6bc909eSSimon Trimmer struct cs_dsp_ops {
286f6bc909eSSimon Trimmer 	bool (*validate_version)(struct cs_dsp *dsp, unsigned int version);
287f6bc909eSSimon Trimmer 	unsigned int (*parse_sizes)(struct cs_dsp *dsp,
288f6bc909eSSimon Trimmer 				    const char * const file,
289f6bc909eSSimon Trimmer 				    unsigned int pos,
290f6bc909eSSimon Trimmer 				    const struct firmware *firmware);
291f6bc909eSSimon Trimmer 	int (*setup_algs)(struct cs_dsp *dsp);
292f6bc909eSSimon Trimmer 	unsigned int (*region_to_reg)(struct cs_dsp_region const *mem,
293f6bc909eSSimon Trimmer 				      unsigned int offset);
294f6bc909eSSimon Trimmer 
295f6bc909eSSimon Trimmer 	void (*show_fw_status)(struct cs_dsp *dsp);
296f6bc909eSSimon Trimmer 	void (*stop_watchdog)(struct cs_dsp *dsp);
297f6bc909eSSimon Trimmer 
298f6bc909eSSimon Trimmer 	int (*enable_memory)(struct cs_dsp *dsp);
299f6bc909eSSimon Trimmer 	void (*disable_memory)(struct cs_dsp *dsp);
300f6bc909eSSimon Trimmer 	int (*lock_memory)(struct cs_dsp *dsp, unsigned int lock_regions);
301f6bc909eSSimon Trimmer 
302f6bc909eSSimon Trimmer 	int (*enable_core)(struct cs_dsp *dsp);
303f6bc909eSSimon Trimmer 	void (*disable_core)(struct cs_dsp *dsp);
304f6bc909eSSimon Trimmer 
305f6bc909eSSimon Trimmer 	int (*start_core)(struct cs_dsp *dsp);
306f6bc909eSSimon Trimmer 	void (*stop_core)(struct cs_dsp *dsp);
307f6bc909eSSimon Trimmer };
308f6bc909eSSimon Trimmer 
309f6bc909eSSimon Trimmer static const struct cs_dsp_ops cs_dsp_adsp1_ops;
310f6bc909eSSimon Trimmer static const struct cs_dsp_ops cs_dsp_adsp2_ops[];
311f6bc909eSSimon Trimmer static const struct cs_dsp_ops cs_dsp_halo_ops;
3127062e1c7SSimon Trimmer static const struct cs_dsp_ops cs_dsp_halo_ao_ops;
313f6bc909eSSimon Trimmer 
314f6bc909eSSimon Trimmer struct cs_dsp_buf {
315f6bc909eSSimon Trimmer 	struct list_head list;
316f6bc909eSSimon Trimmer 	void *buf;
317f6bc909eSSimon Trimmer };
318f6bc909eSSimon Trimmer 
cs_dsp_buf_alloc(const void * src,size_t len,struct list_head * list)319f6bc909eSSimon Trimmer static struct cs_dsp_buf *cs_dsp_buf_alloc(const void *src, size_t len,
320f6bc909eSSimon Trimmer 					   struct list_head *list)
321f6bc909eSSimon Trimmer {
322f6bc909eSSimon Trimmer 	struct cs_dsp_buf *buf = kzalloc(sizeof(*buf), GFP_KERNEL);
323f6bc909eSSimon Trimmer 
324f6bc909eSSimon Trimmer 	if (buf == NULL)
325f6bc909eSSimon Trimmer 		return NULL;
326f6bc909eSSimon Trimmer 
327f6bc909eSSimon Trimmer 	buf->buf = vmalloc(len);
328f6bc909eSSimon Trimmer 	if (!buf->buf) {
329f6bc909eSSimon Trimmer 		kfree(buf);
330f6bc909eSSimon Trimmer 		return NULL;
331f6bc909eSSimon Trimmer 	}
332f6bc909eSSimon Trimmer 	memcpy(buf->buf, src, len);
333f6bc909eSSimon Trimmer 
334f6bc909eSSimon Trimmer 	if (list)
335f6bc909eSSimon Trimmer 		list_add_tail(&buf->list, list);
336f6bc909eSSimon Trimmer 
337f6bc909eSSimon Trimmer 	return buf;
338f6bc909eSSimon Trimmer }
339f6bc909eSSimon Trimmer 
cs_dsp_buf_free(struct list_head * list)340f6bc909eSSimon Trimmer static void cs_dsp_buf_free(struct list_head *list)
341f6bc909eSSimon Trimmer {
342f6bc909eSSimon Trimmer 	while (!list_empty(list)) {
343f6bc909eSSimon Trimmer 		struct cs_dsp_buf *buf = list_first_entry(list,
344f6bc909eSSimon Trimmer 							  struct cs_dsp_buf,
345f6bc909eSSimon Trimmer 							  list);
346f6bc909eSSimon Trimmer 		list_del(&buf->list);
347f6bc909eSSimon Trimmer 		vfree(buf->buf);
348f6bc909eSSimon Trimmer 		kfree(buf);
349f6bc909eSSimon Trimmer 	}
350f6bc909eSSimon Trimmer }
351f6bc909eSSimon Trimmer 
352f6bc909eSSimon Trimmer /**
353f6bc909eSSimon Trimmer  * cs_dsp_mem_region_name() - Return a name string for a memory type
354f6bc909eSSimon Trimmer  * @type: the memory type to match
355f6bc909eSSimon Trimmer  *
356f6bc909eSSimon Trimmer  * Return: A const string identifying the memory region.
357f6bc909eSSimon Trimmer  */
cs_dsp_mem_region_name(unsigned int type)358f6bc909eSSimon Trimmer const char *cs_dsp_mem_region_name(unsigned int type)
359f6bc909eSSimon Trimmer {
360f6bc909eSSimon Trimmer 	switch (type) {
361f6bc909eSSimon Trimmer 	case WMFW_ADSP1_PM:
362f6bc909eSSimon Trimmer 		return "PM";
363f6bc909eSSimon Trimmer 	case WMFW_HALO_PM_PACKED:
364f6bc909eSSimon Trimmer 		return "PM_PACKED";
365f6bc909eSSimon Trimmer 	case WMFW_ADSP1_DM:
366f6bc909eSSimon Trimmer 		return "DM";
367f6bc909eSSimon Trimmer 	case WMFW_ADSP2_XM:
368f6bc909eSSimon Trimmer 		return "XM";
369f6bc909eSSimon Trimmer 	case WMFW_HALO_XM_PACKED:
370f6bc909eSSimon Trimmer 		return "XM_PACKED";
371f6bc909eSSimon Trimmer 	case WMFW_ADSP2_YM:
372f6bc909eSSimon Trimmer 		return "YM";
373f6bc909eSSimon Trimmer 	case WMFW_HALO_YM_PACKED:
374f6bc909eSSimon Trimmer 		return "YM_PACKED";
375f6bc909eSSimon Trimmer 	case WMFW_ADSP1_ZM:
376f6bc909eSSimon Trimmer 		return "ZM";
377f6bc909eSSimon Trimmer 	default:
378f6bc909eSSimon Trimmer 		return NULL;
379f6bc909eSSimon Trimmer 	}
380f6bc909eSSimon Trimmer }
381e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_mem_region_name, FW_CS_DSP);
382f6bc909eSSimon Trimmer 
383f6bc909eSSimon Trimmer #ifdef CONFIG_DEBUG_FS
cs_dsp_debugfs_save_wmfwname(struct cs_dsp * dsp,const char * s)384f6bc909eSSimon Trimmer static void cs_dsp_debugfs_save_wmfwname(struct cs_dsp *dsp, const char *s)
385f6bc909eSSimon Trimmer {
386f6bc909eSSimon Trimmer 	char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
387f6bc909eSSimon Trimmer 
388f6bc909eSSimon Trimmer 	kfree(dsp->wmfw_file_name);
389f6bc909eSSimon Trimmer 	dsp->wmfw_file_name = tmp;
390f6bc909eSSimon Trimmer }
391f6bc909eSSimon Trimmer 
cs_dsp_debugfs_save_binname(struct cs_dsp * dsp,const char * s)392f6bc909eSSimon Trimmer static void cs_dsp_debugfs_save_binname(struct cs_dsp *dsp, const char *s)
393f6bc909eSSimon Trimmer {
394f6bc909eSSimon Trimmer 	char *tmp = kasprintf(GFP_KERNEL, "%s\n", s);
395f6bc909eSSimon Trimmer 
396f6bc909eSSimon Trimmer 	kfree(dsp->bin_file_name);
397f6bc909eSSimon Trimmer 	dsp->bin_file_name = tmp;
398f6bc909eSSimon Trimmer }
399f6bc909eSSimon Trimmer 
cs_dsp_debugfs_clear(struct cs_dsp * dsp)400f6bc909eSSimon Trimmer static void cs_dsp_debugfs_clear(struct cs_dsp *dsp)
401f6bc909eSSimon Trimmer {
402f6bc909eSSimon Trimmer 	kfree(dsp->wmfw_file_name);
403f6bc909eSSimon Trimmer 	kfree(dsp->bin_file_name);
404f6bc909eSSimon Trimmer 	dsp->wmfw_file_name = NULL;
405f6bc909eSSimon Trimmer 	dsp->bin_file_name = NULL;
406f6bc909eSSimon Trimmer }
407f6bc909eSSimon Trimmer 
cs_dsp_debugfs_wmfw_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)408f6bc909eSSimon Trimmer static ssize_t cs_dsp_debugfs_wmfw_read(struct file *file,
409f6bc909eSSimon Trimmer 					char __user *user_buf,
410f6bc909eSSimon Trimmer 					size_t count, loff_t *ppos)
411f6bc909eSSimon Trimmer {
412f6bc909eSSimon Trimmer 	struct cs_dsp *dsp = file->private_data;
413f6bc909eSSimon Trimmer 	ssize_t ret;
414f6bc909eSSimon Trimmer 
415f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
416f6bc909eSSimon Trimmer 
417f6bc909eSSimon Trimmer 	if (!dsp->wmfw_file_name || !dsp->booted)
418f6bc909eSSimon Trimmer 		ret = 0;
419f6bc909eSSimon Trimmer 	else
420f6bc909eSSimon Trimmer 		ret = simple_read_from_buffer(user_buf, count, ppos,
421f6bc909eSSimon Trimmer 					      dsp->wmfw_file_name,
422f6bc909eSSimon Trimmer 					      strlen(dsp->wmfw_file_name));
423f6bc909eSSimon Trimmer 
424f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
425f6bc909eSSimon Trimmer 	return ret;
426f6bc909eSSimon Trimmer }
427f6bc909eSSimon Trimmer 
cs_dsp_debugfs_bin_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)428f6bc909eSSimon Trimmer static ssize_t cs_dsp_debugfs_bin_read(struct file *file,
429f6bc909eSSimon Trimmer 				       char __user *user_buf,
430f6bc909eSSimon Trimmer 				       size_t count, loff_t *ppos)
431f6bc909eSSimon Trimmer {
432f6bc909eSSimon Trimmer 	struct cs_dsp *dsp = file->private_data;
433f6bc909eSSimon Trimmer 	ssize_t ret;
434f6bc909eSSimon Trimmer 
435f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
436f6bc909eSSimon Trimmer 
437f6bc909eSSimon Trimmer 	if (!dsp->bin_file_name || !dsp->booted)
438f6bc909eSSimon Trimmer 		ret = 0;
439f6bc909eSSimon Trimmer 	else
440f6bc909eSSimon Trimmer 		ret = simple_read_from_buffer(user_buf, count, ppos,
441f6bc909eSSimon Trimmer 					      dsp->bin_file_name,
442f6bc909eSSimon Trimmer 					      strlen(dsp->bin_file_name));
443f6bc909eSSimon Trimmer 
444f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
445f6bc909eSSimon Trimmer 	return ret;
446f6bc909eSSimon Trimmer }
447f6bc909eSSimon Trimmer 
448f6bc909eSSimon Trimmer static const struct {
449f6bc909eSSimon Trimmer 	const char *name;
450f6bc909eSSimon Trimmer 	const struct file_operations fops;
451f6bc909eSSimon Trimmer } cs_dsp_debugfs_fops[] = {
452f6bc909eSSimon Trimmer 	{
453f6bc909eSSimon Trimmer 		.name = "wmfw_file_name",
454f6bc909eSSimon Trimmer 		.fops = {
455f6bc909eSSimon Trimmer 			.open = simple_open,
456f6bc909eSSimon Trimmer 			.read = cs_dsp_debugfs_wmfw_read,
457f6bc909eSSimon Trimmer 		},
458f6bc909eSSimon Trimmer 	},
459f6bc909eSSimon Trimmer 	{
460f6bc909eSSimon Trimmer 		.name = "bin_file_name",
461f6bc909eSSimon Trimmer 		.fops = {
462f6bc909eSSimon Trimmer 			.open = simple_open,
463f6bc909eSSimon Trimmer 			.read = cs_dsp_debugfs_bin_read,
464f6bc909eSSimon Trimmer 		},
465f6bc909eSSimon Trimmer 	},
466f6bc909eSSimon Trimmer };
467f6bc909eSSimon Trimmer 
4687a3f924cSSimon Trimmer static int cs_dsp_coeff_base_reg(struct cs_dsp_coeff_ctl *ctl, unsigned int *reg,
4697a3f924cSSimon Trimmer 				 unsigned int off);
4707a3f924cSSimon Trimmer 
cs_dsp_debugfs_read_controls_show(struct seq_file * s,void * ignored)4717a3f924cSSimon Trimmer static int cs_dsp_debugfs_read_controls_show(struct seq_file *s, void *ignored)
4727a3f924cSSimon Trimmer {
4737a3f924cSSimon Trimmer 	struct cs_dsp *dsp = s->private;
4747a3f924cSSimon Trimmer 	struct cs_dsp_coeff_ctl *ctl;
4757a3f924cSSimon Trimmer 	unsigned int reg;
4767a3f924cSSimon Trimmer 
4777a3f924cSSimon Trimmer 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
4787a3f924cSSimon Trimmer 		cs_dsp_coeff_base_reg(ctl, &reg, 0);
4797a3f924cSSimon Trimmer 		seq_printf(s, "%22.*s: %#8zx %s:%08x %#8x %s %#8x %#4x %c%c%c%c %s %s\n",
4807a3f924cSSimon Trimmer 			   ctl->subname_len, ctl->subname, ctl->len,
4817a3f924cSSimon Trimmer 			   cs_dsp_mem_region_name(ctl->alg_region.type),
4827a3f924cSSimon Trimmer 			   ctl->offset, reg, ctl->fw_name, ctl->alg_region.alg, ctl->type,
4837a3f924cSSimon Trimmer 			   ctl->flags & WMFW_CTL_FLAG_VOLATILE ? 'V' : '-',
4847a3f924cSSimon Trimmer 			   ctl->flags & WMFW_CTL_FLAG_SYS ? 'S' : '-',
4857a3f924cSSimon Trimmer 			   ctl->flags & WMFW_CTL_FLAG_READABLE ? 'R' : '-',
4867a3f924cSSimon Trimmer 			   ctl->flags & WMFW_CTL_FLAG_WRITEABLE ? 'W' : '-',
4877a3f924cSSimon Trimmer 			   ctl->enabled ? "enabled" : "disabled",
4887a3f924cSSimon Trimmer 			   ctl->set ? "dirty" : "clean");
4897a3f924cSSimon Trimmer 	}
4907a3f924cSSimon Trimmer 
4917a3f924cSSimon Trimmer 	return 0;
4927a3f924cSSimon Trimmer }
4937a3f924cSSimon Trimmer DEFINE_SHOW_ATTRIBUTE(cs_dsp_debugfs_read_controls);
4947a3f924cSSimon Trimmer 
495f6bc909eSSimon Trimmer /**
496f6bc909eSSimon Trimmer  * cs_dsp_init_debugfs() - Create and populate DSP representation in debugfs
497f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
498f6bc909eSSimon Trimmer  * @debugfs_root: pointer to debugfs directory in which to create this DSP
499f6bc909eSSimon Trimmer  *                representation
500f6bc909eSSimon Trimmer  */
cs_dsp_init_debugfs(struct cs_dsp * dsp,struct dentry * debugfs_root)501f6bc909eSSimon Trimmer void cs_dsp_init_debugfs(struct cs_dsp *dsp, struct dentry *debugfs_root)
502f6bc909eSSimon Trimmer {
503f6bc909eSSimon Trimmer 	struct dentry *root = NULL;
504f6bc909eSSimon Trimmer 	int i;
505f6bc909eSSimon Trimmer 
506f6bc909eSSimon Trimmer 	root = debugfs_create_dir(dsp->name, debugfs_root);
507f6bc909eSSimon Trimmer 
508f6bc909eSSimon Trimmer 	debugfs_create_bool("booted", 0444, root, &dsp->booted);
509f6bc909eSSimon Trimmer 	debugfs_create_bool("running", 0444, root, &dsp->running);
510f6bc909eSSimon Trimmer 	debugfs_create_x32("fw_id", 0444, root, &dsp->fw_id);
511f6bc909eSSimon Trimmer 	debugfs_create_x32("fw_version", 0444, root, &dsp->fw_id_version);
512f6bc909eSSimon Trimmer 
513f6bc909eSSimon Trimmer 	for (i = 0; i < ARRAY_SIZE(cs_dsp_debugfs_fops); ++i)
514f6bc909eSSimon Trimmer 		debugfs_create_file(cs_dsp_debugfs_fops[i].name, 0444, root,
515f6bc909eSSimon Trimmer 				    dsp, &cs_dsp_debugfs_fops[i].fops);
516f6bc909eSSimon Trimmer 
5177a3f924cSSimon Trimmer 	debugfs_create_file("controls", 0444, root, dsp,
5187a3f924cSSimon Trimmer 			    &cs_dsp_debugfs_read_controls_fops);
5197a3f924cSSimon Trimmer 
520f6bc909eSSimon Trimmer 	dsp->debugfs_root = root;
521f6bc909eSSimon Trimmer }
522e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_init_debugfs, FW_CS_DSP);
523f6bc909eSSimon Trimmer 
524f6bc909eSSimon Trimmer /**
525f6bc909eSSimon Trimmer  * cs_dsp_cleanup_debugfs() - Removes DSP representation from debugfs
526f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
527f6bc909eSSimon Trimmer  */
cs_dsp_cleanup_debugfs(struct cs_dsp * dsp)528f6bc909eSSimon Trimmer void cs_dsp_cleanup_debugfs(struct cs_dsp *dsp)
529f6bc909eSSimon Trimmer {
530f6bc909eSSimon Trimmer 	cs_dsp_debugfs_clear(dsp);
531f6bc909eSSimon Trimmer 	debugfs_remove_recursive(dsp->debugfs_root);
53266626b15SRichard Fitzgerald 	dsp->debugfs_root = ERR_PTR(-ENODEV);
533f6bc909eSSimon Trimmer }
534e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_cleanup_debugfs, FW_CS_DSP);
535f6bc909eSSimon Trimmer #else
cs_dsp_init_debugfs(struct cs_dsp * dsp,struct dentry * debugfs_root)536f6bc909eSSimon Trimmer void cs_dsp_init_debugfs(struct cs_dsp *dsp, struct dentry *debugfs_root)
537f6bc909eSSimon Trimmer {
538f6bc909eSSimon Trimmer }
539e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_init_debugfs, FW_CS_DSP);
540f6bc909eSSimon Trimmer 
cs_dsp_cleanup_debugfs(struct cs_dsp * dsp)541f6bc909eSSimon Trimmer void cs_dsp_cleanup_debugfs(struct cs_dsp *dsp)
542f6bc909eSSimon Trimmer {
543f6bc909eSSimon Trimmer }
544e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_cleanup_debugfs, FW_CS_DSP);
545f6bc909eSSimon Trimmer 
cs_dsp_debugfs_save_wmfwname(struct cs_dsp * dsp,const char * s)546f6bc909eSSimon Trimmer static inline void cs_dsp_debugfs_save_wmfwname(struct cs_dsp *dsp,
547f6bc909eSSimon Trimmer 						const char *s)
548f6bc909eSSimon Trimmer {
549f6bc909eSSimon Trimmer }
550f6bc909eSSimon Trimmer 
cs_dsp_debugfs_save_binname(struct cs_dsp * dsp,const char * s)551f6bc909eSSimon Trimmer static inline void cs_dsp_debugfs_save_binname(struct cs_dsp *dsp,
552f6bc909eSSimon Trimmer 					       const char *s)
553f6bc909eSSimon Trimmer {
554f6bc909eSSimon Trimmer }
555f6bc909eSSimon Trimmer 
cs_dsp_debugfs_clear(struct cs_dsp * dsp)556f6bc909eSSimon Trimmer static inline void cs_dsp_debugfs_clear(struct cs_dsp *dsp)
557f6bc909eSSimon Trimmer {
558f6bc909eSSimon Trimmer }
559f6bc909eSSimon Trimmer #endif
560f6bc909eSSimon Trimmer 
cs_dsp_find_region(struct cs_dsp * dsp,int type)561f6bc909eSSimon Trimmer static const struct cs_dsp_region *cs_dsp_find_region(struct cs_dsp *dsp,
562f6bc909eSSimon Trimmer 						      int type)
563f6bc909eSSimon Trimmer {
564f6bc909eSSimon Trimmer 	int i;
565f6bc909eSSimon Trimmer 
566f6bc909eSSimon Trimmer 	for (i = 0; i < dsp->num_mems; i++)
567f6bc909eSSimon Trimmer 		if (dsp->mem[i].type == type)
568f6bc909eSSimon Trimmer 			return &dsp->mem[i];
569f6bc909eSSimon Trimmer 
570f6bc909eSSimon Trimmer 	return NULL;
571f6bc909eSSimon Trimmer }
572f6bc909eSSimon Trimmer 
cs_dsp_region_to_reg(struct cs_dsp_region const * mem,unsigned int offset)573f6bc909eSSimon Trimmer static unsigned int cs_dsp_region_to_reg(struct cs_dsp_region const *mem,
574f6bc909eSSimon Trimmer 					 unsigned int offset)
575f6bc909eSSimon Trimmer {
576f6bc909eSSimon Trimmer 	switch (mem->type) {
577f6bc909eSSimon Trimmer 	case WMFW_ADSP1_PM:
578f6bc909eSSimon Trimmer 		return mem->base + (offset * 3);
579f6bc909eSSimon Trimmer 	case WMFW_ADSP1_DM:
580f6bc909eSSimon Trimmer 	case WMFW_ADSP2_XM:
581f6bc909eSSimon Trimmer 	case WMFW_ADSP2_YM:
582f6bc909eSSimon Trimmer 	case WMFW_ADSP1_ZM:
583f6bc909eSSimon Trimmer 		return mem->base + (offset * 2);
584f6bc909eSSimon Trimmer 	default:
585f6bc909eSSimon Trimmer 		WARN(1, "Unknown memory region type");
586f6bc909eSSimon Trimmer 		return offset;
587f6bc909eSSimon Trimmer 	}
588f6bc909eSSimon Trimmer }
589f6bc909eSSimon Trimmer 
cs_dsp_halo_region_to_reg(struct cs_dsp_region const * mem,unsigned int offset)590f6bc909eSSimon Trimmer static unsigned int cs_dsp_halo_region_to_reg(struct cs_dsp_region const *mem,
591f6bc909eSSimon Trimmer 					      unsigned int offset)
592f6bc909eSSimon Trimmer {
593f6bc909eSSimon Trimmer 	switch (mem->type) {
594f6bc909eSSimon Trimmer 	case WMFW_ADSP2_XM:
595f6bc909eSSimon Trimmer 	case WMFW_ADSP2_YM:
596f6bc909eSSimon Trimmer 		return mem->base + (offset * 4);
597f6bc909eSSimon Trimmer 	case WMFW_HALO_XM_PACKED:
598f6bc909eSSimon Trimmer 	case WMFW_HALO_YM_PACKED:
599f6bc909eSSimon Trimmer 		return (mem->base + (offset * 3)) & ~0x3;
600f6bc909eSSimon Trimmer 	case WMFW_HALO_PM_PACKED:
601f6bc909eSSimon Trimmer 		return mem->base + (offset * 5);
602f6bc909eSSimon Trimmer 	default:
603f6bc909eSSimon Trimmer 		WARN(1, "Unknown memory region type");
604f6bc909eSSimon Trimmer 		return offset;
605f6bc909eSSimon Trimmer 	}
606f6bc909eSSimon Trimmer }
607f6bc909eSSimon Trimmer 
cs_dsp_read_fw_status(struct cs_dsp * dsp,int noffs,unsigned int * offs)608f6bc909eSSimon Trimmer static void cs_dsp_read_fw_status(struct cs_dsp *dsp,
609f6bc909eSSimon Trimmer 				  int noffs, unsigned int *offs)
610f6bc909eSSimon Trimmer {
611f6bc909eSSimon Trimmer 	unsigned int i;
612f6bc909eSSimon Trimmer 	int ret;
613f6bc909eSSimon Trimmer 
614f6bc909eSSimon Trimmer 	for (i = 0; i < noffs; ++i) {
615f6bc909eSSimon Trimmer 		ret = regmap_read(dsp->regmap, dsp->base + offs[i], &offs[i]);
616f6bc909eSSimon Trimmer 		if (ret) {
617f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "Failed to read SCRATCH%u: %d\n", i, ret);
618f6bc909eSSimon Trimmer 			return;
619f6bc909eSSimon Trimmer 		}
620f6bc909eSSimon Trimmer 	}
621f6bc909eSSimon Trimmer }
622f6bc909eSSimon Trimmer 
cs_dsp_adsp2_show_fw_status(struct cs_dsp * dsp)623f6bc909eSSimon Trimmer static void cs_dsp_adsp2_show_fw_status(struct cs_dsp *dsp)
624f6bc909eSSimon Trimmer {
625f6bc909eSSimon Trimmer 	unsigned int offs[] = {
626f6bc909eSSimon Trimmer 		ADSP2_SCRATCH0, ADSP2_SCRATCH1, ADSP2_SCRATCH2, ADSP2_SCRATCH3,
627f6bc909eSSimon Trimmer 	};
628f6bc909eSSimon Trimmer 
629f6bc909eSSimon Trimmer 	cs_dsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs);
630f6bc909eSSimon Trimmer 
631f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
632f6bc909eSSimon Trimmer 		   offs[0], offs[1], offs[2], offs[3]);
633f6bc909eSSimon Trimmer }
634f6bc909eSSimon Trimmer 
cs_dsp_adsp2v2_show_fw_status(struct cs_dsp * dsp)635f6bc909eSSimon Trimmer static void cs_dsp_adsp2v2_show_fw_status(struct cs_dsp *dsp)
636f6bc909eSSimon Trimmer {
637f6bc909eSSimon Trimmer 	unsigned int offs[] = { ADSP2V2_SCRATCH0_1, ADSP2V2_SCRATCH2_3 };
638f6bc909eSSimon Trimmer 
639f6bc909eSSimon Trimmer 	cs_dsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs);
640f6bc909eSSimon Trimmer 
641f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
642f6bc909eSSimon Trimmer 		   offs[0] & 0xFFFF, offs[0] >> 16,
643f6bc909eSSimon Trimmer 		   offs[1] & 0xFFFF, offs[1] >> 16);
644f6bc909eSSimon Trimmer }
645f6bc909eSSimon Trimmer 
cs_dsp_halo_show_fw_status(struct cs_dsp * dsp)646f6bc909eSSimon Trimmer static void cs_dsp_halo_show_fw_status(struct cs_dsp *dsp)
647f6bc909eSSimon Trimmer {
648f6bc909eSSimon Trimmer 	unsigned int offs[] = {
649f6bc909eSSimon Trimmer 		HALO_SCRATCH1, HALO_SCRATCH2, HALO_SCRATCH3, HALO_SCRATCH4,
650f6bc909eSSimon Trimmer 	};
651f6bc909eSSimon Trimmer 
652f6bc909eSSimon Trimmer 	cs_dsp_read_fw_status(dsp, ARRAY_SIZE(offs), offs);
653f6bc909eSSimon Trimmer 
654f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "FW SCRATCH 0:0x%x 1:0x%x 2:0x%x 3:0x%x\n",
655f6bc909eSSimon Trimmer 		   offs[0], offs[1], offs[2], offs[3]);
656f6bc909eSSimon Trimmer }
657f6bc909eSSimon Trimmer 
cs_dsp_coeff_base_reg(struct cs_dsp_coeff_ctl * ctl,unsigned int * reg,unsigned int off)658f444da38SCharles Keepax static int cs_dsp_coeff_base_reg(struct cs_dsp_coeff_ctl *ctl, unsigned int *reg,
659f444da38SCharles Keepax 				 unsigned int off)
660f6bc909eSSimon Trimmer {
661f6bc909eSSimon Trimmer 	const struct cs_dsp_alg_region *alg_region = &ctl->alg_region;
662f6bc909eSSimon Trimmer 	struct cs_dsp *dsp = ctl->dsp;
663f6bc909eSSimon Trimmer 	const struct cs_dsp_region *mem;
664f6bc909eSSimon Trimmer 
665f6bc909eSSimon Trimmer 	mem = cs_dsp_find_region(dsp, alg_region->type);
666f6bc909eSSimon Trimmer 	if (!mem) {
667f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "No base for region %x\n",
668f6bc909eSSimon Trimmer 			   alg_region->type);
669f6bc909eSSimon Trimmer 		return -EINVAL;
670f6bc909eSSimon Trimmer 	}
671f6bc909eSSimon Trimmer 
672f444da38SCharles Keepax 	*reg = dsp->ops->region_to_reg(mem, ctl->alg_region.base + ctl->offset + off);
673f6bc909eSSimon Trimmer 
674f6bc909eSSimon Trimmer 	return 0;
675f6bc909eSSimon Trimmer }
676f6bc909eSSimon Trimmer 
677f6bc909eSSimon Trimmer /**
678f6bc909eSSimon Trimmer  * cs_dsp_coeff_write_acked_control() - Sends event_id to the acked control
679f6bc909eSSimon Trimmer  * @ctl: pointer to acked coefficient control
680f6bc909eSSimon Trimmer  * @event_id: the value to write to the given acked control
681f6bc909eSSimon Trimmer  *
682f6bc909eSSimon Trimmer  * Once the value has been written to the control the function shall block
683f6bc909eSSimon Trimmer  * until the running firmware acknowledges the write or timeout is exceeded.
684f6bc909eSSimon Trimmer  *
685f6bc909eSSimon Trimmer  * Must be called with pwr_lock held.
686f6bc909eSSimon Trimmer  *
687f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
688f6bc909eSSimon Trimmer  */
cs_dsp_coeff_write_acked_control(struct cs_dsp_coeff_ctl * ctl,unsigned int event_id)689f6bc909eSSimon Trimmer int cs_dsp_coeff_write_acked_control(struct cs_dsp_coeff_ctl *ctl, unsigned int event_id)
690f6bc909eSSimon Trimmer {
691f6bc909eSSimon Trimmer 	struct cs_dsp *dsp = ctl->dsp;
692f6bc909eSSimon Trimmer 	__be32 val = cpu_to_be32(event_id);
693f6bc909eSSimon Trimmer 	unsigned int reg;
694f6bc909eSSimon Trimmer 	int i, ret;
695f6bc909eSSimon Trimmer 
6965065cfabSCharles Keepax 	lockdep_assert_held(&dsp->pwr_lock);
6975065cfabSCharles Keepax 
698f6bc909eSSimon Trimmer 	if (!dsp->running)
699f6bc909eSSimon Trimmer 		return -EPERM;
700f6bc909eSSimon Trimmer 
701f444da38SCharles Keepax 	ret = cs_dsp_coeff_base_reg(ctl, &reg, 0);
702f6bc909eSSimon Trimmer 	if (ret)
703f6bc909eSSimon Trimmer 		return ret;
704f6bc909eSSimon Trimmer 
705f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "Sending 0x%x to acked control alg 0x%x %s:0x%x\n",
706f6bc909eSSimon Trimmer 		   event_id, ctl->alg_region.alg,
707f6bc909eSSimon Trimmer 		   cs_dsp_mem_region_name(ctl->alg_region.type), ctl->offset);
708f6bc909eSSimon Trimmer 
709f6bc909eSSimon Trimmer 	ret = regmap_raw_write(dsp->regmap, reg, &val, sizeof(val));
710f6bc909eSSimon Trimmer 	if (ret) {
711f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to write %x: %d\n", reg, ret);
712f6bc909eSSimon Trimmer 		return ret;
713f6bc909eSSimon Trimmer 	}
714f6bc909eSSimon Trimmer 
715f6bc909eSSimon Trimmer 	/*
716f6bc909eSSimon Trimmer 	 * Poll for ack, we initially poll at ~1ms intervals for firmwares
717f6bc909eSSimon Trimmer 	 * that respond quickly, then go to ~10ms polls. A firmware is unlikely
718f6bc909eSSimon Trimmer 	 * to ack instantly so we do the first 1ms delay before reading the
719f6bc909eSSimon Trimmer 	 * control to avoid a pointless bus transaction
720f6bc909eSSimon Trimmer 	 */
721f6bc909eSSimon Trimmer 	for (i = 0; i < CS_DSP_ACKED_CTL_TIMEOUT_MS;) {
722f6bc909eSSimon Trimmer 		switch (i) {
723f6bc909eSSimon Trimmer 		case 0 ... CS_DSP_ACKED_CTL_N_QUICKPOLLS - 1:
724f6bc909eSSimon Trimmer 			usleep_range(1000, 2000);
725f6bc909eSSimon Trimmer 			i++;
726f6bc909eSSimon Trimmer 			break;
727f6bc909eSSimon Trimmer 		default:
728f6bc909eSSimon Trimmer 			usleep_range(10000, 20000);
729f6bc909eSSimon Trimmer 			i += 10;
730f6bc909eSSimon Trimmer 			break;
731f6bc909eSSimon Trimmer 		}
732f6bc909eSSimon Trimmer 
733f6bc909eSSimon Trimmer 		ret = regmap_raw_read(dsp->regmap, reg, &val, sizeof(val));
734f6bc909eSSimon Trimmer 		if (ret) {
735f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "Failed to read %x: %d\n", reg, ret);
736f6bc909eSSimon Trimmer 			return ret;
737f6bc909eSSimon Trimmer 		}
738f6bc909eSSimon Trimmer 
739f6bc909eSSimon Trimmer 		if (val == 0) {
740f6bc909eSSimon Trimmer 			cs_dsp_dbg(dsp, "Acked control ACKED at poll %u\n", i);
741f6bc909eSSimon Trimmer 			return 0;
742f6bc909eSSimon Trimmer 		}
743f6bc909eSSimon Trimmer 	}
744f6bc909eSSimon Trimmer 
745f6bc909eSSimon Trimmer 	cs_dsp_warn(dsp, "Acked control @0x%x alg:0x%x %s:0x%x timed out\n",
746f6bc909eSSimon Trimmer 		    reg, ctl->alg_region.alg,
747f6bc909eSSimon Trimmer 		    cs_dsp_mem_region_name(ctl->alg_region.type),
748f6bc909eSSimon Trimmer 		    ctl->offset);
749f6bc909eSSimon Trimmer 
750f6bc909eSSimon Trimmer 	return -ETIMEDOUT;
751f6bc909eSSimon Trimmer }
752e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_coeff_write_acked_control, FW_CS_DSP);
753f6bc909eSSimon Trimmer 
cs_dsp_coeff_write_ctrl_raw(struct cs_dsp_coeff_ctl * ctl,unsigned int off,const void * buf,size_t len)754f6bc909eSSimon Trimmer static int cs_dsp_coeff_write_ctrl_raw(struct cs_dsp_coeff_ctl *ctl,
755f444da38SCharles Keepax 				       unsigned int off, const void *buf, size_t len)
756f6bc909eSSimon Trimmer {
757f6bc909eSSimon Trimmer 	struct cs_dsp *dsp = ctl->dsp;
758f6bc909eSSimon Trimmer 	void *scratch;
759f6bc909eSSimon Trimmer 	int ret;
760f6bc909eSSimon Trimmer 	unsigned int reg;
761f6bc909eSSimon Trimmer 
762f444da38SCharles Keepax 	ret = cs_dsp_coeff_base_reg(ctl, &reg, off);
763f6bc909eSSimon Trimmer 	if (ret)
764f6bc909eSSimon Trimmer 		return ret;
765f6bc909eSSimon Trimmer 
766f6bc909eSSimon Trimmer 	scratch = kmemdup(buf, len, GFP_KERNEL | GFP_DMA);
767f6bc909eSSimon Trimmer 	if (!scratch)
768f6bc909eSSimon Trimmer 		return -ENOMEM;
769f6bc909eSSimon Trimmer 
770f6bc909eSSimon Trimmer 	ret = regmap_raw_write(dsp->regmap, reg, scratch,
771f6bc909eSSimon Trimmer 			       len);
772f6bc909eSSimon Trimmer 	if (ret) {
773f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to write %zu bytes to %x: %d\n",
774f6bc909eSSimon Trimmer 			   len, reg, ret);
775f6bc909eSSimon Trimmer 		kfree(scratch);
776f6bc909eSSimon Trimmer 		return ret;
777f6bc909eSSimon Trimmer 	}
778f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "Wrote %zu bytes to %x\n", len, reg);
779f6bc909eSSimon Trimmer 
780f6bc909eSSimon Trimmer 	kfree(scratch);
781f6bc909eSSimon Trimmer 
782f6bc909eSSimon Trimmer 	return 0;
783f6bc909eSSimon Trimmer }
784f6bc909eSSimon Trimmer 
785f6bc909eSSimon Trimmer /**
786f6bc909eSSimon Trimmer  * cs_dsp_coeff_write_ctrl() - Writes the given buffer to the given coefficient control
787f6bc909eSSimon Trimmer  * @ctl: pointer to coefficient control
788f444da38SCharles Keepax  * @off: word offset at which data should be written
789f6bc909eSSimon Trimmer  * @buf: the buffer to write to the given control
790b329b3d3SCharles Keepax  * @len: the length of the buffer in bytes
791f6bc909eSSimon Trimmer  *
792f6bc909eSSimon Trimmer  * Must be called with pwr_lock held.
793f6bc909eSSimon Trimmer  *
794c56f4b24SSimon Trimmer  * Return: < 0 on error, 1 when the control value changed and 0 when it has not.
795f6bc909eSSimon Trimmer  */
cs_dsp_coeff_write_ctrl(struct cs_dsp_coeff_ctl * ctl,unsigned int off,const void * buf,size_t len)796f444da38SCharles Keepax int cs_dsp_coeff_write_ctrl(struct cs_dsp_coeff_ctl *ctl,
797f444da38SCharles Keepax 			    unsigned int off, const void *buf, size_t len)
798f6bc909eSSimon Trimmer {
799f6bc909eSSimon Trimmer 	int ret = 0;
800f6bc909eSSimon Trimmer 
80186c60804SCharles Keepax 	if (!ctl)
80286c60804SCharles Keepax 		return -ENOENT;
80386c60804SCharles Keepax 
804043c0a62SCharles Keepax 	lockdep_assert_held(&ctl->dsp->pwr_lock);
805043c0a62SCharles Keepax 
80662412a93SRichard Fitzgerald 	if (ctl->flags && !(ctl->flags & WMFW_CTL_FLAG_WRITEABLE))
80762412a93SRichard Fitzgerald 		return -EPERM;
80862412a93SRichard Fitzgerald 
809f444da38SCharles Keepax 	if (len + off * sizeof(u32) > ctl->len)
810f444da38SCharles Keepax 		return -EINVAL;
811f444da38SCharles Keepax 
812c56f4b24SSimon Trimmer 	if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
813f6bc909eSSimon Trimmer 		ret = -EPERM;
814c56f4b24SSimon Trimmer 	} else if (buf != ctl->cache) {
815c56f4b24SSimon Trimmer 		if (memcmp(ctl->cache + off * sizeof(u32), buf, len))
816f444da38SCharles Keepax 			memcpy(ctl->cache + off * sizeof(u32), buf, len);
817c56f4b24SSimon Trimmer 		else
818c56f4b24SSimon Trimmer 			return 0;
819c56f4b24SSimon Trimmer 	}
820f6bc909eSSimon Trimmer 
821f6bc909eSSimon Trimmer 	ctl->set = 1;
822f6bc909eSSimon Trimmer 	if (ctl->enabled && ctl->dsp->running)
823f444da38SCharles Keepax 		ret = cs_dsp_coeff_write_ctrl_raw(ctl, off, buf, len);
824f6bc909eSSimon Trimmer 
825c56f4b24SSimon Trimmer 	if (ret < 0)
826f6bc909eSSimon Trimmer 		return ret;
827c56f4b24SSimon Trimmer 
828c56f4b24SSimon Trimmer 	return 1;
829f6bc909eSSimon Trimmer }
830e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_coeff_write_ctrl, FW_CS_DSP);
831f6bc909eSSimon Trimmer 
8324d033379SSimon Trimmer /**
8334d033379SSimon Trimmer  * cs_dsp_coeff_lock_and_write_ctrl() - Writes the given buffer to the given coefficient control
8344d033379SSimon Trimmer  * @ctl: pointer to coefficient control
8354d033379SSimon Trimmer  * @off: word offset at which data should be written
8364d033379SSimon Trimmer  * @buf: the buffer to write to the given control
8374d033379SSimon Trimmer  * @len: the length of the buffer in bytes
8384d033379SSimon Trimmer  *
8394d033379SSimon Trimmer  * Same as cs_dsp_coeff_write_ctrl() but takes pwr_lock.
8404d033379SSimon Trimmer  *
8414d033379SSimon Trimmer  * Return: A negative number on error, 1 when the control value changed and 0 when it has not.
8424d033379SSimon Trimmer  */
cs_dsp_coeff_lock_and_write_ctrl(struct cs_dsp_coeff_ctl * ctl,unsigned int off,const void * buf,size_t len)8434d033379SSimon Trimmer int cs_dsp_coeff_lock_and_write_ctrl(struct cs_dsp_coeff_ctl *ctl,
8444d033379SSimon Trimmer 				     unsigned int off, const void *buf, size_t len)
8454d033379SSimon Trimmer {
8464d033379SSimon Trimmer 	struct cs_dsp *dsp = ctl->dsp;
8474d033379SSimon Trimmer 	int ret;
8484d033379SSimon Trimmer 
8494d033379SSimon Trimmer 	lockdep_assert_not_held(&dsp->pwr_lock);
8504d033379SSimon Trimmer 
8514d033379SSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
8524d033379SSimon Trimmer 	ret = cs_dsp_coeff_write_ctrl(ctl, off, buf, len);
8534d033379SSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
8544d033379SSimon Trimmer 
8554d033379SSimon Trimmer 	return ret;
8564d033379SSimon Trimmer }
8574d033379SSimon Trimmer EXPORT_SYMBOL_GPL(cs_dsp_coeff_lock_and_write_ctrl);
8584d033379SSimon Trimmer 
cs_dsp_coeff_read_ctrl_raw(struct cs_dsp_coeff_ctl * ctl,unsigned int off,void * buf,size_t len)859f444da38SCharles Keepax static int cs_dsp_coeff_read_ctrl_raw(struct cs_dsp_coeff_ctl *ctl,
860f444da38SCharles Keepax 				      unsigned int off, void *buf, size_t len)
861f6bc909eSSimon Trimmer {
862f6bc909eSSimon Trimmer 	struct cs_dsp *dsp = ctl->dsp;
863f6bc909eSSimon Trimmer 	void *scratch;
864f6bc909eSSimon Trimmer 	int ret;
865f6bc909eSSimon Trimmer 	unsigned int reg;
866f6bc909eSSimon Trimmer 
867f444da38SCharles Keepax 	ret = cs_dsp_coeff_base_reg(ctl, &reg, off);
868f6bc909eSSimon Trimmer 	if (ret)
869f6bc909eSSimon Trimmer 		return ret;
870f6bc909eSSimon Trimmer 
871f6bc909eSSimon Trimmer 	scratch = kmalloc(len, GFP_KERNEL | GFP_DMA);
872f6bc909eSSimon Trimmer 	if (!scratch)
873f6bc909eSSimon Trimmer 		return -ENOMEM;
874f6bc909eSSimon Trimmer 
875f6bc909eSSimon Trimmer 	ret = regmap_raw_read(dsp->regmap, reg, scratch, len);
876f6bc909eSSimon Trimmer 	if (ret) {
877f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to read %zu bytes from %x: %d\n",
878f6bc909eSSimon Trimmer 			   len, reg, ret);
879f6bc909eSSimon Trimmer 		kfree(scratch);
880f6bc909eSSimon Trimmer 		return ret;
881f6bc909eSSimon Trimmer 	}
882f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "Read %zu bytes from %x\n", len, reg);
883f6bc909eSSimon Trimmer 
884f6bc909eSSimon Trimmer 	memcpy(buf, scratch, len);
885f6bc909eSSimon Trimmer 	kfree(scratch);
886f6bc909eSSimon Trimmer 
887f6bc909eSSimon Trimmer 	return 0;
888f6bc909eSSimon Trimmer }
889f6bc909eSSimon Trimmer 
890f6bc909eSSimon Trimmer /**
891f6bc909eSSimon Trimmer  * cs_dsp_coeff_read_ctrl() - Reads the given coefficient control into the given buffer
892f6bc909eSSimon Trimmer  * @ctl: pointer to coefficient control
893f444da38SCharles Keepax  * @off: word offset at which data should be read
894f6bc909eSSimon Trimmer  * @buf: the buffer to store to the given control
895b329b3d3SCharles Keepax  * @len: the length of the buffer in bytes
896f6bc909eSSimon Trimmer  *
897f6bc909eSSimon Trimmer  * Must be called with pwr_lock held.
898f6bc909eSSimon Trimmer  *
899f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
900f6bc909eSSimon Trimmer  */
cs_dsp_coeff_read_ctrl(struct cs_dsp_coeff_ctl * ctl,unsigned int off,void * buf,size_t len)901f444da38SCharles Keepax int cs_dsp_coeff_read_ctrl(struct cs_dsp_coeff_ctl *ctl,
902f444da38SCharles Keepax 			   unsigned int off, void *buf, size_t len)
903f6bc909eSSimon Trimmer {
904f6bc909eSSimon Trimmer 	int ret = 0;
905f6bc909eSSimon Trimmer 
90686c60804SCharles Keepax 	if (!ctl)
90786c60804SCharles Keepax 		return -ENOENT;
90886c60804SCharles Keepax 
909043c0a62SCharles Keepax 	lockdep_assert_held(&ctl->dsp->pwr_lock);
910043c0a62SCharles Keepax 
911f444da38SCharles Keepax 	if (len + off * sizeof(u32) > ctl->len)
912f444da38SCharles Keepax 		return -EINVAL;
913f444da38SCharles Keepax 
914f6bc909eSSimon Trimmer 	if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
915f6bc909eSSimon Trimmer 		if (ctl->enabled && ctl->dsp->running)
916f444da38SCharles Keepax 			return cs_dsp_coeff_read_ctrl_raw(ctl, off, buf, len);
917f6bc909eSSimon Trimmer 		else
918f6bc909eSSimon Trimmer 			return -EPERM;
919f6bc909eSSimon Trimmer 	} else {
920f6bc909eSSimon Trimmer 		if (!ctl->flags && ctl->enabled && ctl->dsp->running)
921f444da38SCharles Keepax 			ret = cs_dsp_coeff_read_ctrl_raw(ctl, 0, ctl->cache, ctl->len);
922f6bc909eSSimon Trimmer 
923f6bc909eSSimon Trimmer 		if (buf != ctl->cache)
924f444da38SCharles Keepax 			memcpy(buf, ctl->cache + off * sizeof(u32), len);
925f6bc909eSSimon Trimmer 	}
926f6bc909eSSimon Trimmer 
927f6bc909eSSimon Trimmer 	return ret;
928f6bc909eSSimon Trimmer }
929e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_coeff_read_ctrl, FW_CS_DSP);
930f6bc909eSSimon Trimmer 
9314d033379SSimon Trimmer /**
9324d033379SSimon Trimmer  * cs_dsp_coeff_lock_and_read_ctrl() - Reads the given coefficient control into the given buffer
9334d033379SSimon Trimmer  * @ctl: pointer to coefficient control
9344d033379SSimon Trimmer  * @off: word offset at which data should be read
9354d033379SSimon Trimmer  * @buf: the buffer to store to the given control
9364d033379SSimon Trimmer  * @len: the length of the buffer in bytes
9374d033379SSimon Trimmer  *
9384d033379SSimon Trimmer  * Same as cs_dsp_coeff_read_ctrl() but takes pwr_lock.
9394d033379SSimon Trimmer  *
9404d033379SSimon Trimmer  * Return: Zero for success, a negative number on error.
9414d033379SSimon Trimmer  */
cs_dsp_coeff_lock_and_read_ctrl(struct cs_dsp_coeff_ctl * ctl,unsigned int off,void * buf,size_t len)9424d033379SSimon Trimmer int cs_dsp_coeff_lock_and_read_ctrl(struct cs_dsp_coeff_ctl *ctl,
9434d033379SSimon Trimmer 				    unsigned int off, void *buf, size_t len)
9444d033379SSimon Trimmer {
9454d033379SSimon Trimmer 	struct cs_dsp *dsp = ctl->dsp;
9464d033379SSimon Trimmer 	int ret;
9474d033379SSimon Trimmer 
9484d033379SSimon Trimmer 	lockdep_assert_not_held(&dsp->pwr_lock);
9494d033379SSimon Trimmer 
9504d033379SSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
9514d033379SSimon Trimmer 	ret = cs_dsp_coeff_read_ctrl(ctl, off, buf, len);
9524d033379SSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
9534d033379SSimon Trimmer 
9544d033379SSimon Trimmer 	return ret;
9554d033379SSimon Trimmer }
9564d033379SSimon Trimmer EXPORT_SYMBOL_GPL(cs_dsp_coeff_lock_and_read_ctrl);
9574d033379SSimon Trimmer 
cs_dsp_coeff_init_control_caches(struct cs_dsp * dsp)958f6bc909eSSimon Trimmer static int cs_dsp_coeff_init_control_caches(struct cs_dsp *dsp)
959f6bc909eSSimon Trimmer {
960f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_ctl *ctl;
961f6bc909eSSimon Trimmer 	int ret;
962f6bc909eSSimon Trimmer 
963f6bc909eSSimon Trimmer 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
964f6bc909eSSimon Trimmer 		if (!ctl->enabled || ctl->set)
965f6bc909eSSimon Trimmer 			continue;
966f6bc909eSSimon Trimmer 		if (ctl->flags & WMFW_CTL_FLAG_VOLATILE)
967f6bc909eSSimon Trimmer 			continue;
968f6bc909eSSimon Trimmer 
969f6bc909eSSimon Trimmer 		/*
970f6bc909eSSimon Trimmer 		 * For readable controls populate the cache from the DSP memory.
971f6bc909eSSimon Trimmer 		 * For non-readable controls the cache was zero-filled when
972f6bc909eSSimon Trimmer 		 * created so we don't need to do anything.
973f6bc909eSSimon Trimmer 		 */
974f6bc909eSSimon Trimmer 		if (!ctl->flags || (ctl->flags & WMFW_CTL_FLAG_READABLE)) {
975f444da38SCharles Keepax 			ret = cs_dsp_coeff_read_ctrl_raw(ctl, 0, ctl->cache, ctl->len);
976f6bc909eSSimon Trimmer 			if (ret < 0)
977f6bc909eSSimon Trimmer 				return ret;
978f6bc909eSSimon Trimmer 		}
979f6bc909eSSimon Trimmer 	}
980f6bc909eSSimon Trimmer 
981f6bc909eSSimon Trimmer 	return 0;
982f6bc909eSSimon Trimmer }
983f6bc909eSSimon Trimmer 
cs_dsp_coeff_sync_controls(struct cs_dsp * dsp)984f6bc909eSSimon Trimmer static int cs_dsp_coeff_sync_controls(struct cs_dsp *dsp)
985f6bc909eSSimon Trimmer {
986f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_ctl *ctl;
987f6bc909eSSimon Trimmer 	int ret;
988f6bc909eSSimon Trimmer 
989f6bc909eSSimon Trimmer 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
990f6bc909eSSimon Trimmer 		if (!ctl->enabled)
991f6bc909eSSimon Trimmer 			continue;
992f6bc909eSSimon Trimmer 		if (ctl->set && !(ctl->flags & WMFW_CTL_FLAG_VOLATILE)) {
993f444da38SCharles Keepax 			ret = cs_dsp_coeff_write_ctrl_raw(ctl, 0, ctl->cache,
994f6bc909eSSimon Trimmer 							  ctl->len);
995f6bc909eSSimon Trimmer 			if (ret < 0)
996f6bc909eSSimon Trimmer 				return ret;
997f6bc909eSSimon Trimmer 		}
998f6bc909eSSimon Trimmer 	}
999f6bc909eSSimon Trimmer 
1000f6bc909eSSimon Trimmer 	return 0;
1001f6bc909eSSimon Trimmer }
1002f6bc909eSSimon Trimmer 
cs_dsp_signal_event_controls(struct cs_dsp * dsp,unsigned int event)1003f6bc909eSSimon Trimmer static void cs_dsp_signal_event_controls(struct cs_dsp *dsp,
1004f6bc909eSSimon Trimmer 					 unsigned int event)
1005f6bc909eSSimon Trimmer {
1006f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_ctl *ctl;
1007f6bc909eSSimon Trimmer 	int ret;
1008f6bc909eSSimon Trimmer 
1009f6bc909eSSimon Trimmer 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
1010f6bc909eSSimon Trimmer 		if (ctl->type != WMFW_CTL_TYPE_HOSTEVENT)
1011f6bc909eSSimon Trimmer 			continue;
1012f6bc909eSSimon Trimmer 
1013f6bc909eSSimon Trimmer 		if (!ctl->enabled)
1014f6bc909eSSimon Trimmer 			continue;
1015f6bc909eSSimon Trimmer 
1016f6bc909eSSimon Trimmer 		ret = cs_dsp_coeff_write_acked_control(ctl, event);
1017f6bc909eSSimon Trimmer 		if (ret)
1018f6bc909eSSimon Trimmer 			cs_dsp_warn(dsp,
1019f6bc909eSSimon Trimmer 				    "Failed to send 0x%x event to alg 0x%x (%d)\n",
1020f6bc909eSSimon Trimmer 				    event, ctl->alg_region.alg, ret);
1021f6bc909eSSimon Trimmer 	}
1022f6bc909eSSimon Trimmer }
1023f6bc909eSSimon Trimmer 
cs_dsp_free_ctl_blk(struct cs_dsp_coeff_ctl * ctl)1024f6bc909eSSimon Trimmer static void cs_dsp_free_ctl_blk(struct cs_dsp_coeff_ctl *ctl)
1025f6bc909eSSimon Trimmer {
1026f6bc909eSSimon Trimmer 	kfree(ctl->cache);
1027f6bc909eSSimon Trimmer 	kfree(ctl->subname);
1028f6bc909eSSimon Trimmer 	kfree(ctl);
1029f6bc909eSSimon Trimmer }
1030f6bc909eSSimon Trimmer 
cs_dsp_create_control(struct cs_dsp * dsp,const struct cs_dsp_alg_region * alg_region,unsigned int offset,unsigned int len,const char * subname,unsigned int subname_len,unsigned int flags,unsigned int type)1031f6bc909eSSimon Trimmer static int cs_dsp_create_control(struct cs_dsp *dsp,
1032f6bc909eSSimon Trimmer 				 const struct cs_dsp_alg_region *alg_region,
1033f6bc909eSSimon Trimmer 				 unsigned int offset, unsigned int len,
1034f6bc909eSSimon Trimmer 				 const char *subname, unsigned int subname_len,
1035f6bc909eSSimon Trimmer 				 unsigned int flags, unsigned int type)
1036f6bc909eSSimon Trimmer {
1037f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_ctl *ctl;
1038f6bc909eSSimon Trimmer 	int ret;
1039f6bc909eSSimon Trimmer 
1040f6bc909eSSimon Trimmer 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
1041f6bc909eSSimon Trimmer 		if (ctl->fw_name == dsp->fw_name &&
1042f6bc909eSSimon Trimmer 		    ctl->alg_region.alg == alg_region->alg &&
1043f6bc909eSSimon Trimmer 		    ctl->alg_region.type == alg_region->type) {
1044f6bc909eSSimon Trimmer 			if ((!subname && !ctl->subname) ||
10457ac1102bSVlad Karpovich 			    (subname && (ctl->subname_len == subname_len) &&
10467ac1102bSVlad Karpovich 			     !strncmp(ctl->subname, subname, ctl->subname_len))) {
1047f6bc909eSSimon Trimmer 				if (!ctl->enabled)
1048f6bc909eSSimon Trimmer 					ctl->enabled = 1;
1049f6bc909eSSimon Trimmer 				return 0;
1050f6bc909eSSimon Trimmer 			}
1051f6bc909eSSimon Trimmer 		}
1052f6bc909eSSimon Trimmer 	}
1053f6bc909eSSimon Trimmer 
1054f6bc909eSSimon Trimmer 	ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
1055f6bc909eSSimon Trimmer 	if (!ctl)
1056f6bc909eSSimon Trimmer 		return -ENOMEM;
1057f6bc909eSSimon Trimmer 
1058f6bc909eSSimon Trimmer 	ctl->fw_name = dsp->fw_name;
1059f6bc909eSSimon Trimmer 	ctl->alg_region = *alg_region;
1060*dc0e5ca8SRichard Fitzgerald 	if (subname && dsp->wmfw_ver >= 2) {
1061f6bc909eSSimon Trimmer 		ctl->subname_len = subname_len;
10625b933c72SRichard Fitzgerald 		ctl->subname = kasprintf(GFP_KERNEL, "%.*s", subname_len, subname);
1063f6bc909eSSimon Trimmer 		if (!ctl->subname) {
1064f6bc909eSSimon Trimmer 			ret = -ENOMEM;
1065f6bc909eSSimon Trimmer 			goto err_ctl;
1066f6bc909eSSimon Trimmer 		}
1067f6bc909eSSimon Trimmer 	}
1068f6bc909eSSimon Trimmer 	ctl->enabled = 1;
1069f6bc909eSSimon Trimmer 	ctl->set = 0;
1070f6bc909eSSimon Trimmer 	ctl->dsp = dsp;
1071f6bc909eSSimon Trimmer 
1072f6bc909eSSimon Trimmer 	ctl->flags = flags;
1073f6bc909eSSimon Trimmer 	ctl->type = type;
1074f6bc909eSSimon Trimmer 	ctl->offset = offset;
1075f6bc909eSSimon Trimmer 	ctl->len = len;
1076f6bc909eSSimon Trimmer 	ctl->cache = kzalloc(ctl->len, GFP_KERNEL);
1077f6bc909eSSimon Trimmer 	if (!ctl->cache) {
1078f6bc909eSSimon Trimmer 		ret = -ENOMEM;
1079f6bc909eSSimon Trimmer 		goto err_ctl_subname;
1080f6bc909eSSimon Trimmer 	}
1081f6bc909eSSimon Trimmer 
1082f6bc909eSSimon Trimmer 	list_add(&ctl->list, &dsp->ctl_list);
1083f6bc909eSSimon Trimmer 
1084f6bc909eSSimon Trimmer 	if (dsp->client_ops->control_add) {
1085f6bc909eSSimon Trimmer 		ret = dsp->client_ops->control_add(ctl);
1086f6bc909eSSimon Trimmer 		if (ret)
1087f6bc909eSSimon Trimmer 			goto err_list_del;
1088f6bc909eSSimon Trimmer 	}
1089f6bc909eSSimon Trimmer 
1090f6bc909eSSimon Trimmer 	return 0;
1091f6bc909eSSimon Trimmer 
1092f6bc909eSSimon Trimmer err_list_del:
1093f6bc909eSSimon Trimmer 	list_del(&ctl->list);
1094f6bc909eSSimon Trimmer 	kfree(ctl->cache);
1095f6bc909eSSimon Trimmer err_ctl_subname:
1096f6bc909eSSimon Trimmer 	kfree(ctl->subname);
1097f6bc909eSSimon Trimmer err_ctl:
1098f6bc909eSSimon Trimmer 	kfree(ctl);
1099f6bc909eSSimon Trimmer 
1100f6bc909eSSimon Trimmer 	return ret;
1101f6bc909eSSimon Trimmer }
1102f6bc909eSSimon Trimmer 
1103f6bc909eSSimon Trimmer struct cs_dsp_coeff_parsed_alg {
1104f6bc909eSSimon Trimmer 	int id;
1105f6bc909eSSimon Trimmer 	const u8 *name;
1106f6bc909eSSimon Trimmer 	int name_len;
1107f6bc909eSSimon Trimmer 	int ncoeff;
1108f6bc909eSSimon Trimmer };
1109f6bc909eSSimon Trimmer 
1110f6bc909eSSimon Trimmer struct cs_dsp_coeff_parsed_coeff {
1111f6bc909eSSimon Trimmer 	int offset;
1112f6bc909eSSimon Trimmer 	int mem_type;
1113f6bc909eSSimon Trimmer 	const u8 *name;
1114f6bc909eSSimon Trimmer 	int name_len;
1115f6bc909eSSimon Trimmer 	unsigned int ctl_type;
1116f6bc909eSSimon Trimmer 	int flags;
1117f6bc909eSSimon Trimmer 	int len;
1118f6bc909eSSimon Trimmer };
1119f6bc909eSSimon Trimmer 
cs_dsp_coeff_parse_string(int bytes,const u8 ** pos,unsigned int avail,const u8 ** str)11202163aff6SRichard Fitzgerald static int cs_dsp_coeff_parse_string(int bytes, const u8 **pos, unsigned int avail,
11212163aff6SRichard Fitzgerald 				     const u8 **str)
1122f6bc909eSSimon Trimmer {
11232163aff6SRichard Fitzgerald 	int length, total_field_len;
11242163aff6SRichard Fitzgerald 
11252163aff6SRichard Fitzgerald 	/* String fields are at least one __le32 */
11262163aff6SRichard Fitzgerald 	if (sizeof(__le32) > avail) {
11272163aff6SRichard Fitzgerald 		*pos = NULL;
11282163aff6SRichard Fitzgerald 		return 0;
11292163aff6SRichard Fitzgerald 	}
1130f6bc909eSSimon Trimmer 
1131f6bc909eSSimon Trimmer 	switch (bytes) {
1132f6bc909eSSimon Trimmer 	case 1:
1133f6bc909eSSimon Trimmer 		length = **pos;
1134f6bc909eSSimon Trimmer 		break;
1135f6bc909eSSimon Trimmer 	case 2:
1136f6bc909eSSimon Trimmer 		length = le16_to_cpu(*((__le16 *)*pos));
1137f6bc909eSSimon Trimmer 		break;
1138f6bc909eSSimon Trimmer 	default:
1139f6bc909eSSimon Trimmer 		return 0;
1140f6bc909eSSimon Trimmer 	}
1141f6bc909eSSimon Trimmer 
11422163aff6SRichard Fitzgerald 	total_field_len = ((length + bytes) + 3) & ~0x03;
11432163aff6SRichard Fitzgerald 	if ((unsigned int)total_field_len > avail) {
11442163aff6SRichard Fitzgerald 		*pos = NULL;
11452163aff6SRichard Fitzgerald 		return 0;
11462163aff6SRichard Fitzgerald 	}
11472163aff6SRichard Fitzgerald 
1148f6bc909eSSimon Trimmer 	if (str)
1149f6bc909eSSimon Trimmer 		*str = *pos + bytes;
1150f6bc909eSSimon Trimmer 
11512163aff6SRichard Fitzgerald 	*pos += total_field_len;
1152f6bc909eSSimon Trimmer 
1153f6bc909eSSimon Trimmer 	return length;
1154f6bc909eSSimon Trimmer }
1155f6bc909eSSimon Trimmer 
cs_dsp_coeff_parse_int(int bytes,const u8 ** pos)1156f6bc909eSSimon Trimmer static int cs_dsp_coeff_parse_int(int bytes, const u8 **pos)
1157f6bc909eSSimon Trimmer {
1158f6bc909eSSimon Trimmer 	int val = 0;
1159f6bc909eSSimon Trimmer 
1160f6bc909eSSimon Trimmer 	switch (bytes) {
1161f6bc909eSSimon Trimmer 	case 2:
1162f6bc909eSSimon Trimmer 		val = le16_to_cpu(*((__le16 *)*pos));
1163f6bc909eSSimon Trimmer 		break;
1164f6bc909eSSimon Trimmer 	case 4:
1165f6bc909eSSimon Trimmer 		val = le32_to_cpu(*((__le32 *)*pos));
1166f6bc909eSSimon Trimmer 		break;
1167f6bc909eSSimon Trimmer 	default:
1168f6bc909eSSimon Trimmer 		break;
1169f6bc909eSSimon Trimmer 	}
1170f6bc909eSSimon Trimmer 
1171f6bc909eSSimon Trimmer 	*pos += bytes;
1172f6bc909eSSimon Trimmer 
1173f6bc909eSSimon Trimmer 	return val;
1174f6bc909eSSimon Trimmer }
1175f6bc909eSSimon Trimmer 
cs_dsp_coeff_parse_alg(struct cs_dsp * dsp,const struct wmfw_region * region,struct cs_dsp_coeff_parsed_alg * blk)11762163aff6SRichard Fitzgerald static int cs_dsp_coeff_parse_alg(struct cs_dsp *dsp,
11772163aff6SRichard Fitzgerald 				  const struct wmfw_region *region,
1178f6bc909eSSimon Trimmer 				  struct cs_dsp_coeff_parsed_alg *blk)
1179f6bc909eSSimon Trimmer {
1180f6bc909eSSimon Trimmer 	const struct wmfw_adsp_alg_data *raw;
11812163aff6SRichard Fitzgerald 	unsigned int data_len = le32_to_cpu(region->len);
11822163aff6SRichard Fitzgerald 	unsigned int pos;
11832163aff6SRichard Fitzgerald 	const u8 *tmp;
11842163aff6SRichard Fitzgerald 
11852163aff6SRichard Fitzgerald 	raw = (const struct wmfw_adsp_alg_data *)region->data;
1186f6bc909eSSimon Trimmer 
1187*dc0e5ca8SRichard Fitzgerald 	switch (dsp->wmfw_ver) {
1188f6bc909eSSimon Trimmer 	case 0:
1189f6bc909eSSimon Trimmer 	case 1:
11902163aff6SRichard Fitzgerald 		if (sizeof(*raw) > data_len)
11912163aff6SRichard Fitzgerald 			return -EOVERFLOW;
1192f6bc909eSSimon Trimmer 
1193f6bc909eSSimon Trimmer 		blk->id = le32_to_cpu(raw->id);
1194f6bc909eSSimon Trimmer 		blk->name = raw->name;
1195680e126eSRichard Fitzgerald 		blk->name_len = strnlen(raw->name, ARRAY_SIZE(raw->name));
1196f6bc909eSSimon Trimmer 		blk->ncoeff = le32_to_cpu(raw->ncoeff);
11972163aff6SRichard Fitzgerald 
11982163aff6SRichard Fitzgerald 		pos = sizeof(*raw);
1199f6bc909eSSimon Trimmer 		break;
1200f6bc909eSSimon Trimmer 	default:
12012163aff6SRichard Fitzgerald 		if (sizeof(raw->id) > data_len)
12022163aff6SRichard Fitzgerald 			return -EOVERFLOW;
12032163aff6SRichard Fitzgerald 
12042163aff6SRichard Fitzgerald 		tmp = region->data;
12052163aff6SRichard Fitzgerald 		blk->id = cs_dsp_coeff_parse_int(sizeof(raw->id), &tmp);
12062163aff6SRichard Fitzgerald 		pos = tmp - region->data;
12072163aff6SRichard Fitzgerald 
12082163aff6SRichard Fitzgerald 		tmp = &region->data[pos];
12092163aff6SRichard Fitzgerald 		blk->name_len = cs_dsp_coeff_parse_string(sizeof(u8), &tmp, data_len - pos,
1210f6bc909eSSimon Trimmer 							  &blk->name);
12112163aff6SRichard Fitzgerald 		if (!tmp)
12122163aff6SRichard Fitzgerald 			return -EOVERFLOW;
12132163aff6SRichard Fitzgerald 
12142163aff6SRichard Fitzgerald 		pos = tmp - region->data;
12152163aff6SRichard Fitzgerald 		cs_dsp_coeff_parse_string(sizeof(u16), &tmp, data_len - pos, NULL);
12162163aff6SRichard Fitzgerald 		if (!tmp)
12172163aff6SRichard Fitzgerald 			return -EOVERFLOW;
12182163aff6SRichard Fitzgerald 
12192163aff6SRichard Fitzgerald 		pos = tmp - region->data;
12202163aff6SRichard Fitzgerald 		if (sizeof(raw->ncoeff) > (data_len - pos))
12212163aff6SRichard Fitzgerald 			return -EOVERFLOW;
12222163aff6SRichard Fitzgerald 
12232163aff6SRichard Fitzgerald 		blk->ncoeff = cs_dsp_coeff_parse_int(sizeof(raw->ncoeff), &tmp);
12242163aff6SRichard Fitzgerald 		pos += sizeof(raw->ncoeff);
1225f6bc909eSSimon Trimmer 		break;
1226f6bc909eSSimon Trimmer 	}
1227f6bc909eSSimon Trimmer 
12282163aff6SRichard Fitzgerald 	if ((int)blk->ncoeff < 0)
12292163aff6SRichard Fitzgerald 		return -EOVERFLOW;
12302163aff6SRichard Fitzgerald 
1231f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "Algorithm ID: %#x\n", blk->id);
1232f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "Algorithm name: %.*s\n", blk->name_len, blk->name);
1233f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "# of coefficient descriptors: %#x\n", blk->ncoeff);
12342163aff6SRichard Fitzgerald 
12352163aff6SRichard Fitzgerald 	return pos;
1236f6bc909eSSimon Trimmer }
1237f6bc909eSSimon Trimmer 
cs_dsp_coeff_parse_coeff(struct cs_dsp * dsp,const struct wmfw_region * region,unsigned int pos,struct cs_dsp_coeff_parsed_coeff * blk)12382163aff6SRichard Fitzgerald static int cs_dsp_coeff_parse_coeff(struct cs_dsp *dsp,
12392163aff6SRichard Fitzgerald 				    const struct wmfw_region *region,
12402163aff6SRichard Fitzgerald 				    unsigned int pos,
1241f6bc909eSSimon Trimmer 				    struct cs_dsp_coeff_parsed_coeff *blk)
1242f6bc909eSSimon Trimmer {
1243f6bc909eSSimon Trimmer 	const struct wmfw_adsp_coeff_data *raw;
12442163aff6SRichard Fitzgerald 	unsigned int data_len = le32_to_cpu(region->len);
12452163aff6SRichard Fitzgerald 	unsigned int blk_len, blk_end_pos;
1246f6bc909eSSimon Trimmer 	const u8 *tmp;
12472163aff6SRichard Fitzgerald 
12482163aff6SRichard Fitzgerald 	raw = (const struct wmfw_adsp_coeff_data *)&region->data[pos];
12492163aff6SRichard Fitzgerald 	if (sizeof(raw->hdr) > (data_len - pos))
12502163aff6SRichard Fitzgerald 		return -EOVERFLOW;
12512163aff6SRichard Fitzgerald 
12522163aff6SRichard Fitzgerald 	blk_len = le32_to_cpu(raw->hdr.size);
12532163aff6SRichard Fitzgerald 	if (blk_len > S32_MAX)
12542163aff6SRichard Fitzgerald 		return -EOVERFLOW;
12552163aff6SRichard Fitzgerald 
12562163aff6SRichard Fitzgerald 	if (blk_len > (data_len - pos - sizeof(raw->hdr)))
12572163aff6SRichard Fitzgerald 		return -EOVERFLOW;
12582163aff6SRichard Fitzgerald 
12592163aff6SRichard Fitzgerald 	blk_end_pos = pos + sizeof(raw->hdr) + blk_len;
12602163aff6SRichard Fitzgerald 
12612163aff6SRichard Fitzgerald 	blk->offset = le16_to_cpu(raw->hdr.offset);
12622163aff6SRichard Fitzgerald 	blk->mem_type = le16_to_cpu(raw->hdr.type);
1263f6bc909eSSimon Trimmer 
1264*dc0e5ca8SRichard Fitzgerald 	switch (dsp->wmfw_ver) {
1265f6bc909eSSimon Trimmer 	case 0:
1266f6bc909eSSimon Trimmer 	case 1:
12672163aff6SRichard Fitzgerald 		if (sizeof(*raw) > (data_len - pos))
12682163aff6SRichard Fitzgerald 			return -EOVERFLOW;
1269f6bc909eSSimon Trimmer 
1270f6bc909eSSimon Trimmer 		blk->name = raw->name;
1271680e126eSRichard Fitzgerald 		blk->name_len = strnlen(raw->name, ARRAY_SIZE(raw->name));
1272f6bc909eSSimon Trimmer 		blk->ctl_type = le16_to_cpu(raw->ctl_type);
1273f6bc909eSSimon Trimmer 		blk->flags = le16_to_cpu(raw->flags);
1274f6bc909eSSimon Trimmer 		blk->len = le32_to_cpu(raw->len);
1275f6bc909eSSimon Trimmer 		break;
1276f6bc909eSSimon Trimmer 	default:
12772163aff6SRichard Fitzgerald 		pos += sizeof(raw->hdr);
12782163aff6SRichard Fitzgerald 		tmp = &region->data[pos];
12792163aff6SRichard Fitzgerald 		blk->name_len = cs_dsp_coeff_parse_string(sizeof(u8), &tmp, data_len - pos,
1280f6bc909eSSimon Trimmer 							  &blk->name);
12812163aff6SRichard Fitzgerald 		if (!tmp)
12822163aff6SRichard Fitzgerald 			return -EOVERFLOW;
1283f6bc909eSSimon Trimmer 
12842163aff6SRichard Fitzgerald 		pos = tmp - region->data;
12852163aff6SRichard Fitzgerald 		cs_dsp_coeff_parse_string(sizeof(u8), &tmp, data_len - pos, NULL);
12862163aff6SRichard Fitzgerald 		if (!tmp)
12872163aff6SRichard Fitzgerald 			return -EOVERFLOW;
12882163aff6SRichard Fitzgerald 
12892163aff6SRichard Fitzgerald 		pos = tmp - region->data;
12902163aff6SRichard Fitzgerald 		cs_dsp_coeff_parse_string(sizeof(u16), &tmp, data_len - pos, NULL);
12912163aff6SRichard Fitzgerald 		if (!tmp)
12922163aff6SRichard Fitzgerald 			return -EOVERFLOW;
12932163aff6SRichard Fitzgerald 
12942163aff6SRichard Fitzgerald 		pos = tmp - region->data;
12952163aff6SRichard Fitzgerald 		if (sizeof(raw->ctl_type) + sizeof(raw->flags) + sizeof(raw->len) >
12962163aff6SRichard Fitzgerald 		    (data_len - pos))
12972163aff6SRichard Fitzgerald 			return -EOVERFLOW;
12982163aff6SRichard Fitzgerald 
12992163aff6SRichard Fitzgerald 		blk->ctl_type = cs_dsp_coeff_parse_int(sizeof(raw->ctl_type), &tmp);
13002163aff6SRichard Fitzgerald 		pos += sizeof(raw->ctl_type);
13012163aff6SRichard Fitzgerald 		blk->flags = cs_dsp_coeff_parse_int(sizeof(raw->flags), &tmp);
13022163aff6SRichard Fitzgerald 		pos += sizeof(raw->flags);
13032163aff6SRichard Fitzgerald 		blk->len = cs_dsp_coeff_parse_int(sizeof(raw->len), &tmp);
1304f6bc909eSSimon Trimmer 		break;
1305f6bc909eSSimon Trimmer 	}
1306f6bc909eSSimon Trimmer 
1307f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "\tCoefficient type: %#x\n", blk->mem_type);
1308f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "\tCoefficient offset: %#x\n", blk->offset);
1309f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "\tCoefficient name: %.*s\n", blk->name_len, blk->name);
1310f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "\tCoefficient flags: %#x\n", blk->flags);
1311f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "\tALSA control type: %#x\n", blk->ctl_type);
1312f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "\tALSA control len: %#x\n", blk->len);
13132163aff6SRichard Fitzgerald 
13142163aff6SRichard Fitzgerald 	return blk_end_pos;
1315f6bc909eSSimon Trimmer }
1316f6bc909eSSimon Trimmer 
cs_dsp_check_coeff_flags(struct cs_dsp * dsp,const struct cs_dsp_coeff_parsed_coeff * coeff_blk,unsigned int f_required,unsigned int f_illegal)1317f6bc909eSSimon Trimmer static int cs_dsp_check_coeff_flags(struct cs_dsp *dsp,
1318f6bc909eSSimon Trimmer 				    const struct cs_dsp_coeff_parsed_coeff *coeff_blk,
1319f6bc909eSSimon Trimmer 				    unsigned int f_required,
1320f6bc909eSSimon Trimmer 				    unsigned int f_illegal)
1321f6bc909eSSimon Trimmer {
1322f6bc909eSSimon Trimmer 	if ((coeff_blk->flags & f_illegal) ||
1323f6bc909eSSimon Trimmer 	    ((coeff_blk->flags & f_required) != f_required)) {
1324f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Illegal flags 0x%x for control type 0x%x\n",
1325f6bc909eSSimon Trimmer 			   coeff_blk->flags, coeff_blk->ctl_type);
1326f6bc909eSSimon Trimmer 		return -EINVAL;
1327f6bc909eSSimon Trimmer 	}
1328f6bc909eSSimon Trimmer 
1329f6bc909eSSimon Trimmer 	return 0;
1330f6bc909eSSimon Trimmer }
1331f6bc909eSSimon Trimmer 
cs_dsp_parse_coeff(struct cs_dsp * dsp,const struct wmfw_region * region)1332f6bc909eSSimon Trimmer static int cs_dsp_parse_coeff(struct cs_dsp *dsp,
1333f6bc909eSSimon Trimmer 			      const struct wmfw_region *region)
1334f6bc909eSSimon Trimmer {
1335f6bc909eSSimon Trimmer 	struct cs_dsp_alg_region alg_region = {};
1336f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_parsed_alg alg_blk;
1337f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_parsed_coeff coeff_blk;
13382163aff6SRichard Fitzgerald 	int i, pos, ret;
1339f6bc909eSSimon Trimmer 
13402163aff6SRichard Fitzgerald 	pos = cs_dsp_coeff_parse_alg(dsp, region, &alg_blk);
13412163aff6SRichard Fitzgerald 	if (pos < 0)
13422163aff6SRichard Fitzgerald 		return pos;
13432163aff6SRichard Fitzgerald 
1344f6bc909eSSimon Trimmer 	for (i = 0; i < alg_blk.ncoeff; i++) {
13452163aff6SRichard Fitzgerald 		pos = cs_dsp_coeff_parse_coeff(dsp, region, pos, &coeff_blk);
13462163aff6SRichard Fitzgerald 		if (pos < 0)
13472163aff6SRichard Fitzgerald 			return pos;
1348f6bc909eSSimon Trimmer 
1349f6bc909eSSimon Trimmer 		switch (coeff_blk.ctl_type) {
1350f6bc909eSSimon Trimmer 		case WMFW_CTL_TYPE_BYTES:
1351f6bc909eSSimon Trimmer 			break;
1352f6bc909eSSimon Trimmer 		case WMFW_CTL_TYPE_ACKED:
1353f6bc909eSSimon Trimmer 			if (coeff_blk.flags & WMFW_CTL_FLAG_SYS)
1354f6bc909eSSimon Trimmer 				continue;	/* ignore */
1355f6bc909eSSimon Trimmer 
1356f6bc909eSSimon Trimmer 			ret = cs_dsp_check_coeff_flags(dsp, &coeff_blk,
1357f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_VOLATILE |
1358f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_WRITEABLE |
1359f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_READABLE,
1360f6bc909eSSimon Trimmer 						       0);
1361f6bc909eSSimon Trimmer 			if (ret)
1362f6bc909eSSimon Trimmer 				return -EINVAL;
1363f6bc909eSSimon Trimmer 			break;
1364f6bc909eSSimon Trimmer 		case WMFW_CTL_TYPE_HOSTEVENT:
13655c903f64SCharles Keepax 		case WMFW_CTL_TYPE_FWEVENT:
1366f6bc909eSSimon Trimmer 			ret = cs_dsp_check_coeff_flags(dsp, &coeff_blk,
1367f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_SYS |
1368f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_VOLATILE |
1369f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_WRITEABLE |
1370f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_READABLE,
1371f6bc909eSSimon Trimmer 						       0);
1372f6bc909eSSimon Trimmer 			if (ret)
1373f6bc909eSSimon Trimmer 				return -EINVAL;
1374f6bc909eSSimon Trimmer 			break;
1375f6bc909eSSimon Trimmer 		case WMFW_CTL_TYPE_HOST_BUFFER:
1376f6bc909eSSimon Trimmer 			ret = cs_dsp_check_coeff_flags(dsp, &coeff_blk,
1377f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_SYS |
1378f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_VOLATILE |
1379f6bc909eSSimon Trimmer 						       WMFW_CTL_FLAG_READABLE,
1380f6bc909eSSimon Trimmer 						       0);
1381f6bc909eSSimon Trimmer 			if (ret)
1382f6bc909eSSimon Trimmer 				return -EINVAL;
1383f6bc909eSSimon Trimmer 			break;
1384f6bc909eSSimon Trimmer 		default:
1385f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "Unknown control type: %d\n",
1386f6bc909eSSimon Trimmer 				   coeff_blk.ctl_type);
1387f6bc909eSSimon Trimmer 			return -EINVAL;
1388f6bc909eSSimon Trimmer 		}
1389f6bc909eSSimon Trimmer 
1390f6bc909eSSimon Trimmer 		alg_region.type = coeff_blk.mem_type;
1391f6bc909eSSimon Trimmer 		alg_region.alg = alg_blk.id;
1392f6bc909eSSimon Trimmer 
1393f6bc909eSSimon Trimmer 		ret = cs_dsp_create_control(dsp, &alg_region,
1394f6bc909eSSimon Trimmer 					    coeff_blk.offset,
1395f6bc909eSSimon Trimmer 					    coeff_blk.len,
1396f6bc909eSSimon Trimmer 					    coeff_blk.name,
1397f6bc909eSSimon Trimmer 					    coeff_blk.name_len,
1398f6bc909eSSimon Trimmer 					    coeff_blk.flags,
1399f6bc909eSSimon Trimmer 					    coeff_blk.ctl_type);
1400f6bc909eSSimon Trimmer 		if (ret < 0)
1401f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "Failed to create control: %.*s, %d\n",
1402f6bc909eSSimon Trimmer 				   coeff_blk.name_len, coeff_blk.name, ret);
1403f6bc909eSSimon Trimmer 	}
1404f6bc909eSSimon Trimmer 
1405f6bc909eSSimon Trimmer 	return 0;
1406f6bc909eSSimon Trimmer }
1407f6bc909eSSimon Trimmer 
cs_dsp_adsp1_parse_sizes(struct cs_dsp * dsp,const char * const file,unsigned int pos,const struct firmware * firmware)1408f6bc909eSSimon Trimmer static unsigned int cs_dsp_adsp1_parse_sizes(struct cs_dsp *dsp,
1409f6bc909eSSimon Trimmer 					     const char * const file,
1410f6bc909eSSimon Trimmer 					     unsigned int pos,
1411f6bc909eSSimon Trimmer 					     const struct firmware *firmware)
1412f6bc909eSSimon Trimmer {
1413f6bc909eSSimon Trimmer 	const struct wmfw_adsp1_sizes *adsp1_sizes;
1414f6bc909eSSimon Trimmer 
1415f6bc909eSSimon Trimmer 	adsp1_sizes = (void *)&firmware->data[pos];
14163019b86bSRichard Fitzgerald 	if (sizeof(*adsp1_sizes) > firmware->size - pos) {
14173019b86bSRichard Fitzgerald 		cs_dsp_err(dsp, "%s: file truncated\n", file);
14183019b86bSRichard Fitzgerald 		return 0;
14193019b86bSRichard Fitzgerald 	}
1420f6bc909eSSimon Trimmer 
1421f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "%s: %d DM, %d PM, %d ZM\n", file,
1422f6bc909eSSimon Trimmer 		   le32_to_cpu(adsp1_sizes->dm), le32_to_cpu(adsp1_sizes->pm),
1423f6bc909eSSimon Trimmer 		   le32_to_cpu(adsp1_sizes->zm));
1424f6bc909eSSimon Trimmer 
1425f6bc909eSSimon Trimmer 	return pos + sizeof(*adsp1_sizes);
1426f6bc909eSSimon Trimmer }
1427f6bc909eSSimon Trimmer 
cs_dsp_adsp2_parse_sizes(struct cs_dsp * dsp,const char * const file,unsigned int pos,const struct firmware * firmware)1428f6bc909eSSimon Trimmer static unsigned int cs_dsp_adsp2_parse_sizes(struct cs_dsp *dsp,
1429f6bc909eSSimon Trimmer 					     const char * const file,
1430f6bc909eSSimon Trimmer 					     unsigned int pos,
1431f6bc909eSSimon Trimmer 					     const struct firmware *firmware)
1432f6bc909eSSimon Trimmer {
1433f6bc909eSSimon Trimmer 	const struct wmfw_adsp2_sizes *adsp2_sizes;
1434f6bc909eSSimon Trimmer 
1435f6bc909eSSimon Trimmer 	adsp2_sizes = (void *)&firmware->data[pos];
14363019b86bSRichard Fitzgerald 	if (sizeof(*adsp2_sizes) > firmware->size - pos) {
14373019b86bSRichard Fitzgerald 		cs_dsp_err(dsp, "%s: file truncated\n", file);
14383019b86bSRichard Fitzgerald 		return 0;
14393019b86bSRichard Fitzgerald 	}
1440f6bc909eSSimon Trimmer 
1441f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "%s: %d XM, %d YM %d PM, %d ZM\n", file,
1442f6bc909eSSimon Trimmer 		   le32_to_cpu(adsp2_sizes->xm), le32_to_cpu(adsp2_sizes->ym),
1443f6bc909eSSimon Trimmer 		   le32_to_cpu(adsp2_sizes->pm), le32_to_cpu(adsp2_sizes->zm));
1444f6bc909eSSimon Trimmer 
1445f6bc909eSSimon Trimmer 	return pos + sizeof(*adsp2_sizes);
1446f6bc909eSSimon Trimmer }
1447f6bc909eSSimon Trimmer 
cs_dsp_validate_version(struct cs_dsp * dsp,unsigned int version)1448f6bc909eSSimon Trimmer static bool cs_dsp_validate_version(struct cs_dsp *dsp, unsigned int version)
1449f6bc909eSSimon Trimmer {
1450f6bc909eSSimon Trimmer 	switch (version) {
1451f6bc909eSSimon Trimmer 	case 0:
1452f6bc909eSSimon Trimmer 		cs_dsp_warn(dsp, "Deprecated file format %d\n", version);
1453f6bc909eSSimon Trimmer 		return true;
1454f6bc909eSSimon Trimmer 	case 1:
1455f6bc909eSSimon Trimmer 	case 2:
1456f6bc909eSSimon Trimmer 		return true;
1457f6bc909eSSimon Trimmer 	default:
1458f6bc909eSSimon Trimmer 		return false;
1459f6bc909eSSimon Trimmer 	}
1460f6bc909eSSimon Trimmer }
1461f6bc909eSSimon Trimmer 
cs_dsp_halo_validate_version(struct cs_dsp * dsp,unsigned int version)1462f6bc909eSSimon Trimmer static bool cs_dsp_halo_validate_version(struct cs_dsp *dsp, unsigned int version)
1463f6bc909eSSimon Trimmer {
1464f6bc909eSSimon Trimmer 	switch (version) {
1465f6bc909eSSimon Trimmer 	case 3:
1466f6bc909eSSimon Trimmer 		return true;
1467f6bc909eSSimon Trimmer 	default:
1468f6bc909eSSimon Trimmer 		return false;
1469f6bc909eSSimon Trimmer 	}
1470f6bc909eSSimon Trimmer }
1471f6bc909eSSimon Trimmer 
cs_dsp_load(struct cs_dsp * dsp,const struct firmware * firmware,const char * file)1472f6bc909eSSimon Trimmer static int cs_dsp_load(struct cs_dsp *dsp, const struct firmware *firmware,
1473f6bc909eSSimon Trimmer 		       const char *file)
1474f6bc909eSSimon Trimmer {
1475f6bc909eSSimon Trimmer 	LIST_HEAD(buf_list);
1476f6bc909eSSimon Trimmer 	struct regmap *regmap = dsp->regmap;
1477f6bc909eSSimon Trimmer 	unsigned int pos = 0;
1478f6bc909eSSimon Trimmer 	const struct wmfw_header *header;
1479f6bc909eSSimon Trimmer 	const struct wmfw_footer *footer;
1480f6bc909eSSimon Trimmer 	const struct wmfw_region *region;
1481f6bc909eSSimon Trimmer 	const struct cs_dsp_region *mem;
1482f6bc909eSSimon Trimmer 	const char *region_name;
1483f6bc909eSSimon Trimmer 	struct cs_dsp_buf *buf;
1484f6bc909eSSimon Trimmer 	unsigned int reg;
1485f6bc909eSSimon Trimmer 	int regions = 0;
1486f6bc909eSSimon Trimmer 	int ret, offset, type;
1487f6bc909eSSimon Trimmer 
1488107c1674SSimon Trimmer 	if (!firmware)
1489107c1674SSimon Trimmer 		return 0;
1490107c1674SSimon Trimmer 
1491f6bc909eSSimon Trimmer 	ret = -EINVAL;
1492f6bc909eSSimon Trimmer 
14933019b86bSRichard Fitzgerald 	if (sizeof(*header) >= firmware->size) {
14943019b86bSRichard Fitzgerald 		ret = -EOVERFLOW;
1495f6bc909eSSimon Trimmer 		goto out_fw;
1496f6bc909eSSimon Trimmer 	}
1497f6bc909eSSimon Trimmer 
1498f6bc909eSSimon Trimmer 	header = (void *)&firmware->data[0];
1499f6bc909eSSimon Trimmer 
1500f6bc909eSSimon Trimmer 	if (memcmp(&header->magic[0], "WMFW", 4) != 0) {
1501f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "%s: invalid magic\n", file);
1502f6bc909eSSimon Trimmer 		goto out_fw;
1503f6bc909eSSimon Trimmer 	}
1504f6bc909eSSimon Trimmer 
1505f6bc909eSSimon Trimmer 	if (!dsp->ops->validate_version(dsp, header->ver)) {
1506f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "%s: unknown file format %d\n",
1507f6bc909eSSimon Trimmer 			   file, header->ver);
1508f6bc909eSSimon Trimmer 		goto out_fw;
1509f6bc909eSSimon Trimmer 	}
1510f6bc909eSSimon Trimmer 
1511*dc0e5ca8SRichard Fitzgerald 	dsp->wmfw_ver = header->ver;
1512f6bc909eSSimon Trimmer 
1513f6bc909eSSimon Trimmer 	if (header->core != dsp->type) {
1514f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "%s: invalid core %d != %d\n",
1515f6bc909eSSimon Trimmer 			   file, header->core, dsp->type);
1516f6bc909eSSimon Trimmer 		goto out_fw;
1517f6bc909eSSimon Trimmer 	}
1518f6bc909eSSimon Trimmer 
1519f6bc909eSSimon Trimmer 	pos = sizeof(*header);
1520f6bc909eSSimon Trimmer 	pos = dsp->ops->parse_sizes(dsp, file, pos, firmware);
15213019b86bSRichard Fitzgerald 	if ((pos == 0) || (sizeof(*footer) > firmware->size - pos)) {
15223019b86bSRichard Fitzgerald 		ret = -EOVERFLOW;
15233019b86bSRichard Fitzgerald 		goto out_fw;
15243019b86bSRichard Fitzgerald 	}
1525f6bc909eSSimon Trimmer 
1526f6bc909eSSimon Trimmer 	footer = (void *)&firmware->data[pos];
1527f6bc909eSSimon Trimmer 	pos += sizeof(*footer);
1528f6bc909eSSimon Trimmer 
1529f6bc909eSSimon Trimmer 	if (le32_to_cpu(header->len) != pos) {
15303019b86bSRichard Fitzgerald 		ret = -EOVERFLOW;
1531f6bc909eSSimon Trimmer 		goto out_fw;
1532f6bc909eSSimon Trimmer 	}
1533f6bc909eSSimon Trimmer 
1534a4939119SRichard Fitzgerald 	cs_dsp_info(dsp, "%s: format %d timestamp %#llx\n", file, header->ver,
1535f6bc909eSSimon Trimmer 		    le64_to_cpu(footer->timestamp));
1536f6bc909eSSimon Trimmer 
1537959fe01eSRichard Fitzgerald 	while (pos < firmware->size) {
1538959fe01eSRichard Fitzgerald 		/* Is there enough data for a complete block header? */
1539959fe01eSRichard Fitzgerald 		if (sizeof(*region) > firmware->size - pos) {
1540959fe01eSRichard Fitzgerald 			ret = -EOVERFLOW;
1541959fe01eSRichard Fitzgerald 			goto out_fw;
1542959fe01eSRichard Fitzgerald 		}
1543959fe01eSRichard Fitzgerald 
1544f6bc909eSSimon Trimmer 		region = (void *)&(firmware->data[pos]);
15456598afa9SRichard Fitzgerald 
15466598afa9SRichard Fitzgerald 		if (le32_to_cpu(region->len) > firmware->size - pos - sizeof(*region)) {
15476598afa9SRichard Fitzgerald 			ret = -EOVERFLOW;
15486598afa9SRichard Fitzgerald 			goto out_fw;
15496598afa9SRichard Fitzgerald 		}
15506598afa9SRichard Fitzgerald 
1551f6bc909eSSimon Trimmer 		region_name = "Unknown";
1552f6bc909eSSimon Trimmer 		reg = 0;
1553f6bc909eSSimon Trimmer 		offset = le32_to_cpu(region->offset) & 0xffffff;
1554f6bc909eSSimon Trimmer 		type = be32_to_cpu(region->type) & 0xff;
1555f6bc909eSSimon Trimmer 
1556f6bc909eSSimon Trimmer 		switch (type) {
1557bff92858SRichard Fitzgerald 		case WMFW_INFO_TEXT:
1558f6bc909eSSimon Trimmer 		case WMFW_NAME_TEXT:
1559bff92858SRichard Fitzgerald 			region_name = "Info/Name";
1560bff92858SRichard Fitzgerald 			cs_dsp_info(dsp, "%s: %.*s\n", file,
1561bff92858SRichard Fitzgerald 				    min(le32_to_cpu(region->len), 100), region->data);
1562f6bc909eSSimon Trimmer 			break;
1563f6bc909eSSimon Trimmer 		case WMFW_ALGORITHM_DATA:
1564f6bc909eSSimon Trimmer 			region_name = "Algorithm";
1565f6bc909eSSimon Trimmer 			ret = cs_dsp_parse_coeff(dsp, region);
1566f6bc909eSSimon Trimmer 			if (ret != 0)
1567f6bc909eSSimon Trimmer 				goto out_fw;
1568f6bc909eSSimon Trimmer 			break;
1569f6bc909eSSimon Trimmer 		case WMFW_ABSOLUTE:
1570f6bc909eSSimon Trimmer 			region_name = "Absolute";
1571f6bc909eSSimon Trimmer 			reg = offset;
1572f6bc909eSSimon Trimmer 			break;
1573f6bc909eSSimon Trimmer 		case WMFW_ADSP1_PM:
1574f6bc909eSSimon Trimmer 		case WMFW_ADSP1_DM:
1575f6bc909eSSimon Trimmer 		case WMFW_ADSP2_XM:
1576f6bc909eSSimon Trimmer 		case WMFW_ADSP2_YM:
1577f6bc909eSSimon Trimmer 		case WMFW_ADSP1_ZM:
1578f6bc909eSSimon Trimmer 		case WMFW_HALO_PM_PACKED:
1579f6bc909eSSimon Trimmer 		case WMFW_HALO_XM_PACKED:
1580f6bc909eSSimon Trimmer 		case WMFW_HALO_YM_PACKED:
1581f6bc909eSSimon Trimmer 			mem = cs_dsp_find_region(dsp, type);
1582f6bc909eSSimon Trimmer 			if (!mem) {
1583f6bc909eSSimon Trimmer 				cs_dsp_err(dsp, "No region of type: %x\n", type);
1584f6bc909eSSimon Trimmer 				ret = -EINVAL;
1585f6bc909eSSimon Trimmer 				goto out_fw;
1586f6bc909eSSimon Trimmer 			}
1587f6bc909eSSimon Trimmer 
1588f6bc909eSSimon Trimmer 			region_name = cs_dsp_mem_region_name(type);
1589f6bc909eSSimon Trimmer 			reg = dsp->ops->region_to_reg(mem, offset);
1590f6bc909eSSimon Trimmer 			break;
1591f6bc909eSSimon Trimmer 		default:
1592f6bc909eSSimon Trimmer 			cs_dsp_warn(dsp,
1593f6bc909eSSimon Trimmer 				    "%s.%d: Unknown region type %x at %d(%x)\n",
1594f6bc909eSSimon Trimmer 				    file, regions, type, pos, pos);
1595f6bc909eSSimon Trimmer 			break;
1596f6bc909eSSimon Trimmer 		}
1597f6bc909eSSimon Trimmer 
1598f6bc909eSSimon Trimmer 		cs_dsp_dbg(dsp, "%s.%d: %d bytes at %d in %s\n", file,
1599f6bc909eSSimon Trimmer 			   regions, le32_to_cpu(region->len), offset,
1600f6bc909eSSimon Trimmer 			   region_name);
1601f6bc909eSSimon Trimmer 
1602f6bc909eSSimon Trimmer 		if (reg) {
1603f6bc909eSSimon Trimmer 			buf = cs_dsp_buf_alloc(region->data,
1604f6bc909eSSimon Trimmer 					       le32_to_cpu(region->len),
1605f6bc909eSSimon Trimmer 					       &buf_list);
1606f6bc909eSSimon Trimmer 			if (!buf) {
1607f6bc909eSSimon Trimmer 				cs_dsp_err(dsp, "Out of memory\n");
1608f6bc909eSSimon Trimmer 				ret = -ENOMEM;
1609f6bc909eSSimon Trimmer 				goto out_fw;
1610f6bc909eSSimon Trimmer 			}
1611f6bc909eSSimon Trimmer 
1612f6bc909eSSimon Trimmer 			ret = regmap_raw_write_async(regmap, reg, buf->buf,
1613f6bc909eSSimon Trimmer 						     le32_to_cpu(region->len));
1614f6bc909eSSimon Trimmer 			if (ret != 0) {
1615f6bc909eSSimon Trimmer 				cs_dsp_err(dsp,
1616f6bc909eSSimon Trimmer 					   "%s.%d: Failed to write %d bytes at %d in %s: %d\n",
1617f6bc909eSSimon Trimmer 					   file, regions,
1618f6bc909eSSimon Trimmer 					   le32_to_cpu(region->len), offset,
1619f6bc909eSSimon Trimmer 					   region_name, ret);
1620f6bc909eSSimon Trimmer 				goto out_fw;
1621f6bc909eSSimon Trimmer 			}
1622f6bc909eSSimon Trimmer 		}
1623f6bc909eSSimon Trimmer 
1624f6bc909eSSimon Trimmer 		pos += le32_to_cpu(region->len) + sizeof(*region);
1625f6bc909eSSimon Trimmer 		regions++;
1626f6bc909eSSimon Trimmer 	}
1627f6bc909eSSimon Trimmer 
1628f6bc909eSSimon Trimmer 	ret = regmap_async_complete(regmap);
1629f6bc909eSSimon Trimmer 	if (ret != 0) {
1630f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to complete async write: %d\n", ret);
1631f6bc909eSSimon Trimmer 		goto out_fw;
1632f6bc909eSSimon Trimmer 	}
1633f6bc909eSSimon Trimmer 
1634f6bc909eSSimon Trimmer 	if (pos > firmware->size)
1635f6bc909eSSimon Trimmer 		cs_dsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
1636f6bc909eSSimon Trimmer 			    file, regions, pos - firmware->size);
1637f6bc909eSSimon Trimmer 
1638f6bc909eSSimon Trimmer 	cs_dsp_debugfs_save_wmfwname(dsp, file);
1639f6bc909eSSimon Trimmer 
1640f6bc909eSSimon Trimmer out_fw:
1641f6bc909eSSimon Trimmer 	regmap_async_complete(regmap);
1642f6bc909eSSimon Trimmer 	cs_dsp_buf_free(&buf_list);
1643f6bc909eSSimon Trimmer 
16443019b86bSRichard Fitzgerald 	if (ret == -EOVERFLOW)
16453019b86bSRichard Fitzgerald 		cs_dsp_err(dsp, "%s: file content overflows file data\n", file);
16463019b86bSRichard Fitzgerald 
1647f6bc909eSSimon Trimmer 	return ret;
1648f6bc909eSSimon Trimmer }
1649f6bc909eSSimon Trimmer 
1650f6bc909eSSimon Trimmer /**
1651f6bc909eSSimon Trimmer  * cs_dsp_get_ctl() - Finds a matching coefficient control
1652f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
1653f6bc909eSSimon Trimmer  * @name: pointer to string to match with a control's subname
1654f6bc909eSSimon Trimmer  * @type: the algorithm type to match
1655f6bc909eSSimon Trimmer  * @alg: the algorithm id to match
1656f6bc909eSSimon Trimmer  *
1657f6bc909eSSimon Trimmer  * Find cs_dsp_coeff_ctl with input name as its subname
1658f6bc909eSSimon Trimmer  *
1659f6bc909eSSimon Trimmer  * Return: pointer to the control on success, NULL if not found
1660f6bc909eSSimon Trimmer  */
cs_dsp_get_ctl(struct cs_dsp * dsp,const char * name,int type,unsigned int alg)1661f6bc909eSSimon Trimmer struct cs_dsp_coeff_ctl *cs_dsp_get_ctl(struct cs_dsp *dsp, const char *name, int type,
1662f6bc909eSSimon Trimmer 					unsigned int alg)
1663f6bc909eSSimon Trimmer {
1664f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_ctl *pos, *rslt = NULL;
1665f6bc909eSSimon Trimmer 
16665065cfabSCharles Keepax 	lockdep_assert_held(&dsp->pwr_lock);
16675065cfabSCharles Keepax 
1668f6bc909eSSimon Trimmer 	list_for_each_entry(pos, &dsp->ctl_list, list) {
1669f6bc909eSSimon Trimmer 		if (!pos->subname)
1670f6bc909eSSimon Trimmer 			continue;
1671f6bc909eSSimon Trimmer 		if (strncmp(pos->subname, name, pos->subname_len) == 0 &&
1672f6bc909eSSimon Trimmer 		    pos->fw_name == dsp->fw_name &&
1673f6bc909eSSimon Trimmer 		    pos->alg_region.alg == alg &&
1674f6bc909eSSimon Trimmer 		    pos->alg_region.type == type) {
1675f6bc909eSSimon Trimmer 			rslt = pos;
1676f6bc909eSSimon Trimmer 			break;
1677f6bc909eSSimon Trimmer 		}
1678f6bc909eSSimon Trimmer 	}
1679f6bc909eSSimon Trimmer 
1680f6bc909eSSimon Trimmer 	return rslt;
1681f6bc909eSSimon Trimmer }
1682e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_get_ctl, FW_CS_DSP);
1683f6bc909eSSimon Trimmer 
cs_dsp_ctl_fixup_base(struct cs_dsp * dsp,const struct cs_dsp_alg_region * alg_region)1684f6bc909eSSimon Trimmer static void cs_dsp_ctl_fixup_base(struct cs_dsp *dsp,
1685f6bc909eSSimon Trimmer 				  const struct cs_dsp_alg_region *alg_region)
1686f6bc909eSSimon Trimmer {
1687f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_ctl *ctl;
1688f6bc909eSSimon Trimmer 
1689f6bc909eSSimon Trimmer 	list_for_each_entry(ctl, &dsp->ctl_list, list) {
1690f6bc909eSSimon Trimmer 		if (ctl->fw_name == dsp->fw_name &&
1691f6bc909eSSimon Trimmer 		    alg_region->alg == ctl->alg_region.alg &&
1692f6bc909eSSimon Trimmer 		    alg_region->type == ctl->alg_region.type) {
1693f6bc909eSSimon Trimmer 			ctl->alg_region.base = alg_region->base;
1694f6bc909eSSimon Trimmer 		}
1695f6bc909eSSimon Trimmer 	}
1696f6bc909eSSimon Trimmer }
1697f6bc909eSSimon Trimmer 
cs_dsp_read_algs(struct cs_dsp * dsp,size_t n_algs,const struct cs_dsp_region * mem,unsigned int pos,unsigned int len)1698f6bc909eSSimon Trimmer static void *cs_dsp_read_algs(struct cs_dsp *dsp, size_t n_algs,
1699f6bc909eSSimon Trimmer 			      const struct cs_dsp_region *mem,
1700f6bc909eSSimon Trimmer 			      unsigned int pos, unsigned int len)
1701f6bc909eSSimon Trimmer {
1702f6bc909eSSimon Trimmer 	void *alg;
1703f6bc909eSSimon Trimmer 	unsigned int reg;
1704f6bc909eSSimon Trimmer 	int ret;
1705f6bc909eSSimon Trimmer 	__be32 val;
1706f6bc909eSSimon Trimmer 
1707f6bc909eSSimon Trimmer 	if (n_algs == 0) {
1708f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "No algorithms\n");
1709f6bc909eSSimon Trimmer 		return ERR_PTR(-EINVAL);
1710f6bc909eSSimon Trimmer 	}
1711f6bc909eSSimon Trimmer 
1712f6bc909eSSimon Trimmer 	if (n_algs > 1024) {
1713f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Algorithm count %zx excessive\n", n_algs);
1714f6bc909eSSimon Trimmer 		return ERR_PTR(-EINVAL);
1715f6bc909eSSimon Trimmer 	}
1716f6bc909eSSimon Trimmer 
1717f6bc909eSSimon Trimmer 	/* Read the terminator first to validate the length */
1718f6bc909eSSimon Trimmer 	reg = dsp->ops->region_to_reg(mem, pos + len);
1719f6bc909eSSimon Trimmer 
1720f6bc909eSSimon Trimmer 	ret = regmap_raw_read(dsp->regmap, reg, &val, sizeof(val));
1721f6bc909eSSimon Trimmer 	if (ret != 0) {
1722f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to read algorithm list end: %d\n",
1723f6bc909eSSimon Trimmer 			   ret);
1724f6bc909eSSimon Trimmer 		return ERR_PTR(ret);
1725f6bc909eSSimon Trimmer 	}
1726f6bc909eSSimon Trimmer 
1727f6bc909eSSimon Trimmer 	if (be32_to_cpu(val) != 0xbedead)
1728f6bc909eSSimon Trimmer 		cs_dsp_warn(dsp, "Algorithm list end %x 0x%x != 0xbedead\n",
1729f6bc909eSSimon Trimmer 			    reg, be32_to_cpu(val));
1730f6bc909eSSimon Trimmer 
1731f6bc909eSSimon Trimmer 	/* Convert length from DSP words to bytes */
1732f6bc909eSSimon Trimmer 	len *= sizeof(u32);
1733f6bc909eSSimon Trimmer 
1734f6bc909eSSimon Trimmer 	alg = kzalloc(len, GFP_KERNEL | GFP_DMA);
1735f6bc909eSSimon Trimmer 	if (!alg)
1736f6bc909eSSimon Trimmer 		return ERR_PTR(-ENOMEM);
1737f6bc909eSSimon Trimmer 
1738f6bc909eSSimon Trimmer 	reg = dsp->ops->region_to_reg(mem, pos);
1739f6bc909eSSimon Trimmer 
1740f6bc909eSSimon Trimmer 	ret = regmap_raw_read(dsp->regmap, reg, alg, len);
1741f6bc909eSSimon Trimmer 	if (ret != 0) {
1742f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to read algorithm list: %d\n", ret);
1743f6bc909eSSimon Trimmer 		kfree(alg);
1744f6bc909eSSimon Trimmer 		return ERR_PTR(ret);
1745f6bc909eSSimon Trimmer 	}
1746f6bc909eSSimon Trimmer 
1747f6bc909eSSimon Trimmer 	return alg;
1748f6bc909eSSimon Trimmer }
1749f6bc909eSSimon Trimmer 
1750f6bc909eSSimon Trimmer /**
1751f6bc909eSSimon Trimmer  * cs_dsp_find_alg_region() - Finds a matching algorithm region
1752f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
1753f6bc909eSSimon Trimmer  * @type: the algorithm type to match
1754f6bc909eSSimon Trimmer  * @id: the algorithm id to match
1755f6bc909eSSimon Trimmer  *
1756f6bc909eSSimon Trimmer  * Return: Pointer to matching algorithm region, or NULL if not found.
1757f6bc909eSSimon Trimmer  */
cs_dsp_find_alg_region(struct cs_dsp * dsp,int type,unsigned int id)1758f6bc909eSSimon Trimmer struct cs_dsp_alg_region *cs_dsp_find_alg_region(struct cs_dsp *dsp,
1759f6bc909eSSimon Trimmer 						 int type, unsigned int id)
1760f6bc909eSSimon Trimmer {
1761f6bc909eSSimon Trimmer 	struct cs_dsp_alg_region *alg_region;
1762f6bc909eSSimon Trimmer 
17635065cfabSCharles Keepax 	lockdep_assert_held(&dsp->pwr_lock);
17645065cfabSCharles Keepax 
1765f6bc909eSSimon Trimmer 	list_for_each_entry(alg_region, &dsp->alg_regions, list) {
1766f6bc909eSSimon Trimmer 		if (id == alg_region->alg && type == alg_region->type)
1767f6bc909eSSimon Trimmer 			return alg_region;
1768f6bc909eSSimon Trimmer 	}
1769f6bc909eSSimon Trimmer 
1770f6bc909eSSimon Trimmer 	return NULL;
1771f6bc909eSSimon Trimmer }
1772e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_find_alg_region, FW_CS_DSP);
1773f6bc909eSSimon Trimmer 
cs_dsp_create_region(struct cs_dsp * dsp,int type,__be32 id,__be32 ver,__be32 base)1774f6bc909eSSimon Trimmer static struct cs_dsp_alg_region *cs_dsp_create_region(struct cs_dsp *dsp,
1775f6bc909eSSimon Trimmer 						      int type, __be32 id,
17762925748eSCharles Keepax 						      __be32 ver, __be32 base)
1777f6bc909eSSimon Trimmer {
1778f6bc909eSSimon Trimmer 	struct cs_dsp_alg_region *alg_region;
1779f6bc909eSSimon Trimmer 
1780f6bc909eSSimon Trimmer 	alg_region = kzalloc(sizeof(*alg_region), GFP_KERNEL);
1781f6bc909eSSimon Trimmer 	if (!alg_region)
1782f6bc909eSSimon Trimmer 		return ERR_PTR(-ENOMEM);
1783f6bc909eSSimon Trimmer 
1784f6bc909eSSimon Trimmer 	alg_region->type = type;
1785f6bc909eSSimon Trimmer 	alg_region->alg = be32_to_cpu(id);
17862925748eSCharles Keepax 	alg_region->ver = be32_to_cpu(ver);
1787f6bc909eSSimon Trimmer 	alg_region->base = be32_to_cpu(base);
1788f6bc909eSSimon Trimmer 
1789f6bc909eSSimon Trimmer 	list_add_tail(&alg_region->list, &dsp->alg_regions);
1790f6bc909eSSimon Trimmer 
1791*dc0e5ca8SRichard Fitzgerald 	if (dsp->wmfw_ver > 0)
1792f6bc909eSSimon Trimmer 		cs_dsp_ctl_fixup_base(dsp, alg_region);
1793f6bc909eSSimon Trimmer 
1794f6bc909eSSimon Trimmer 	return alg_region;
1795f6bc909eSSimon Trimmer }
1796f6bc909eSSimon Trimmer 
cs_dsp_free_alg_regions(struct cs_dsp * dsp)1797f6bc909eSSimon Trimmer static void cs_dsp_free_alg_regions(struct cs_dsp *dsp)
1798f6bc909eSSimon Trimmer {
1799f6bc909eSSimon Trimmer 	struct cs_dsp_alg_region *alg_region;
1800f6bc909eSSimon Trimmer 
1801f6bc909eSSimon Trimmer 	while (!list_empty(&dsp->alg_regions)) {
1802f6bc909eSSimon Trimmer 		alg_region = list_first_entry(&dsp->alg_regions,
1803f6bc909eSSimon Trimmer 					      struct cs_dsp_alg_region,
1804f6bc909eSSimon Trimmer 					      list);
1805f6bc909eSSimon Trimmer 		list_del(&alg_region->list);
1806f6bc909eSSimon Trimmer 		kfree(alg_region);
1807f6bc909eSSimon Trimmer 	}
1808f6bc909eSSimon Trimmer }
1809f6bc909eSSimon Trimmer 
cs_dsp_parse_wmfw_id_header(struct cs_dsp * dsp,struct wmfw_id_hdr * fw,int nalgs)1810f6bc909eSSimon Trimmer static void cs_dsp_parse_wmfw_id_header(struct cs_dsp *dsp,
1811f6bc909eSSimon Trimmer 					struct wmfw_id_hdr *fw, int nalgs)
1812f6bc909eSSimon Trimmer {
1813f6bc909eSSimon Trimmer 	dsp->fw_id = be32_to_cpu(fw->id);
1814f6bc909eSSimon Trimmer 	dsp->fw_id_version = be32_to_cpu(fw->ver);
1815f6bc909eSSimon Trimmer 
1816f6bc909eSSimon Trimmer 	cs_dsp_info(dsp, "Firmware: %x v%d.%d.%d, %d algorithms\n",
1817f6bc909eSSimon Trimmer 		    dsp->fw_id, (dsp->fw_id_version & 0xff0000) >> 16,
1818f6bc909eSSimon Trimmer 		    (dsp->fw_id_version & 0xff00) >> 8, dsp->fw_id_version & 0xff,
1819f6bc909eSSimon Trimmer 		    nalgs);
1820f6bc909eSSimon Trimmer }
1821f6bc909eSSimon Trimmer 
cs_dsp_parse_wmfw_v3_id_header(struct cs_dsp * dsp,struct wmfw_v3_id_hdr * fw,int nalgs)1822f6bc909eSSimon Trimmer static void cs_dsp_parse_wmfw_v3_id_header(struct cs_dsp *dsp,
1823f6bc909eSSimon Trimmer 					   struct wmfw_v3_id_hdr *fw, int nalgs)
1824f6bc909eSSimon Trimmer {
1825f6bc909eSSimon Trimmer 	dsp->fw_id = be32_to_cpu(fw->id);
1826f6bc909eSSimon Trimmer 	dsp->fw_id_version = be32_to_cpu(fw->ver);
1827f6bc909eSSimon Trimmer 	dsp->fw_vendor_id = be32_to_cpu(fw->vendor_id);
1828f6bc909eSSimon Trimmer 
1829f6bc909eSSimon Trimmer 	cs_dsp_info(dsp, "Firmware: %x vendor: 0x%x v%d.%d.%d, %d algorithms\n",
1830f6bc909eSSimon Trimmer 		    dsp->fw_id, dsp->fw_vendor_id,
1831f6bc909eSSimon Trimmer 		    (dsp->fw_id_version & 0xff0000) >> 16,
1832f6bc909eSSimon Trimmer 		    (dsp->fw_id_version & 0xff00) >> 8, dsp->fw_id_version & 0xff,
1833f6bc909eSSimon Trimmer 		    nalgs);
1834f6bc909eSSimon Trimmer }
1835f6bc909eSSimon Trimmer 
cs_dsp_create_regions(struct cs_dsp * dsp,__be32 id,__be32 ver,int nregions,const int * type,__be32 * base)18362925748eSCharles Keepax static int cs_dsp_create_regions(struct cs_dsp *dsp, __be32 id, __be32 ver,
18372925748eSCharles Keepax 				 int nregions, const int *type, __be32 *base)
1838f6bc909eSSimon Trimmer {
1839f6bc909eSSimon Trimmer 	struct cs_dsp_alg_region *alg_region;
1840f6bc909eSSimon Trimmer 	int i;
1841f6bc909eSSimon Trimmer 
1842f6bc909eSSimon Trimmer 	for (i = 0; i < nregions; i++) {
18432925748eSCharles Keepax 		alg_region = cs_dsp_create_region(dsp, type[i], id, ver, base[i]);
1844f6bc909eSSimon Trimmer 		if (IS_ERR(alg_region))
1845f6bc909eSSimon Trimmer 			return PTR_ERR(alg_region);
1846f6bc909eSSimon Trimmer 	}
1847f6bc909eSSimon Trimmer 
1848f6bc909eSSimon Trimmer 	return 0;
1849f6bc909eSSimon Trimmer }
1850f6bc909eSSimon Trimmer 
cs_dsp_adsp1_setup_algs(struct cs_dsp * dsp)1851f6bc909eSSimon Trimmer static int cs_dsp_adsp1_setup_algs(struct cs_dsp *dsp)
1852f6bc909eSSimon Trimmer {
1853f6bc909eSSimon Trimmer 	struct wmfw_adsp1_id_hdr adsp1_id;
1854f6bc909eSSimon Trimmer 	struct wmfw_adsp1_alg_hdr *adsp1_alg;
1855f6bc909eSSimon Trimmer 	struct cs_dsp_alg_region *alg_region;
1856f6bc909eSSimon Trimmer 	const struct cs_dsp_region *mem;
1857f6bc909eSSimon Trimmer 	unsigned int pos, len;
1858f6bc909eSSimon Trimmer 	size_t n_algs;
1859f6bc909eSSimon Trimmer 	int i, ret;
1860f6bc909eSSimon Trimmer 
1861f6bc909eSSimon Trimmer 	mem = cs_dsp_find_region(dsp, WMFW_ADSP1_DM);
1862f6bc909eSSimon Trimmer 	if (WARN_ON(!mem))
1863f6bc909eSSimon Trimmer 		return -EINVAL;
1864f6bc909eSSimon Trimmer 
1865f6bc909eSSimon Trimmer 	ret = regmap_raw_read(dsp->regmap, mem->base, &adsp1_id,
1866f6bc909eSSimon Trimmer 			      sizeof(adsp1_id));
1867f6bc909eSSimon Trimmer 	if (ret != 0) {
1868f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to read algorithm info: %d\n",
1869f6bc909eSSimon Trimmer 			   ret);
1870f6bc909eSSimon Trimmer 		return ret;
1871f6bc909eSSimon Trimmer 	}
1872f6bc909eSSimon Trimmer 
1873f6bc909eSSimon Trimmer 	n_algs = be32_to_cpu(adsp1_id.n_algs);
1874f6bc909eSSimon Trimmer 
1875f6bc909eSSimon Trimmer 	cs_dsp_parse_wmfw_id_header(dsp, &adsp1_id.fw, n_algs);
1876f6bc909eSSimon Trimmer 
1877f6bc909eSSimon Trimmer 	alg_region = cs_dsp_create_region(dsp, WMFW_ADSP1_ZM,
18782925748eSCharles Keepax 					  adsp1_id.fw.id, adsp1_id.fw.ver,
18792925748eSCharles Keepax 					  adsp1_id.zm);
1880f6bc909eSSimon Trimmer 	if (IS_ERR(alg_region))
1881f6bc909eSSimon Trimmer 		return PTR_ERR(alg_region);
1882f6bc909eSSimon Trimmer 
1883f6bc909eSSimon Trimmer 	alg_region = cs_dsp_create_region(dsp, WMFW_ADSP1_DM,
18842925748eSCharles Keepax 					  adsp1_id.fw.id, adsp1_id.fw.ver,
18852925748eSCharles Keepax 					  adsp1_id.dm);
1886f6bc909eSSimon Trimmer 	if (IS_ERR(alg_region))
1887f6bc909eSSimon Trimmer 		return PTR_ERR(alg_region);
1888f6bc909eSSimon Trimmer 
1889f6bc909eSSimon Trimmer 	/* Calculate offset and length in DSP words */
1890f6bc909eSSimon Trimmer 	pos = sizeof(adsp1_id) / sizeof(u32);
1891f6bc909eSSimon Trimmer 	len = (sizeof(*adsp1_alg) * n_algs) / sizeof(u32);
1892f6bc909eSSimon Trimmer 
1893f6bc909eSSimon Trimmer 	adsp1_alg = cs_dsp_read_algs(dsp, n_algs, mem, pos, len);
1894f6bc909eSSimon Trimmer 	if (IS_ERR(adsp1_alg))
1895f6bc909eSSimon Trimmer 		return PTR_ERR(adsp1_alg);
1896f6bc909eSSimon Trimmer 
1897f6bc909eSSimon Trimmer 	for (i = 0; i < n_algs; i++) {
1898f6bc909eSSimon Trimmer 		cs_dsp_info(dsp, "%d: ID %x v%d.%d.%d DM@%x ZM@%x\n",
1899f6bc909eSSimon Trimmer 			    i, be32_to_cpu(adsp1_alg[i].alg.id),
1900f6bc909eSSimon Trimmer 			    (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff0000) >> 16,
1901f6bc909eSSimon Trimmer 			    (be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff00) >> 8,
1902f6bc909eSSimon Trimmer 			    be32_to_cpu(adsp1_alg[i].alg.ver) & 0xff,
1903f6bc909eSSimon Trimmer 			    be32_to_cpu(adsp1_alg[i].dm),
1904f6bc909eSSimon Trimmer 			    be32_to_cpu(adsp1_alg[i].zm));
1905f6bc909eSSimon Trimmer 
1906f6bc909eSSimon Trimmer 		alg_region = cs_dsp_create_region(dsp, WMFW_ADSP1_DM,
1907f6bc909eSSimon Trimmer 						  adsp1_alg[i].alg.id,
19082925748eSCharles Keepax 						  adsp1_alg[i].alg.ver,
1909f6bc909eSSimon Trimmer 						  adsp1_alg[i].dm);
1910f6bc909eSSimon Trimmer 		if (IS_ERR(alg_region)) {
1911f6bc909eSSimon Trimmer 			ret = PTR_ERR(alg_region);
1912f6bc909eSSimon Trimmer 			goto out;
1913f6bc909eSSimon Trimmer 		}
1914*dc0e5ca8SRichard Fitzgerald 		if (dsp->wmfw_ver == 0) {
1915f6bc909eSSimon Trimmer 			if (i + 1 < n_algs) {
1916f6bc909eSSimon Trimmer 				len = be32_to_cpu(adsp1_alg[i + 1].dm);
1917f6bc909eSSimon Trimmer 				len -= be32_to_cpu(adsp1_alg[i].dm);
1918f6bc909eSSimon Trimmer 				len *= 4;
1919f6bc909eSSimon Trimmer 				cs_dsp_create_control(dsp, alg_region, 0,
1920f6bc909eSSimon Trimmer 						      len, NULL, 0, 0,
1921f6bc909eSSimon Trimmer 						      WMFW_CTL_TYPE_BYTES);
1922f6bc909eSSimon Trimmer 			} else {
1923f6bc909eSSimon Trimmer 				cs_dsp_warn(dsp, "Missing length info for region DM with ID %x\n",
1924f6bc909eSSimon Trimmer 					    be32_to_cpu(adsp1_alg[i].alg.id));
1925f6bc909eSSimon Trimmer 			}
1926f6bc909eSSimon Trimmer 		}
1927f6bc909eSSimon Trimmer 
1928f6bc909eSSimon Trimmer 		alg_region = cs_dsp_create_region(dsp, WMFW_ADSP1_ZM,
1929f6bc909eSSimon Trimmer 						  adsp1_alg[i].alg.id,
19302925748eSCharles Keepax 						  adsp1_alg[i].alg.ver,
1931f6bc909eSSimon Trimmer 						  adsp1_alg[i].zm);
1932f6bc909eSSimon Trimmer 		if (IS_ERR(alg_region)) {
1933f6bc909eSSimon Trimmer 			ret = PTR_ERR(alg_region);
1934f6bc909eSSimon Trimmer 			goto out;
1935f6bc909eSSimon Trimmer 		}
1936*dc0e5ca8SRichard Fitzgerald 		if (dsp->wmfw_ver == 0) {
1937f6bc909eSSimon Trimmer 			if (i + 1 < n_algs) {
1938f6bc909eSSimon Trimmer 				len = be32_to_cpu(adsp1_alg[i + 1].zm);
1939f6bc909eSSimon Trimmer 				len -= be32_to_cpu(adsp1_alg[i].zm);
1940f6bc909eSSimon Trimmer 				len *= 4;
1941f6bc909eSSimon Trimmer 				cs_dsp_create_control(dsp, alg_region, 0,
1942f6bc909eSSimon Trimmer 						      len, NULL, 0, 0,
1943f6bc909eSSimon Trimmer 						      WMFW_CTL_TYPE_BYTES);
1944f6bc909eSSimon Trimmer 			} else {
1945f6bc909eSSimon Trimmer 				cs_dsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
1946f6bc909eSSimon Trimmer 					    be32_to_cpu(adsp1_alg[i].alg.id));
1947f6bc909eSSimon Trimmer 			}
1948f6bc909eSSimon Trimmer 		}
1949f6bc909eSSimon Trimmer 	}
1950f6bc909eSSimon Trimmer 
1951f6bc909eSSimon Trimmer out:
1952f6bc909eSSimon Trimmer 	kfree(adsp1_alg);
1953f6bc909eSSimon Trimmer 	return ret;
1954f6bc909eSSimon Trimmer }
1955f6bc909eSSimon Trimmer 
cs_dsp_adsp2_setup_algs(struct cs_dsp * dsp)1956f6bc909eSSimon Trimmer static int cs_dsp_adsp2_setup_algs(struct cs_dsp *dsp)
1957f6bc909eSSimon Trimmer {
1958f6bc909eSSimon Trimmer 	struct wmfw_adsp2_id_hdr adsp2_id;
1959f6bc909eSSimon Trimmer 	struct wmfw_adsp2_alg_hdr *adsp2_alg;
1960f6bc909eSSimon Trimmer 	struct cs_dsp_alg_region *alg_region;
1961f6bc909eSSimon Trimmer 	const struct cs_dsp_region *mem;
1962f6bc909eSSimon Trimmer 	unsigned int pos, len;
1963f6bc909eSSimon Trimmer 	size_t n_algs;
1964f6bc909eSSimon Trimmer 	int i, ret;
1965f6bc909eSSimon Trimmer 
1966f6bc909eSSimon Trimmer 	mem = cs_dsp_find_region(dsp, WMFW_ADSP2_XM);
1967f6bc909eSSimon Trimmer 	if (WARN_ON(!mem))
1968f6bc909eSSimon Trimmer 		return -EINVAL;
1969f6bc909eSSimon Trimmer 
1970f6bc909eSSimon Trimmer 	ret = regmap_raw_read(dsp->regmap, mem->base, &adsp2_id,
1971f6bc909eSSimon Trimmer 			      sizeof(adsp2_id));
1972f6bc909eSSimon Trimmer 	if (ret != 0) {
1973f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to read algorithm info: %d\n",
1974f6bc909eSSimon Trimmer 			   ret);
1975f6bc909eSSimon Trimmer 		return ret;
1976f6bc909eSSimon Trimmer 	}
1977f6bc909eSSimon Trimmer 
1978f6bc909eSSimon Trimmer 	n_algs = be32_to_cpu(adsp2_id.n_algs);
1979f6bc909eSSimon Trimmer 
1980f6bc909eSSimon Trimmer 	cs_dsp_parse_wmfw_id_header(dsp, &adsp2_id.fw, n_algs);
1981f6bc909eSSimon Trimmer 
1982f6bc909eSSimon Trimmer 	alg_region = cs_dsp_create_region(dsp, WMFW_ADSP2_XM,
19832925748eSCharles Keepax 					  adsp2_id.fw.id, adsp2_id.fw.ver,
19842925748eSCharles Keepax 					  adsp2_id.xm);
1985f6bc909eSSimon Trimmer 	if (IS_ERR(alg_region))
1986f6bc909eSSimon Trimmer 		return PTR_ERR(alg_region);
1987f6bc909eSSimon Trimmer 
1988f6bc909eSSimon Trimmer 	alg_region = cs_dsp_create_region(dsp, WMFW_ADSP2_YM,
19892925748eSCharles Keepax 					  adsp2_id.fw.id, adsp2_id.fw.ver,
19902925748eSCharles Keepax 					  adsp2_id.ym);
1991f6bc909eSSimon Trimmer 	if (IS_ERR(alg_region))
1992f6bc909eSSimon Trimmer 		return PTR_ERR(alg_region);
1993f6bc909eSSimon Trimmer 
1994f6bc909eSSimon Trimmer 	alg_region = cs_dsp_create_region(dsp, WMFW_ADSP2_ZM,
19952925748eSCharles Keepax 					  adsp2_id.fw.id, adsp2_id.fw.ver,
19962925748eSCharles Keepax 					  adsp2_id.zm);
1997f6bc909eSSimon Trimmer 	if (IS_ERR(alg_region))
1998f6bc909eSSimon Trimmer 		return PTR_ERR(alg_region);
1999f6bc909eSSimon Trimmer 
2000f6bc909eSSimon Trimmer 	/* Calculate offset and length in DSP words */
2001f6bc909eSSimon Trimmer 	pos = sizeof(adsp2_id) / sizeof(u32);
2002f6bc909eSSimon Trimmer 	len = (sizeof(*adsp2_alg) * n_algs) / sizeof(u32);
2003f6bc909eSSimon Trimmer 
2004f6bc909eSSimon Trimmer 	adsp2_alg = cs_dsp_read_algs(dsp, n_algs, mem, pos, len);
2005f6bc909eSSimon Trimmer 	if (IS_ERR(adsp2_alg))
2006f6bc909eSSimon Trimmer 		return PTR_ERR(adsp2_alg);
2007f6bc909eSSimon Trimmer 
2008f6bc909eSSimon Trimmer 	for (i = 0; i < n_algs; i++) {
200969343ce9SRichard Fitzgerald 		cs_dsp_dbg(dsp,
2010f6bc909eSSimon Trimmer 			   "%d: ID %x v%d.%d.%d XM@%x YM@%x ZM@%x\n",
2011f6bc909eSSimon Trimmer 			   i, be32_to_cpu(adsp2_alg[i].alg.id),
2012f6bc909eSSimon Trimmer 			   (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff0000) >> 16,
2013f6bc909eSSimon Trimmer 			   (be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff00) >> 8,
2014f6bc909eSSimon Trimmer 			   be32_to_cpu(adsp2_alg[i].alg.ver) & 0xff,
2015f6bc909eSSimon Trimmer 			   be32_to_cpu(adsp2_alg[i].xm),
2016f6bc909eSSimon Trimmer 			   be32_to_cpu(adsp2_alg[i].ym),
2017f6bc909eSSimon Trimmer 			   be32_to_cpu(adsp2_alg[i].zm));
2018f6bc909eSSimon Trimmer 
2019f6bc909eSSimon Trimmer 		alg_region = cs_dsp_create_region(dsp, WMFW_ADSP2_XM,
2020f6bc909eSSimon Trimmer 						  adsp2_alg[i].alg.id,
20212925748eSCharles Keepax 						  adsp2_alg[i].alg.ver,
2022f6bc909eSSimon Trimmer 						  adsp2_alg[i].xm);
2023f6bc909eSSimon Trimmer 		if (IS_ERR(alg_region)) {
2024f6bc909eSSimon Trimmer 			ret = PTR_ERR(alg_region);
2025f6bc909eSSimon Trimmer 			goto out;
2026f6bc909eSSimon Trimmer 		}
2027*dc0e5ca8SRichard Fitzgerald 		if (dsp->wmfw_ver == 0) {
2028f6bc909eSSimon Trimmer 			if (i + 1 < n_algs) {
2029f6bc909eSSimon Trimmer 				len = be32_to_cpu(adsp2_alg[i + 1].xm);
2030f6bc909eSSimon Trimmer 				len -= be32_to_cpu(adsp2_alg[i].xm);
2031f6bc909eSSimon Trimmer 				len *= 4;
2032f6bc909eSSimon Trimmer 				cs_dsp_create_control(dsp, alg_region, 0,
2033f6bc909eSSimon Trimmer 						      len, NULL, 0, 0,
2034f6bc909eSSimon Trimmer 						      WMFW_CTL_TYPE_BYTES);
2035f6bc909eSSimon Trimmer 			} else {
2036f6bc909eSSimon Trimmer 				cs_dsp_warn(dsp, "Missing length info for region XM with ID %x\n",
2037f6bc909eSSimon Trimmer 					    be32_to_cpu(adsp2_alg[i].alg.id));
2038f6bc909eSSimon Trimmer 			}
2039f6bc909eSSimon Trimmer 		}
2040f6bc909eSSimon Trimmer 
2041f6bc909eSSimon Trimmer 		alg_region = cs_dsp_create_region(dsp, WMFW_ADSP2_YM,
2042f6bc909eSSimon Trimmer 						  adsp2_alg[i].alg.id,
20432925748eSCharles Keepax 						  adsp2_alg[i].alg.ver,
2044f6bc909eSSimon Trimmer 						  adsp2_alg[i].ym);
2045f6bc909eSSimon Trimmer 		if (IS_ERR(alg_region)) {
2046f6bc909eSSimon Trimmer 			ret = PTR_ERR(alg_region);
2047f6bc909eSSimon Trimmer 			goto out;
2048f6bc909eSSimon Trimmer 		}
2049*dc0e5ca8SRichard Fitzgerald 		if (dsp->wmfw_ver == 0) {
2050f6bc909eSSimon Trimmer 			if (i + 1 < n_algs) {
2051f6bc909eSSimon Trimmer 				len = be32_to_cpu(adsp2_alg[i + 1].ym);
2052f6bc909eSSimon Trimmer 				len -= be32_to_cpu(adsp2_alg[i].ym);
2053f6bc909eSSimon Trimmer 				len *= 4;
2054f6bc909eSSimon Trimmer 				cs_dsp_create_control(dsp, alg_region, 0,
2055f6bc909eSSimon Trimmer 						      len, NULL, 0, 0,
2056f6bc909eSSimon Trimmer 						      WMFW_CTL_TYPE_BYTES);
2057f6bc909eSSimon Trimmer 			} else {
2058f6bc909eSSimon Trimmer 				cs_dsp_warn(dsp, "Missing length info for region YM with ID %x\n",
2059f6bc909eSSimon Trimmer 					    be32_to_cpu(adsp2_alg[i].alg.id));
2060f6bc909eSSimon Trimmer 			}
2061f6bc909eSSimon Trimmer 		}
2062f6bc909eSSimon Trimmer 
2063f6bc909eSSimon Trimmer 		alg_region = cs_dsp_create_region(dsp, WMFW_ADSP2_ZM,
2064f6bc909eSSimon Trimmer 						  adsp2_alg[i].alg.id,
20652925748eSCharles Keepax 						  adsp2_alg[i].alg.ver,
2066f6bc909eSSimon Trimmer 						  adsp2_alg[i].zm);
2067f6bc909eSSimon Trimmer 		if (IS_ERR(alg_region)) {
2068f6bc909eSSimon Trimmer 			ret = PTR_ERR(alg_region);
2069f6bc909eSSimon Trimmer 			goto out;
2070f6bc909eSSimon Trimmer 		}
2071*dc0e5ca8SRichard Fitzgerald 		if (dsp->wmfw_ver == 0) {
2072f6bc909eSSimon Trimmer 			if (i + 1 < n_algs) {
2073f6bc909eSSimon Trimmer 				len = be32_to_cpu(adsp2_alg[i + 1].zm);
2074f6bc909eSSimon Trimmer 				len -= be32_to_cpu(adsp2_alg[i].zm);
2075f6bc909eSSimon Trimmer 				len *= 4;
2076f6bc909eSSimon Trimmer 				cs_dsp_create_control(dsp, alg_region, 0,
2077f6bc909eSSimon Trimmer 						      len, NULL, 0, 0,
2078f6bc909eSSimon Trimmer 						      WMFW_CTL_TYPE_BYTES);
2079f6bc909eSSimon Trimmer 			} else {
2080f6bc909eSSimon Trimmer 				cs_dsp_warn(dsp, "Missing length info for region ZM with ID %x\n",
2081f6bc909eSSimon Trimmer 					    be32_to_cpu(adsp2_alg[i].alg.id));
2082f6bc909eSSimon Trimmer 			}
2083f6bc909eSSimon Trimmer 		}
2084f6bc909eSSimon Trimmer 	}
2085f6bc909eSSimon Trimmer 
2086f6bc909eSSimon Trimmer out:
2087f6bc909eSSimon Trimmer 	kfree(adsp2_alg);
2088f6bc909eSSimon Trimmer 	return ret;
2089f6bc909eSSimon Trimmer }
2090f6bc909eSSimon Trimmer 
cs_dsp_halo_create_regions(struct cs_dsp * dsp,__be32 id,__be32 ver,__be32 xm_base,__be32 ym_base)20912925748eSCharles Keepax static int cs_dsp_halo_create_regions(struct cs_dsp *dsp, __be32 id, __be32 ver,
2092f6bc909eSSimon Trimmer 				      __be32 xm_base, __be32 ym_base)
2093f6bc909eSSimon Trimmer {
2094f6bc909eSSimon Trimmer 	static const int types[] = {
2095f6bc909eSSimon Trimmer 		WMFW_ADSP2_XM, WMFW_HALO_XM_PACKED,
2096f6bc909eSSimon Trimmer 		WMFW_ADSP2_YM, WMFW_HALO_YM_PACKED
2097f6bc909eSSimon Trimmer 	};
2098f6bc909eSSimon Trimmer 	__be32 bases[] = { xm_base, xm_base, ym_base, ym_base };
2099f6bc909eSSimon Trimmer 
21002925748eSCharles Keepax 	return cs_dsp_create_regions(dsp, id, ver, ARRAY_SIZE(types), types, bases);
2101f6bc909eSSimon Trimmer }
2102f6bc909eSSimon Trimmer 
cs_dsp_halo_setup_algs(struct cs_dsp * dsp)2103f6bc909eSSimon Trimmer static int cs_dsp_halo_setup_algs(struct cs_dsp *dsp)
2104f6bc909eSSimon Trimmer {
2105f6bc909eSSimon Trimmer 	struct wmfw_halo_id_hdr halo_id;
2106f6bc909eSSimon Trimmer 	struct wmfw_halo_alg_hdr *halo_alg;
2107f6bc909eSSimon Trimmer 	const struct cs_dsp_region *mem;
2108f6bc909eSSimon Trimmer 	unsigned int pos, len;
2109f6bc909eSSimon Trimmer 	size_t n_algs;
2110f6bc909eSSimon Trimmer 	int i, ret;
2111f6bc909eSSimon Trimmer 
2112f6bc909eSSimon Trimmer 	mem = cs_dsp_find_region(dsp, WMFW_ADSP2_XM);
2113f6bc909eSSimon Trimmer 	if (WARN_ON(!mem))
2114f6bc909eSSimon Trimmer 		return -EINVAL;
2115f6bc909eSSimon Trimmer 
2116f6bc909eSSimon Trimmer 	ret = regmap_raw_read(dsp->regmap, mem->base, &halo_id,
2117f6bc909eSSimon Trimmer 			      sizeof(halo_id));
2118f6bc909eSSimon Trimmer 	if (ret != 0) {
2119f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to read algorithm info: %d\n",
2120f6bc909eSSimon Trimmer 			   ret);
2121f6bc909eSSimon Trimmer 		return ret;
2122f6bc909eSSimon Trimmer 	}
2123f6bc909eSSimon Trimmer 
2124f6bc909eSSimon Trimmer 	n_algs = be32_to_cpu(halo_id.n_algs);
2125f6bc909eSSimon Trimmer 
2126f6bc909eSSimon Trimmer 	cs_dsp_parse_wmfw_v3_id_header(dsp, &halo_id.fw, n_algs);
2127f6bc909eSSimon Trimmer 
21282925748eSCharles Keepax 	ret = cs_dsp_halo_create_regions(dsp, halo_id.fw.id, halo_id.fw.ver,
2129f6bc909eSSimon Trimmer 					 halo_id.xm_base, halo_id.ym_base);
2130f6bc909eSSimon Trimmer 	if (ret)
2131f6bc909eSSimon Trimmer 		return ret;
2132f6bc909eSSimon Trimmer 
2133f6bc909eSSimon Trimmer 	/* Calculate offset and length in DSP words */
2134f6bc909eSSimon Trimmer 	pos = sizeof(halo_id) / sizeof(u32);
2135f6bc909eSSimon Trimmer 	len = (sizeof(*halo_alg) * n_algs) / sizeof(u32);
2136f6bc909eSSimon Trimmer 
2137f6bc909eSSimon Trimmer 	halo_alg = cs_dsp_read_algs(dsp, n_algs, mem, pos, len);
2138f6bc909eSSimon Trimmer 	if (IS_ERR(halo_alg))
2139f6bc909eSSimon Trimmer 		return PTR_ERR(halo_alg);
2140f6bc909eSSimon Trimmer 
2141f6bc909eSSimon Trimmer 	for (i = 0; i < n_algs; i++) {
214269343ce9SRichard Fitzgerald 		cs_dsp_dbg(dsp,
2143f6bc909eSSimon Trimmer 			   "%d: ID %x v%d.%d.%d XM@%x YM@%x\n",
2144f6bc909eSSimon Trimmer 			   i, be32_to_cpu(halo_alg[i].alg.id),
2145f6bc909eSSimon Trimmer 			   (be32_to_cpu(halo_alg[i].alg.ver) & 0xff0000) >> 16,
2146f6bc909eSSimon Trimmer 			   (be32_to_cpu(halo_alg[i].alg.ver) & 0xff00) >> 8,
2147f6bc909eSSimon Trimmer 			   be32_to_cpu(halo_alg[i].alg.ver) & 0xff,
2148f6bc909eSSimon Trimmer 			   be32_to_cpu(halo_alg[i].xm_base),
2149f6bc909eSSimon Trimmer 			   be32_to_cpu(halo_alg[i].ym_base));
2150f6bc909eSSimon Trimmer 
2151f6bc909eSSimon Trimmer 		ret = cs_dsp_halo_create_regions(dsp, halo_alg[i].alg.id,
21522925748eSCharles Keepax 						 halo_alg[i].alg.ver,
2153f6bc909eSSimon Trimmer 						 halo_alg[i].xm_base,
2154f6bc909eSSimon Trimmer 						 halo_alg[i].ym_base);
2155f6bc909eSSimon Trimmer 		if (ret)
2156f6bc909eSSimon Trimmer 			goto out;
2157f6bc909eSSimon Trimmer 	}
2158f6bc909eSSimon Trimmer 
2159f6bc909eSSimon Trimmer out:
2160f6bc909eSSimon Trimmer 	kfree(halo_alg);
2161f6bc909eSSimon Trimmer 	return ret;
2162f6bc909eSSimon Trimmer }
2163f6bc909eSSimon Trimmer 
cs_dsp_load_coeff(struct cs_dsp * dsp,const struct firmware * firmware,const char * file)2164f6bc909eSSimon Trimmer static int cs_dsp_load_coeff(struct cs_dsp *dsp, const struct firmware *firmware,
2165f6bc909eSSimon Trimmer 			     const char *file)
2166f6bc909eSSimon Trimmer {
2167f6bc909eSSimon Trimmer 	LIST_HEAD(buf_list);
2168f6bc909eSSimon Trimmer 	struct regmap *regmap = dsp->regmap;
2169f6bc909eSSimon Trimmer 	struct wmfw_coeff_hdr *hdr;
2170f6bc909eSSimon Trimmer 	struct wmfw_coeff_item *blk;
2171f6bc909eSSimon Trimmer 	const struct cs_dsp_region *mem;
2172f6bc909eSSimon Trimmer 	struct cs_dsp_alg_region *alg_region;
2173f6bc909eSSimon Trimmer 	const char *region_name;
21742925748eSCharles Keepax 	int ret, pos, blocks, type, offset, reg, version;
2175f6bc909eSSimon Trimmer 	struct cs_dsp_buf *buf;
2176f6bc909eSSimon Trimmer 
2177f6bc909eSSimon Trimmer 	if (!firmware)
2178f6bc909eSSimon Trimmer 		return 0;
2179f6bc909eSSimon Trimmer 
2180f6bc909eSSimon Trimmer 	ret = -EINVAL;
2181f6bc909eSSimon Trimmer 
2182f6bc909eSSimon Trimmer 	if (sizeof(*hdr) >= firmware->size) {
2183f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "%s: coefficient file too short, %zu bytes\n",
2184f6bc909eSSimon Trimmer 			   file, firmware->size);
2185f6bc909eSSimon Trimmer 		goto out_fw;
2186f6bc909eSSimon Trimmer 	}
2187f6bc909eSSimon Trimmer 
2188f6bc909eSSimon Trimmer 	hdr = (void *)&firmware->data[0];
2189f6bc909eSSimon Trimmer 	if (memcmp(hdr->magic, "WMDR", 4) != 0) {
2190f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "%s: invalid coefficient magic\n", file);
2191f6bc909eSSimon Trimmer 		goto out_fw;
2192f6bc909eSSimon Trimmer 	}
2193f6bc909eSSimon Trimmer 
2194f6bc909eSSimon Trimmer 	switch (be32_to_cpu(hdr->rev) & 0xff) {
2195f6bc909eSSimon Trimmer 	case 1:
2196dcee7676SCharles Keepax 	case 2:
2197f6bc909eSSimon Trimmer 		break;
2198f6bc909eSSimon Trimmer 	default:
2199f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "%s: Unsupported coefficient file format %d\n",
2200f6bc909eSSimon Trimmer 			   file, be32_to_cpu(hdr->rev) & 0xff);
2201f6bc909eSSimon Trimmer 		ret = -EINVAL;
2202f6bc909eSSimon Trimmer 		goto out_fw;
2203f6bc909eSSimon Trimmer 	}
2204f6bc909eSSimon Trimmer 
22058f4007e8SRichard Fitzgerald 	cs_dsp_info(dsp, "%s: v%d.%d.%d\n", file,
2206f6bc909eSSimon Trimmer 		    (le32_to_cpu(hdr->ver) >> 16) & 0xff,
2207f6bc909eSSimon Trimmer 		    (le32_to_cpu(hdr->ver) >>  8) & 0xff,
2208f6bc909eSSimon Trimmer 		    le32_to_cpu(hdr->ver) & 0xff);
2209f6bc909eSSimon Trimmer 
2210f6bc909eSSimon Trimmer 	pos = le32_to_cpu(hdr->len);
2211f6bc909eSSimon Trimmer 
2212f6bc909eSSimon Trimmer 	blocks = 0;
2213959fe01eSRichard Fitzgerald 	while (pos < firmware->size) {
2214959fe01eSRichard Fitzgerald 		/* Is there enough data for a complete block header? */
2215959fe01eSRichard Fitzgerald 		if (sizeof(*blk) > firmware->size - pos) {
2216959fe01eSRichard Fitzgerald 			ret = -EOVERFLOW;
2217959fe01eSRichard Fitzgerald 			goto out_fw;
2218959fe01eSRichard Fitzgerald 		}
2219959fe01eSRichard Fitzgerald 
2220f6bc909eSSimon Trimmer 		blk = (void *)(&firmware->data[pos]);
2221f6bc909eSSimon Trimmer 
22226598afa9SRichard Fitzgerald 		if (le32_to_cpu(blk->len) > firmware->size - pos - sizeof(*blk)) {
22236598afa9SRichard Fitzgerald 			ret = -EOVERFLOW;
22246598afa9SRichard Fitzgerald 			goto out_fw;
22256598afa9SRichard Fitzgerald 		}
22266598afa9SRichard Fitzgerald 
2227f6bc909eSSimon Trimmer 		type = le16_to_cpu(blk->type);
2228f6bc909eSSimon Trimmer 		offset = le16_to_cpu(blk->offset);
22292925748eSCharles Keepax 		version = le32_to_cpu(blk->ver) >> 8;
2230f6bc909eSSimon Trimmer 
2231f6bc909eSSimon Trimmer 		cs_dsp_dbg(dsp, "%s.%d: %x v%d.%d.%d\n",
2232f6bc909eSSimon Trimmer 			   file, blocks, le32_to_cpu(blk->id),
2233f6bc909eSSimon Trimmer 			   (le32_to_cpu(blk->ver) >> 16) & 0xff,
2234f6bc909eSSimon Trimmer 			   (le32_to_cpu(blk->ver) >>  8) & 0xff,
2235f6bc909eSSimon Trimmer 			   le32_to_cpu(blk->ver) & 0xff);
2236f6bc909eSSimon Trimmer 		cs_dsp_dbg(dsp, "%s.%d: %d bytes at 0x%x in %x\n",
2237f6bc909eSSimon Trimmer 			   file, blocks, le32_to_cpu(blk->len), offset, type);
2238f6bc909eSSimon Trimmer 
2239f6bc909eSSimon Trimmer 		reg = 0;
2240f6bc909eSSimon Trimmer 		region_name = "Unknown";
2241f6bc909eSSimon Trimmer 		switch (type) {
2242f6bc909eSSimon Trimmer 		case (WMFW_NAME_TEXT << 8):
2243bff92858SRichard Fitzgerald 			cs_dsp_info(dsp, "%s: %.*s\n", dsp->fw_name,
2244bff92858SRichard Fitzgerald 				    min(le32_to_cpu(blk->len), 100), blk->data);
224540a34ae7SCharles Keepax 			break;
2246f6bc909eSSimon Trimmer 		case (WMFW_INFO_TEXT << 8):
2247f6bc909eSSimon Trimmer 		case (WMFW_METADATA << 8):
2248f6bc909eSSimon Trimmer 			break;
2249f6bc909eSSimon Trimmer 		case (WMFW_ABSOLUTE << 8):
2250f6bc909eSSimon Trimmer 			/*
2251f6bc909eSSimon Trimmer 			 * Old files may use this for global
2252f6bc909eSSimon Trimmer 			 * coefficients.
2253f6bc909eSSimon Trimmer 			 */
2254f6bc909eSSimon Trimmer 			if (le32_to_cpu(blk->id) == dsp->fw_id &&
2255f6bc909eSSimon Trimmer 			    offset == 0) {
2256f6bc909eSSimon Trimmer 				region_name = "global coefficients";
2257f6bc909eSSimon Trimmer 				mem = cs_dsp_find_region(dsp, type);
2258f6bc909eSSimon Trimmer 				if (!mem) {
2259f6bc909eSSimon Trimmer 					cs_dsp_err(dsp, "No ZM\n");
2260f6bc909eSSimon Trimmer 					break;
2261f6bc909eSSimon Trimmer 				}
2262f6bc909eSSimon Trimmer 				reg = dsp->ops->region_to_reg(mem, 0);
2263f6bc909eSSimon Trimmer 
2264f6bc909eSSimon Trimmer 			} else {
2265f6bc909eSSimon Trimmer 				region_name = "register";
2266f6bc909eSSimon Trimmer 				reg = offset;
2267f6bc909eSSimon Trimmer 			}
2268f6bc909eSSimon Trimmer 			break;
2269f6bc909eSSimon Trimmer 
2270f6bc909eSSimon Trimmer 		case WMFW_ADSP1_DM:
2271f6bc909eSSimon Trimmer 		case WMFW_ADSP1_ZM:
2272f6bc909eSSimon Trimmer 		case WMFW_ADSP2_XM:
2273f6bc909eSSimon Trimmer 		case WMFW_ADSP2_YM:
2274f6bc909eSSimon Trimmer 		case WMFW_HALO_XM_PACKED:
2275f6bc909eSSimon Trimmer 		case WMFW_HALO_YM_PACKED:
2276f6bc909eSSimon Trimmer 		case WMFW_HALO_PM_PACKED:
2277f6bc909eSSimon Trimmer 			cs_dsp_dbg(dsp, "%s.%d: %d bytes in %x for %x\n",
2278f6bc909eSSimon Trimmer 				   file, blocks, le32_to_cpu(blk->len),
2279f6bc909eSSimon Trimmer 				   type, le32_to_cpu(blk->id));
2280f6bc909eSSimon Trimmer 
2281ad249195SRichard Fitzgerald 			region_name = cs_dsp_mem_region_name(type);
2282f6bc909eSSimon Trimmer 			mem = cs_dsp_find_region(dsp, type);
2283f6bc909eSSimon Trimmer 			if (!mem) {
2284f6bc909eSSimon Trimmer 				cs_dsp_err(dsp, "No base for region %x\n", type);
2285f6bc909eSSimon Trimmer 				break;
2286f6bc909eSSimon Trimmer 			}
2287f6bc909eSSimon Trimmer 
2288f6bc909eSSimon Trimmer 			alg_region = cs_dsp_find_alg_region(dsp, type,
2289f6bc909eSSimon Trimmer 							    le32_to_cpu(blk->id));
2290f6bc909eSSimon Trimmer 			if (alg_region) {
22912925748eSCharles Keepax 				if (version != alg_region->ver)
22922925748eSCharles Keepax 					cs_dsp_warn(dsp,
22932925748eSCharles Keepax 						    "Algorithm coefficient version %d.%d.%d but expected %d.%d.%d\n",
22942925748eSCharles Keepax 						   (version >> 16) & 0xFF,
22952925748eSCharles Keepax 						   (version >> 8) & 0xFF,
22962925748eSCharles Keepax 						   version & 0xFF,
22972925748eSCharles Keepax 						   (alg_region->ver >> 16) & 0xFF,
22982925748eSCharles Keepax 						   (alg_region->ver >> 8) & 0xFF,
22992925748eSCharles Keepax 						   alg_region->ver & 0xFF);
23002925748eSCharles Keepax 
2301f6bc909eSSimon Trimmer 				reg = alg_region->base;
2302f6bc909eSSimon Trimmer 				reg = dsp->ops->region_to_reg(mem, reg);
2303f6bc909eSSimon Trimmer 				reg += offset;
2304f6bc909eSSimon Trimmer 			} else {
2305ad249195SRichard Fitzgerald 				cs_dsp_err(dsp, "No %s for algorithm %x\n",
2306ad249195SRichard Fitzgerald 					   region_name, le32_to_cpu(blk->id));
2307f6bc909eSSimon Trimmer 			}
2308f6bc909eSSimon Trimmer 			break;
2309f6bc909eSSimon Trimmer 
2310f6bc909eSSimon Trimmer 		default:
2311f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "%s.%d: Unknown region type %x at %d\n",
2312f6bc909eSSimon Trimmer 				   file, blocks, type, pos);
2313f6bc909eSSimon Trimmer 			break;
2314f6bc909eSSimon Trimmer 		}
2315f6bc909eSSimon Trimmer 
2316f6bc909eSSimon Trimmer 		if (reg) {
2317f6bc909eSSimon Trimmer 			buf = cs_dsp_buf_alloc(blk->data,
2318f6bc909eSSimon Trimmer 					       le32_to_cpu(blk->len),
2319f6bc909eSSimon Trimmer 					       &buf_list);
2320f6bc909eSSimon Trimmer 			if (!buf) {
2321f6bc909eSSimon Trimmer 				cs_dsp_err(dsp, "Out of memory\n");
2322f6bc909eSSimon Trimmer 				ret = -ENOMEM;
2323f6bc909eSSimon Trimmer 				goto out_fw;
2324f6bc909eSSimon Trimmer 			}
2325f6bc909eSSimon Trimmer 
2326f6bc909eSSimon Trimmer 			cs_dsp_dbg(dsp, "%s.%d: Writing %d bytes at %x\n",
2327f6bc909eSSimon Trimmer 				   file, blocks, le32_to_cpu(blk->len),
2328f6bc909eSSimon Trimmer 				   reg);
2329f6bc909eSSimon Trimmer 			ret = regmap_raw_write_async(regmap, reg, buf->buf,
2330f6bc909eSSimon Trimmer 						     le32_to_cpu(blk->len));
2331f6bc909eSSimon Trimmer 			if (ret != 0) {
2332f6bc909eSSimon Trimmer 				cs_dsp_err(dsp,
2333f6bc909eSSimon Trimmer 					   "%s.%d: Failed to write to %x in %s: %d\n",
2334f6bc909eSSimon Trimmer 					   file, blocks, reg, region_name, ret);
2335f6bc909eSSimon Trimmer 			}
2336f6bc909eSSimon Trimmer 		}
2337f6bc909eSSimon Trimmer 
2338f6bc909eSSimon Trimmer 		pos += (le32_to_cpu(blk->len) + sizeof(*blk) + 3) & ~0x03;
2339f6bc909eSSimon Trimmer 		blocks++;
2340f6bc909eSSimon Trimmer 	}
2341f6bc909eSSimon Trimmer 
2342f6bc909eSSimon Trimmer 	ret = regmap_async_complete(regmap);
2343f6bc909eSSimon Trimmer 	if (ret != 0)
2344f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to complete async write: %d\n", ret);
2345f6bc909eSSimon Trimmer 
2346f6bc909eSSimon Trimmer 	if (pos > firmware->size)
2347f6bc909eSSimon Trimmer 		cs_dsp_warn(dsp, "%s.%d: %zu bytes at end of file\n",
2348f6bc909eSSimon Trimmer 			    file, blocks, pos - firmware->size);
2349f6bc909eSSimon Trimmer 
2350f6bc909eSSimon Trimmer 	cs_dsp_debugfs_save_binname(dsp, file);
2351f6bc909eSSimon Trimmer 
2352f6bc909eSSimon Trimmer out_fw:
2353f6bc909eSSimon Trimmer 	regmap_async_complete(regmap);
2354f6bc909eSSimon Trimmer 	cs_dsp_buf_free(&buf_list);
23556598afa9SRichard Fitzgerald 
23566598afa9SRichard Fitzgerald 	if (ret == -EOVERFLOW)
23576598afa9SRichard Fitzgerald 		cs_dsp_err(dsp, "%s: file content overflows file data\n", file);
23586598afa9SRichard Fitzgerald 
2359f6bc909eSSimon Trimmer 	return ret;
2360f6bc909eSSimon Trimmer }
2361f6bc909eSSimon Trimmer 
cs_dsp_create_name(struct cs_dsp * dsp)2362f6bc909eSSimon Trimmer static int cs_dsp_create_name(struct cs_dsp *dsp)
2363f6bc909eSSimon Trimmer {
2364f6bc909eSSimon Trimmer 	if (!dsp->name) {
2365f6bc909eSSimon Trimmer 		dsp->name = devm_kasprintf(dsp->dev, GFP_KERNEL, "DSP%d",
2366f6bc909eSSimon Trimmer 					   dsp->num);
2367f6bc909eSSimon Trimmer 		if (!dsp->name)
2368f6bc909eSSimon Trimmer 			return -ENOMEM;
2369f6bc909eSSimon Trimmer 	}
2370f6bc909eSSimon Trimmer 
2371f6bc909eSSimon Trimmer 	return 0;
2372f6bc909eSSimon Trimmer }
2373f6bc909eSSimon Trimmer 
cs_dsp_common_init(struct cs_dsp * dsp)2374f6bc909eSSimon Trimmer static int cs_dsp_common_init(struct cs_dsp *dsp)
2375f6bc909eSSimon Trimmer {
2376f6bc909eSSimon Trimmer 	int ret;
2377f6bc909eSSimon Trimmer 
2378f6bc909eSSimon Trimmer 	ret = cs_dsp_create_name(dsp);
2379f6bc909eSSimon Trimmer 	if (ret)
2380f6bc909eSSimon Trimmer 		return ret;
2381f6bc909eSSimon Trimmer 
2382f6bc909eSSimon Trimmer 	INIT_LIST_HEAD(&dsp->alg_regions);
2383f6bc909eSSimon Trimmer 	INIT_LIST_HEAD(&dsp->ctl_list);
2384f6bc909eSSimon Trimmer 
2385f6bc909eSSimon Trimmer 	mutex_init(&dsp->pwr_lock);
2386f6bc909eSSimon Trimmer 
238766626b15SRichard Fitzgerald #ifdef CONFIG_DEBUG_FS
238866626b15SRichard Fitzgerald 	/* Ensure this is invalid if client never provides a debugfs root */
238966626b15SRichard Fitzgerald 	dsp->debugfs_root = ERR_PTR(-ENODEV);
239066626b15SRichard Fitzgerald #endif
239166626b15SRichard Fitzgerald 
2392f6bc909eSSimon Trimmer 	return 0;
2393f6bc909eSSimon Trimmer }
2394f6bc909eSSimon Trimmer 
2395f6bc909eSSimon Trimmer /**
2396f6bc909eSSimon Trimmer  * cs_dsp_adsp1_init() - Initialise a cs_dsp structure representing a ADSP1 device
2397f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2398f6bc909eSSimon Trimmer  *
2399f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
2400f6bc909eSSimon Trimmer  */
cs_dsp_adsp1_init(struct cs_dsp * dsp)2401f6bc909eSSimon Trimmer int cs_dsp_adsp1_init(struct cs_dsp *dsp)
2402f6bc909eSSimon Trimmer {
2403f6bc909eSSimon Trimmer 	dsp->ops = &cs_dsp_adsp1_ops;
2404f6bc909eSSimon Trimmer 
2405f6bc909eSSimon Trimmer 	return cs_dsp_common_init(dsp);
2406f6bc909eSSimon Trimmer }
2407e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp1_init, FW_CS_DSP);
2408f6bc909eSSimon Trimmer 
2409f6bc909eSSimon Trimmer /**
2410f6bc909eSSimon Trimmer  * cs_dsp_adsp1_power_up() - Load and start the named firmware
2411f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2412f6bc909eSSimon Trimmer  * @wmfw_firmware: the firmware to be sent
2413f6bc909eSSimon Trimmer  * @wmfw_filename: file name of firmware to be sent
2414f6bc909eSSimon Trimmer  * @coeff_firmware: the coefficient data to be sent
2415f6bc909eSSimon Trimmer  * @coeff_filename: file name of coefficient to data be sent
2416f6bc909eSSimon Trimmer  * @fw_name: the user-friendly firmware name
2417f6bc909eSSimon Trimmer  *
2418f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
2419f6bc909eSSimon Trimmer  */
cs_dsp_adsp1_power_up(struct cs_dsp * dsp,const struct firmware * wmfw_firmware,const char * wmfw_filename,const struct firmware * coeff_firmware,const char * coeff_filename,const char * fw_name)2420f6bc909eSSimon Trimmer int cs_dsp_adsp1_power_up(struct cs_dsp *dsp,
2421584e86e1SRichard Fitzgerald 			  const struct firmware *wmfw_firmware, const char *wmfw_filename,
2422584e86e1SRichard Fitzgerald 			  const struct firmware *coeff_firmware, const char *coeff_filename,
2423f6bc909eSSimon Trimmer 			  const char *fw_name)
2424f6bc909eSSimon Trimmer {
2425f6bc909eSSimon Trimmer 	unsigned int val;
2426f6bc909eSSimon Trimmer 	int ret;
2427f6bc909eSSimon Trimmer 
2428f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
2429f6bc909eSSimon Trimmer 
2430f6bc909eSSimon Trimmer 	dsp->fw_name = fw_name;
2431f6bc909eSSimon Trimmer 
2432f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2433f6bc909eSSimon Trimmer 			   ADSP1_SYS_ENA, ADSP1_SYS_ENA);
2434f6bc909eSSimon Trimmer 
2435f6bc909eSSimon Trimmer 	/*
2436f6bc909eSSimon Trimmer 	 * For simplicity set the DSP clock rate to be the
2437f6bc909eSSimon Trimmer 	 * SYSCLK rate rather than making it configurable.
2438f6bc909eSSimon Trimmer 	 */
2439f6bc909eSSimon Trimmer 	if (dsp->sysclk_reg) {
2440f6bc909eSSimon Trimmer 		ret = regmap_read(dsp->regmap, dsp->sysclk_reg, &val);
2441f6bc909eSSimon Trimmer 		if (ret != 0) {
2442f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "Failed to read SYSCLK state: %d\n", ret);
2443f6bc909eSSimon Trimmer 			goto err_mutex;
2444f6bc909eSSimon Trimmer 		}
2445f6bc909eSSimon Trimmer 
2446f6bc909eSSimon Trimmer 		val = (val & dsp->sysclk_mask) >> dsp->sysclk_shift;
2447f6bc909eSSimon Trimmer 
2448f6bc909eSSimon Trimmer 		ret = regmap_update_bits(dsp->regmap,
2449f6bc909eSSimon Trimmer 					 dsp->base + ADSP1_CONTROL_31,
2450f6bc909eSSimon Trimmer 					 ADSP1_CLK_SEL_MASK, val);
2451f6bc909eSSimon Trimmer 		if (ret != 0) {
2452f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "Failed to set clock rate: %d\n", ret);
2453f6bc909eSSimon Trimmer 			goto err_mutex;
2454f6bc909eSSimon Trimmer 		}
2455f6bc909eSSimon Trimmer 	}
2456f6bc909eSSimon Trimmer 
2457f6bc909eSSimon Trimmer 	ret = cs_dsp_load(dsp, wmfw_firmware, wmfw_filename);
2458f6bc909eSSimon Trimmer 	if (ret != 0)
2459f6bc909eSSimon Trimmer 		goto err_ena;
2460f6bc909eSSimon Trimmer 
2461f6bc909eSSimon Trimmer 	ret = cs_dsp_adsp1_setup_algs(dsp);
2462f6bc909eSSimon Trimmer 	if (ret != 0)
2463f6bc909eSSimon Trimmer 		goto err_ena;
2464f6bc909eSSimon Trimmer 
2465f6bc909eSSimon Trimmer 	ret = cs_dsp_load_coeff(dsp, coeff_firmware, coeff_filename);
2466f6bc909eSSimon Trimmer 	if (ret != 0)
2467f6bc909eSSimon Trimmer 		goto err_ena;
2468f6bc909eSSimon Trimmer 
2469f6bc909eSSimon Trimmer 	/* Initialize caches for enabled and unset controls */
2470f6bc909eSSimon Trimmer 	ret = cs_dsp_coeff_init_control_caches(dsp);
2471f6bc909eSSimon Trimmer 	if (ret != 0)
2472f6bc909eSSimon Trimmer 		goto err_ena;
2473f6bc909eSSimon Trimmer 
2474f6bc909eSSimon Trimmer 	/* Sync set controls */
2475f6bc909eSSimon Trimmer 	ret = cs_dsp_coeff_sync_controls(dsp);
2476f6bc909eSSimon Trimmer 	if (ret != 0)
2477f6bc909eSSimon Trimmer 		goto err_ena;
2478f6bc909eSSimon Trimmer 
2479f6bc909eSSimon Trimmer 	dsp->booted = true;
2480f6bc909eSSimon Trimmer 
2481f6bc909eSSimon Trimmer 	/* Start the core running */
2482f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2483f6bc909eSSimon Trimmer 			   ADSP1_CORE_ENA | ADSP1_START,
2484f6bc909eSSimon Trimmer 			   ADSP1_CORE_ENA | ADSP1_START);
2485f6bc909eSSimon Trimmer 
2486f6bc909eSSimon Trimmer 	dsp->running = true;
2487f6bc909eSSimon Trimmer 
2488f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
2489f6bc909eSSimon Trimmer 
2490f6bc909eSSimon Trimmer 	return 0;
2491f6bc909eSSimon Trimmer 
2492f6bc909eSSimon Trimmer err_ena:
2493f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2494f6bc909eSSimon Trimmer 			   ADSP1_SYS_ENA, 0);
2495f6bc909eSSimon Trimmer err_mutex:
2496f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
2497f6bc909eSSimon Trimmer 	return ret;
2498f6bc909eSSimon Trimmer }
2499e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp1_power_up, FW_CS_DSP);
2500f6bc909eSSimon Trimmer 
2501f6bc909eSSimon Trimmer /**
2502f6bc909eSSimon Trimmer  * cs_dsp_adsp1_power_down() - Halts the DSP
2503f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2504f6bc909eSSimon Trimmer  */
cs_dsp_adsp1_power_down(struct cs_dsp * dsp)2505f6bc909eSSimon Trimmer void cs_dsp_adsp1_power_down(struct cs_dsp *dsp)
2506f6bc909eSSimon Trimmer {
2507f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_ctl *ctl;
2508f6bc909eSSimon Trimmer 
2509f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
2510f6bc909eSSimon Trimmer 
2511f6bc909eSSimon Trimmer 	dsp->running = false;
2512f6bc909eSSimon Trimmer 	dsp->booted = false;
2513f6bc909eSSimon Trimmer 
2514f6bc909eSSimon Trimmer 	/* Halt the core */
2515f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2516f6bc909eSSimon Trimmer 			   ADSP1_CORE_ENA | ADSP1_START, 0);
2517f6bc909eSSimon Trimmer 
2518f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_19,
2519f6bc909eSSimon Trimmer 			   ADSP1_WDMA_BUFFER_LENGTH_MASK, 0);
2520f6bc909eSSimon Trimmer 
2521f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP1_CONTROL_30,
2522f6bc909eSSimon Trimmer 			   ADSP1_SYS_ENA, 0);
2523f6bc909eSSimon Trimmer 
2524f6bc909eSSimon Trimmer 	list_for_each_entry(ctl, &dsp->ctl_list, list)
2525f6bc909eSSimon Trimmer 		ctl->enabled = 0;
2526f6bc909eSSimon Trimmer 
2527f6bc909eSSimon Trimmer 	cs_dsp_free_alg_regions(dsp);
2528f6bc909eSSimon Trimmer 
2529f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
2530f6bc909eSSimon Trimmer }
2531e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp1_power_down, FW_CS_DSP);
2532f6bc909eSSimon Trimmer 
cs_dsp_adsp2v2_enable_core(struct cs_dsp * dsp)2533f6bc909eSSimon Trimmer static int cs_dsp_adsp2v2_enable_core(struct cs_dsp *dsp)
2534f6bc909eSSimon Trimmer {
2535f6bc909eSSimon Trimmer 	unsigned int val;
2536f6bc909eSSimon Trimmer 	int ret, count;
2537f6bc909eSSimon Trimmer 
2538f6bc909eSSimon Trimmer 	/* Wait for the RAM to start, should be near instantaneous */
2539f6bc909eSSimon Trimmer 	for (count = 0; count < 10; ++count) {
2540f6bc909eSSimon Trimmer 		ret = regmap_read(dsp->regmap, dsp->base + ADSP2_STATUS1, &val);
2541f6bc909eSSimon Trimmer 		if (ret != 0)
2542f6bc909eSSimon Trimmer 			return ret;
2543f6bc909eSSimon Trimmer 
2544f6bc909eSSimon Trimmer 		if (val & ADSP2_RAM_RDY)
2545f6bc909eSSimon Trimmer 			break;
2546f6bc909eSSimon Trimmer 
2547f6bc909eSSimon Trimmer 		usleep_range(250, 500);
2548f6bc909eSSimon Trimmer 	}
2549f6bc909eSSimon Trimmer 
2550f6bc909eSSimon Trimmer 	if (!(val & ADSP2_RAM_RDY)) {
2551f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to start DSP RAM\n");
2552f6bc909eSSimon Trimmer 		return -EBUSY;
2553f6bc909eSSimon Trimmer 	}
2554f6bc909eSSimon Trimmer 
2555f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "RAM ready after %d polls\n", count);
2556f6bc909eSSimon Trimmer 
2557f6bc909eSSimon Trimmer 	return 0;
2558f6bc909eSSimon Trimmer }
2559f6bc909eSSimon Trimmer 
cs_dsp_adsp2_enable_core(struct cs_dsp * dsp)2560f6bc909eSSimon Trimmer static int cs_dsp_adsp2_enable_core(struct cs_dsp *dsp)
2561f6bc909eSSimon Trimmer {
2562f6bc909eSSimon Trimmer 	int ret;
2563f6bc909eSSimon Trimmer 
2564f6bc909eSSimon Trimmer 	ret = regmap_update_bits_async(dsp->regmap, dsp->base + ADSP2_CONTROL,
2565f6bc909eSSimon Trimmer 				       ADSP2_SYS_ENA, ADSP2_SYS_ENA);
2566f6bc909eSSimon Trimmer 	if (ret != 0)
2567f6bc909eSSimon Trimmer 		return ret;
2568f6bc909eSSimon Trimmer 
2569f6bc909eSSimon Trimmer 	return cs_dsp_adsp2v2_enable_core(dsp);
2570f6bc909eSSimon Trimmer }
2571f6bc909eSSimon Trimmer 
cs_dsp_adsp2_lock(struct cs_dsp * dsp,unsigned int lock_regions)2572f6bc909eSSimon Trimmer static int cs_dsp_adsp2_lock(struct cs_dsp *dsp, unsigned int lock_regions)
2573f6bc909eSSimon Trimmer {
2574f6bc909eSSimon Trimmer 	struct regmap *regmap = dsp->regmap;
2575f6bc909eSSimon Trimmer 	unsigned int code0, code1, lock_reg;
2576f6bc909eSSimon Trimmer 
2577f6bc909eSSimon Trimmer 	if (!(lock_regions & CS_ADSP2_REGION_ALL))
2578f6bc909eSSimon Trimmer 		return 0;
2579f6bc909eSSimon Trimmer 
2580f6bc909eSSimon Trimmer 	lock_regions &= CS_ADSP2_REGION_ALL;
2581f6bc909eSSimon Trimmer 	lock_reg = dsp->base + ADSP2_LOCK_REGION_1_LOCK_REGION_0;
2582f6bc909eSSimon Trimmer 
2583f6bc909eSSimon Trimmer 	while (lock_regions) {
2584f6bc909eSSimon Trimmer 		code0 = code1 = 0;
2585f6bc909eSSimon Trimmer 		if (lock_regions & BIT(0)) {
2586f6bc909eSSimon Trimmer 			code0 = ADSP2_LOCK_CODE_0;
2587f6bc909eSSimon Trimmer 			code1 = ADSP2_LOCK_CODE_1;
2588f6bc909eSSimon Trimmer 		}
2589f6bc909eSSimon Trimmer 		if (lock_regions & BIT(1)) {
2590f6bc909eSSimon Trimmer 			code0 |= ADSP2_LOCK_CODE_0 << ADSP2_LOCK_REGION_SHIFT;
2591f6bc909eSSimon Trimmer 			code1 |= ADSP2_LOCK_CODE_1 << ADSP2_LOCK_REGION_SHIFT;
2592f6bc909eSSimon Trimmer 		}
2593f6bc909eSSimon Trimmer 		regmap_write(regmap, lock_reg, code0);
2594f6bc909eSSimon Trimmer 		regmap_write(regmap, lock_reg, code1);
2595f6bc909eSSimon Trimmer 		lock_regions >>= 2;
2596f6bc909eSSimon Trimmer 		lock_reg += 2;
2597f6bc909eSSimon Trimmer 	}
2598f6bc909eSSimon Trimmer 
2599f6bc909eSSimon Trimmer 	return 0;
2600f6bc909eSSimon Trimmer }
2601f6bc909eSSimon Trimmer 
cs_dsp_adsp2_enable_memory(struct cs_dsp * dsp)2602f6bc909eSSimon Trimmer static int cs_dsp_adsp2_enable_memory(struct cs_dsp *dsp)
2603f6bc909eSSimon Trimmer {
2604f6bc909eSSimon Trimmer 	return regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2605f6bc909eSSimon Trimmer 				  ADSP2_MEM_ENA, ADSP2_MEM_ENA);
2606f6bc909eSSimon Trimmer }
2607f6bc909eSSimon Trimmer 
cs_dsp_adsp2_disable_memory(struct cs_dsp * dsp)2608f6bc909eSSimon Trimmer static void cs_dsp_adsp2_disable_memory(struct cs_dsp *dsp)
2609f6bc909eSSimon Trimmer {
2610f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2611f6bc909eSSimon Trimmer 			   ADSP2_MEM_ENA, 0);
2612f6bc909eSSimon Trimmer }
2613f6bc909eSSimon Trimmer 
cs_dsp_adsp2_disable_core(struct cs_dsp * dsp)2614f6bc909eSSimon Trimmer static void cs_dsp_adsp2_disable_core(struct cs_dsp *dsp)
2615f6bc909eSSimon Trimmer {
2616f6bc909eSSimon Trimmer 	regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
2617f6bc909eSSimon Trimmer 	regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
2618f6bc909eSSimon Trimmer 	regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_2, 0);
2619f6bc909eSSimon Trimmer 
2620f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2621f6bc909eSSimon Trimmer 			   ADSP2_SYS_ENA, 0);
2622f6bc909eSSimon Trimmer }
2623f6bc909eSSimon Trimmer 
cs_dsp_adsp2v2_disable_core(struct cs_dsp * dsp)2624f6bc909eSSimon Trimmer static void cs_dsp_adsp2v2_disable_core(struct cs_dsp *dsp)
2625f6bc909eSSimon Trimmer {
2626f6bc909eSSimon Trimmer 	regmap_write(dsp->regmap, dsp->base + ADSP2_RDMA_CONFIG_1, 0);
2627f6bc909eSSimon Trimmer 	regmap_write(dsp->regmap, dsp->base + ADSP2_WDMA_CONFIG_1, 0);
2628f6bc909eSSimon Trimmer 	regmap_write(dsp->regmap, dsp->base + ADSP2V2_WDMA_CONFIG_2, 0);
2629f6bc909eSSimon Trimmer }
2630f6bc909eSSimon Trimmer 
cs_dsp_halo_configure_mpu(struct cs_dsp * dsp,unsigned int lock_regions)2631f6bc909eSSimon Trimmer static int cs_dsp_halo_configure_mpu(struct cs_dsp *dsp, unsigned int lock_regions)
2632f6bc909eSSimon Trimmer {
2633f6bc909eSSimon Trimmer 	struct reg_sequence config[] = {
2634f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_LOCK_CONFIG,     0x5555 },
2635f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_LOCK_CONFIG,     0xAAAA },
2636f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_XMEM_ACCESS_0,   0xFFFFFFFF },
2637f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_YMEM_ACCESS_0,   0xFFFFFFFF },
2638f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_WINDOW_ACCESS_0, lock_regions },
2639f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_XREG_ACCESS_0,   lock_regions },
2640f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_YREG_ACCESS_0,   lock_regions },
2641f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_XMEM_ACCESS_1,   0xFFFFFFFF },
2642f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_YMEM_ACCESS_1,   0xFFFFFFFF },
2643f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_WINDOW_ACCESS_1, lock_regions },
2644f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_XREG_ACCESS_1,   lock_regions },
2645f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_YREG_ACCESS_1,   lock_regions },
2646f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_XMEM_ACCESS_2,   0xFFFFFFFF },
2647f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_YMEM_ACCESS_2,   0xFFFFFFFF },
2648f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_WINDOW_ACCESS_2, lock_regions },
2649f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_XREG_ACCESS_2,   lock_regions },
2650f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_YREG_ACCESS_2,   lock_regions },
2651f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_XMEM_ACCESS_3,   0xFFFFFFFF },
2652f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_YMEM_ACCESS_3,   0xFFFFFFFF },
2653f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_WINDOW_ACCESS_3, lock_regions },
2654f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_XREG_ACCESS_3,   lock_regions },
2655f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_YREG_ACCESS_3,   lock_regions },
2656f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_LOCK_CONFIG,     0 },
2657f6bc909eSSimon Trimmer 	};
2658f6bc909eSSimon Trimmer 
2659f6bc909eSSimon Trimmer 	return regmap_multi_reg_write(dsp->regmap, config, ARRAY_SIZE(config));
2660f6bc909eSSimon Trimmer }
2661f6bc909eSSimon Trimmer 
2662f6bc909eSSimon Trimmer /**
2663f6bc909eSSimon Trimmer  * cs_dsp_set_dspclk() - Applies the given frequency to the given cs_dsp
2664f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2665f6bc909eSSimon Trimmer  * @freq: clock rate to set
2666f6bc909eSSimon Trimmer  *
2667f6bc909eSSimon Trimmer  * This is only for use on ADSP2 cores.
2668f6bc909eSSimon Trimmer  *
2669f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
2670f6bc909eSSimon Trimmer  */
cs_dsp_set_dspclk(struct cs_dsp * dsp,unsigned int freq)2671f6bc909eSSimon Trimmer int cs_dsp_set_dspclk(struct cs_dsp *dsp, unsigned int freq)
2672f6bc909eSSimon Trimmer {
2673f6bc909eSSimon Trimmer 	int ret;
2674f6bc909eSSimon Trimmer 
2675f6bc909eSSimon Trimmer 	ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CLOCKING,
2676f6bc909eSSimon Trimmer 				 ADSP2_CLK_SEL_MASK,
2677f6bc909eSSimon Trimmer 				 freq << ADSP2_CLK_SEL_SHIFT);
2678f6bc909eSSimon Trimmer 	if (ret)
2679f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "Failed to set clock rate: %d\n", ret);
2680f6bc909eSSimon Trimmer 
2681f6bc909eSSimon Trimmer 	return ret;
2682f6bc909eSSimon Trimmer }
2683e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_set_dspclk, FW_CS_DSP);
2684f6bc909eSSimon Trimmer 
cs_dsp_stop_watchdog(struct cs_dsp * dsp)2685f6bc909eSSimon Trimmer static void cs_dsp_stop_watchdog(struct cs_dsp *dsp)
2686f6bc909eSSimon Trimmer {
2687f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP2_WATCHDOG,
2688f6bc909eSSimon Trimmer 			   ADSP2_WDT_ENA_MASK, 0);
2689f6bc909eSSimon Trimmer }
2690f6bc909eSSimon Trimmer 
cs_dsp_halo_stop_watchdog(struct cs_dsp * dsp)2691f6bc909eSSimon Trimmer static void cs_dsp_halo_stop_watchdog(struct cs_dsp *dsp)
2692f6bc909eSSimon Trimmer {
2693f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + HALO_WDT_CONTROL,
2694f6bc909eSSimon Trimmer 			   HALO_WDT_EN_MASK, 0);
2695f6bc909eSSimon Trimmer }
2696f6bc909eSSimon Trimmer 
2697f6bc909eSSimon Trimmer /**
2698f6bc909eSSimon Trimmer  * cs_dsp_power_up() - Downloads firmware to the DSP
2699f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2700f6bc909eSSimon Trimmer  * @wmfw_firmware: the firmware to be sent
2701f6bc909eSSimon Trimmer  * @wmfw_filename: file name of firmware to be sent
2702f6bc909eSSimon Trimmer  * @coeff_firmware: the coefficient data to be sent
2703f6bc909eSSimon Trimmer  * @coeff_filename: file name of coefficient to data be sent
2704f6bc909eSSimon Trimmer  * @fw_name: the user-friendly firmware name
2705f6bc909eSSimon Trimmer  *
2706f6bc909eSSimon Trimmer  * This function is used on ADSP2 and Halo DSP cores, it powers-up the DSP core
2707f6bc909eSSimon Trimmer  * and downloads the firmware but does not start the firmware running. The
2708f6bc909eSSimon Trimmer  * cs_dsp booted flag will be set once completed and if the core has a low-power
2709f6bc909eSSimon Trimmer  * memory retention mode it will be put into this state after the firmware is
2710f6bc909eSSimon Trimmer  * downloaded.
2711f6bc909eSSimon Trimmer  *
2712f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
2713f6bc909eSSimon Trimmer  */
cs_dsp_power_up(struct cs_dsp * dsp,const struct firmware * wmfw_firmware,const char * wmfw_filename,const struct firmware * coeff_firmware,const char * coeff_filename,const char * fw_name)2714f6bc909eSSimon Trimmer int cs_dsp_power_up(struct cs_dsp *dsp,
2715584e86e1SRichard Fitzgerald 		    const struct firmware *wmfw_firmware, const char *wmfw_filename,
2716584e86e1SRichard Fitzgerald 		    const struct firmware *coeff_firmware, const char *coeff_filename,
2717f6bc909eSSimon Trimmer 		    const char *fw_name)
2718f6bc909eSSimon Trimmer {
2719f6bc909eSSimon Trimmer 	int ret;
2720f6bc909eSSimon Trimmer 
2721f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
2722f6bc909eSSimon Trimmer 
2723f6bc909eSSimon Trimmer 	dsp->fw_name = fw_name;
2724f6bc909eSSimon Trimmer 
2725f6bc909eSSimon Trimmer 	if (dsp->ops->enable_memory) {
2726f6bc909eSSimon Trimmer 		ret = dsp->ops->enable_memory(dsp);
2727f6bc909eSSimon Trimmer 		if (ret != 0)
2728f6bc909eSSimon Trimmer 			goto err_mutex;
2729f6bc909eSSimon Trimmer 	}
2730f6bc909eSSimon Trimmer 
2731f6bc909eSSimon Trimmer 	if (dsp->ops->enable_core) {
2732f6bc909eSSimon Trimmer 		ret = dsp->ops->enable_core(dsp);
2733f6bc909eSSimon Trimmer 		if (ret != 0)
2734f6bc909eSSimon Trimmer 			goto err_mem;
2735f6bc909eSSimon Trimmer 	}
2736f6bc909eSSimon Trimmer 
2737f6bc909eSSimon Trimmer 	ret = cs_dsp_load(dsp, wmfw_firmware, wmfw_filename);
2738f6bc909eSSimon Trimmer 	if (ret != 0)
2739f6bc909eSSimon Trimmer 		goto err_ena;
2740f6bc909eSSimon Trimmer 
2741f6bc909eSSimon Trimmer 	ret = dsp->ops->setup_algs(dsp);
2742f6bc909eSSimon Trimmer 	if (ret != 0)
2743f6bc909eSSimon Trimmer 		goto err_ena;
2744f6bc909eSSimon Trimmer 
2745f6bc909eSSimon Trimmer 	ret = cs_dsp_load_coeff(dsp, coeff_firmware, coeff_filename);
2746f6bc909eSSimon Trimmer 	if (ret != 0)
2747f6bc909eSSimon Trimmer 		goto err_ena;
2748f6bc909eSSimon Trimmer 
2749f6bc909eSSimon Trimmer 	/* Initialize caches for enabled and unset controls */
2750f6bc909eSSimon Trimmer 	ret = cs_dsp_coeff_init_control_caches(dsp);
2751f6bc909eSSimon Trimmer 	if (ret != 0)
2752f6bc909eSSimon Trimmer 		goto err_ena;
2753f6bc909eSSimon Trimmer 
2754f6bc909eSSimon Trimmer 	if (dsp->ops->disable_core)
2755f6bc909eSSimon Trimmer 		dsp->ops->disable_core(dsp);
2756f6bc909eSSimon Trimmer 
2757f6bc909eSSimon Trimmer 	dsp->booted = true;
2758f6bc909eSSimon Trimmer 
2759f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
2760f6bc909eSSimon Trimmer 
2761f6bc909eSSimon Trimmer 	return 0;
2762f6bc909eSSimon Trimmer err_ena:
2763f6bc909eSSimon Trimmer 	if (dsp->ops->disable_core)
2764f6bc909eSSimon Trimmer 		dsp->ops->disable_core(dsp);
2765f6bc909eSSimon Trimmer err_mem:
2766f6bc909eSSimon Trimmer 	if (dsp->ops->disable_memory)
2767f6bc909eSSimon Trimmer 		dsp->ops->disable_memory(dsp);
2768f6bc909eSSimon Trimmer err_mutex:
2769f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
2770f6bc909eSSimon Trimmer 
2771f6bc909eSSimon Trimmer 	return ret;
2772f6bc909eSSimon Trimmer }
2773e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_power_up, FW_CS_DSP);
2774f6bc909eSSimon Trimmer 
2775f6bc909eSSimon Trimmer /**
2776f6bc909eSSimon Trimmer  * cs_dsp_power_down() - Powers-down the DSP
2777f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2778f6bc909eSSimon Trimmer  *
2779f6bc909eSSimon Trimmer  * cs_dsp_stop() must have been called before this function. The core will be
2780f6bc909eSSimon Trimmer  * fully powered down and so the memory will not be retained.
2781f6bc909eSSimon Trimmer  */
cs_dsp_power_down(struct cs_dsp * dsp)2782f6bc909eSSimon Trimmer void cs_dsp_power_down(struct cs_dsp *dsp)
2783f6bc909eSSimon Trimmer {
2784f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_ctl *ctl;
2785f6bc909eSSimon Trimmer 
2786f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
2787f6bc909eSSimon Trimmer 
2788f6bc909eSSimon Trimmer 	cs_dsp_debugfs_clear(dsp);
2789f6bc909eSSimon Trimmer 
2790f6bc909eSSimon Trimmer 	dsp->fw_id = 0;
2791f6bc909eSSimon Trimmer 	dsp->fw_id_version = 0;
2792f6bc909eSSimon Trimmer 
2793f6bc909eSSimon Trimmer 	dsp->booted = false;
2794f6bc909eSSimon Trimmer 
2795f6bc909eSSimon Trimmer 	if (dsp->ops->disable_memory)
2796f6bc909eSSimon Trimmer 		dsp->ops->disable_memory(dsp);
2797f6bc909eSSimon Trimmer 
2798f6bc909eSSimon Trimmer 	list_for_each_entry(ctl, &dsp->ctl_list, list)
2799f6bc909eSSimon Trimmer 		ctl->enabled = 0;
2800f6bc909eSSimon Trimmer 
2801f6bc909eSSimon Trimmer 	cs_dsp_free_alg_regions(dsp);
2802f6bc909eSSimon Trimmer 
2803f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
2804f6bc909eSSimon Trimmer 
2805f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "Shutdown complete\n");
2806f6bc909eSSimon Trimmer }
2807e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_power_down, FW_CS_DSP);
2808f6bc909eSSimon Trimmer 
cs_dsp_adsp2_start_core(struct cs_dsp * dsp)2809f6bc909eSSimon Trimmer static int cs_dsp_adsp2_start_core(struct cs_dsp *dsp)
2810f6bc909eSSimon Trimmer {
2811f6bc909eSSimon Trimmer 	return regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2812f6bc909eSSimon Trimmer 				  ADSP2_CORE_ENA | ADSP2_START,
2813f6bc909eSSimon Trimmer 				  ADSP2_CORE_ENA | ADSP2_START);
2814f6bc909eSSimon Trimmer }
2815f6bc909eSSimon Trimmer 
cs_dsp_adsp2_stop_core(struct cs_dsp * dsp)2816f6bc909eSSimon Trimmer static void cs_dsp_adsp2_stop_core(struct cs_dsp *dsp)
2817f6bc909eSSimon Trimmer {
2818f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2819f6bc909eSSimon Trimmer 			   ADSP2_CORE_ENA | ADSP2_START, 0);
2820f6bc909eSSimon Trimmer }
2821f6bc909eSSimon Trimmer 
2822f6bc909eSSimon Trimmer /**
2823f6bc909eSSimon Trimmer  * cs_dsp_run() - Starts the firmware running
2824f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2825f6bc909eSSimon Trimmer  *
2826f6bc909eSSimon Trimmer  * cs_dsp_power_up() must have previously been called successfully.
2827f6bc909eSSimon Trimmer  *
2828f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
2829f6bc909eSSimon Trimmer  */
cs_dsp_run(struct cs_dsp * dsp)2830f6bc909eSSimon Trimmer int cs_dsp_run(struct cs_dsp *dsp)
2831f6bc909eSSimon Trimmer {
2832f6bc909eSSimon Trimmer 	int ret;
2833f6bc909eSSimon Trimmer 
2834f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
2835f6bc909eSSimon Trimmer 
2836f6bc909eSSimon Trimmer 	if (!dsp->booted) {
2837f6bc909eSSimon Trimmer 		ret = -EIO;
2838f6bc909eSSimon Trimmer 		goto err;
2839f6bc909eSSimon Trimmer 	}
2840f6bc909eSSimon Trimmer 
2841f6bc909eSSimon Trimmer 	if (dsp->ops->enable_core) {
2842f6bc909eSSimon Trimmer 		ret = dsp->ops->enable_core(dsp);
2843f6bc909eSSimon Trimmer 		if (ret != 0)
2844f6bc909eSSimon Trimmer 			goto err;
2845f6bc909eSSimon Trimmer 	}
2846f6bc909eSSimon Trimmer 
284714055b5aSCharles Keepax 	if (dsp->client_ops->pre_run) {
284814055b5aSCharles Keepax 		ret = dsp->client_ops->pre_run(dsp);
284914055b5aSCharles Keepax 		if (ret)
285014055b5aSCharles Keepax 			goto err;
285114055b5aSCharles Keepax 	}
285214055b5aSCharles Keepax 
2853f6bc909eSSimon Trimmer 	/* Sync set controls */
2854f6bc909eSSimon Trimmer 	ret = cs_dsp_coeff_sync_controls(dsp);
2855f6bc909eSSimon Trimmer 	if (ret != 0)
2856f6bc909eSSimon Trimmer 		goto err;
2857f6bc909eSSimon Trimmer 
2858f6bc909eSSimon Trimmer 	if (dsp->ops->lock_memory) {
2859f6bc909eSSimon Trimmer 		ret = dsp->ops->lock_memory(dsp, dsp->lock_regions);
2860f6bc909eSSimon Trimmer 		if (ret != 0) {
2861f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "Error configuring MPU: %d\n", ret);
2862f6bc909eSSimon Trimmer 			goto err;
2863f6bc909eSSimon Trimmer 		}
2864f6bc909eSSimon Trimmer 	}
2865f6bc909eSSimon Trimmer 
2866f6bc909eSSimon Trimmer 	if (dsp->ops->start_core) {
2867f6bc909eSSimon Trimmer 		ret = dsp->ops->start_core(dsp);
2868f6bc909eSSimon Trimmer 		if (ret != 0)
2869f6bc909eSSimon Trimmer 			goto err;
2870f6bc909eSSimon Trimmer 	}
2871f6bc909eSSimon Trimmer 
2872f6bc909eSSimon Trimmer 	dsp->running = true;
2873f6bc909eSSimon Trimmer 
2874f6bc909eSSimon Trimmer 	if (dsp->client_ops->post_run) {
2875f6bc909eSSimon Trimmer 		ret = dsp->client_ops->post_run(dsp);
2876f6bc909eSSimon Trimmer 		if (ret)
2877f6bc909eSSimon Trimmer 			goto err;
2878f6bc909eSSimon Trimmer 	}
2879f6bc909eSSimon Trimmer 
2880f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
2881f6bc909eSSimon Trimmer 
2882f6bc909eSSimon Trimmer 	return 0;
2883f6bc909eSSimon Trimmer 
2884f6bc909eSSimon Trimmer err:
2885f6bc909eSSimon Trimmer 	if (dsp->ops->stop_core)
2886f6bc909eSSimon Trimmer 		dsp->ops->stop_core(dsp);
2887f6bc909eSSimon Trimmer 	if (dsp->ops->disable_core)
2888f6bc909eSSimon Trimmer 		dsp->ops->disable_core(dsp);
2889f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
2890f6bc909eSSimon Trimmer 
2891f6bc909eSSimon Trimmer 	return ret;
2892f6bc909eSSimon Trimmer }
2893e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_run, FW_CS_DSP);
2894f6bc909eSSimon Trimmer 
2895f6bc909eSSimon Trimmer /**
2896f6bc909eSSimon Trimmer  * cs_dsp_stop() - Stops the firmware
2897f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2898f6bc909eSSimon Trimmer  *
2899f6bc909eSSimon Trimmer  * Memory will not be disabled so firmware will remain loaded.
2900f6bc909eSSimon Trimmer  */
cs_dsp_stop(struct cs_dsp * dsp)2901f6bc909eSSimon Trimmer void cs_dsp_stop(struct cs_dsp *dsp)
2902f6bc909eSSimon Trimmer {
2903f6bc909eSSimon Trimmer 	/* Tell the firmware to cleanup */
2904f6bc909eSSimon Trimmer 	cs_dsp_signal_event_controls(dsp, CS_DSP_FW_EVENT_SHUTDOWN);
2905f6bc909eSSimon Trimmer 
2906f6bc909eSSimon Trimmer 	if (dsp->ops->stop_watchdog)
2907f6bc909eSSimon Trimmer 		dsp->ops->stop_watchdog(dsp);
2908f6bc909eSSimon Trimmer 
2909f6bc909eSSimon Trimmer 	/* Log firmware state, it can be useful for analysis */
2910f6bc909eSSimon Trimmer 	if (dsp->ops->show_fw_status)
2911f6bc909eSSimon Trimmer 		dsp->ops->show_fw_status(dsp);
2912f6bc909eSSimon Trimmer 
2913f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
2914f6bc909eSSimon Trimmer 
2915dea99773SCharles Keepax 	if (dsp->client_ops->pre_stop)
2916dea99773SCharles Keepax 		dsp->client_ops->pre_stop(dsp);
2917dea99773SCharles Keepax 
2918f6bc909eSSimon Trimmer 	dsp->running = false;
2919f6bc909eSSimon Trimmer 
2920f6bc909eSSimon Trimmer 	if (dsp->ops->stop_core)
2921f6bc909eSSimon Trimmer 		dsp->ops->stop_core(dsp);
2922f6bc909eSSimon Trimmer 	if (dsp->ops->disable_core)
2923f6bc909eSSimon Trimmer 		dsp->ops->disable_core(dsp);
2924f6bc909eSSimon Trimmer 
2925f6bc909eSSimon Trimmer 	if (dsp->client_ops->post_stop)
2926f6bc909eSSimon Trimmer 		dsp->client_ops->post_stop(dsp);
2927f6bc909eSSimon Trimmer 
2928f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
2929f6bc909eSSimon Trimmer 
2930f6bc909eSSimon Trimmer 	cs_dsp_dbg(dsp, "Execution stopped\n");
2931f6bc909eSSimon Trimmer }
2932e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_stop, FW_CS_DSP);
2933f6bc909eSSimon Trimmer 
cs_dsp_halo_start_core(struct cs_dsp * dsp)2934f6bc909eSSimon Trimmer static int cs_dsp_halo_start_core(struct cs_dsp *dsp)
2935f6bc909eSSimon Trimmer {
29367aa1cc10SCharles Keepax 	int ret;
29377aa1cc10SCharles Keepax 
29387aa1cc10SCharles Keepax 	ret = regmap_update_bits(dsp->regmap, dsp->base + HALO_CCM_CORE_CONTROL,
2939f6bc909eSSimon Trimmer 				 HALO_CORE_RESET | HALO_CORE_EN,
2940f6bc909eSSimon Trimmer 				 HALO_CORE_RESET | HALO_CORE_EN);
29417aa1cc10SCharles Keepax 	if (ret)
29427aa1cc10SCharles Keepax 		return ret;
29437aa1cc10SCharles Keepax 
29447aa1cc10SCharles Keepax 	return regmap_update_bits(dsp->regmap, dsp->base + HALO_CCM_CORE_CONTROL,
29457aa1cc10SCharles Keepax 				  HALO_CORE_RESET, 0);
2946f6bc909eSSimon Trimmer }
2947f6bc909eSSimon Trimmer 
cs_dsp_halo_stop_core(struct cs_dsp * dsp)2948f6bc909eSSimon Trimmer static void cs_dsp_halo_stop_core(struct cs_dsp *dsp)
2949f6bc909eSSimon Trimmer {
2950f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + HALO_CCM_CORE_CONTROL,
2951f6bc909eSSimon Trimmer 			   HALO_CORE_EN, 0);
2952f6bc909eSSimon Trimmer 
2953f6bc909eSSimon Trimmer 	/* reset halo core with CORE_SOFT_RESET */
2954f6bc909eSSimon Trimmer 	regmap_update_bits(dsp->regmap, dsp->base + HALO_CORE_SOFT_RESET,
2955f6bc909eSSimon Trimmer 			   HALO_CORE_SOFT_RESET_MASK, 1);
2956f6bc909eSSimon Trimmer }
2957f6bc909eSSimon Trimmer 
2958f6bc909eSSimon Trimmer /**
2959f6bc909eSSimon Trimmer  * cs_dsp_adsp2_init() - Initialise a cs_dsp structure representing a ADSP2 core
2960f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2961f6bc909eSSimon Trimmer  *
2962f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
2963f6bc909eSSimon Trimmer  */
cs_dsp_adsp2_init(struct cs_dsp * dsp)2964f6bc909eSSimon Trimmer int cs_dsp_adsp2_init(struct cs_dsp *dsp)
2965f6bc909eSSimon Trimmer {
2966f6bc909eSSimon Trimmer 	int ret;
2967f6bc909eSSimon Trimmer 
2968f6bc909eSSimon Trimmer 	switch (dsp->rev) {
2969f6bc909eSSimon Trimmer 	case 0:
2970f6bc909eSSimon Trimmer 		/*
2971f6bc909eSSimon Trimmer 		 * Disable the DSP memory by default when in reset for a small
2972f6bc909eSSimon Trimmer 		 * power saving.
2973f6bc909eSSimon Trimmer 		 */
2974f6bc909eSSimon Trimmer 		ret = regmap_update_bits(dsp->regmap, dsp->base + ADSP2_CONTROL,
2975f6bc909eSSimon Trimmer 					 ADSP2_MEM_ENA, 0);
2976f6bc909eSSimon Trimmer 		if (ret) {
2977f6bc909eSSimon Trimmer 			cs_dsp_err(dsp,
2978f6bc909eSSimon Trimmer 				   "Failed to clear memory retention: %d\n", ret);
2979f6bc909eSSimon Trimmer 			return ret;
2980f6bc909eSSimon Trimmer 		}
2981f6bc909eSSimon Trimmer 
2982f6bc909eSSimon Trimmer 		dsp->ops = &cs_dsp_adsp2_ops[0];
2983f6bc909eSSimon Trimmer 		break;
2984f6bc909eSSimon Trimmer 	case 1:
2985f6bc909eSSimon Trimmer 		dsp->ops = &cs_dsp_adsp2_ops[1];
2986f6bc909eSSimon Trimmer 		break;
2987f6bc909eSSimon Trimmer 	default:
2988f6bc909eSSimon Trimmer 		dsp->ops = &cs_dsp_adsp2_ops[2];
2989f6bc909eSSimon Trimmer 		break;
2990f6bc909eSSimon Trimmer 	}
2991f6bc909eSSimon Trimmer 
2992f6bc909eSSimon Trimmer 	return cs_dsp_common_init(dsp);
2993f6bc909eSSimon Trimmer }
2994e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp2_init, FW_CS_DSP);
2995f6bc909eSSimon Trimmer 
2996f6bc909eSSimon Trimmer /**
2997f6bc909eSSimon Trimmer  * cs_dsp_halo_init() - Initialise a cs_dsp structure representing a HALO Core DSP
2998f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
2999f6bc909eSSimon Trimmer  *
3000f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
3001f6bc909eSSimon Trimmer  */
cs_dsp_halo_init(struct cs_dsp * dsp)3002f6bc909eSSimon Trimmer int cs_dsp_halo_init(struct cs_dsp *dsp)
3003f6bc909eSSimon Trimmer {
30047062e1c7SSimon Trimmer 	if (dsp->no_core_startstop)
30057062e1c7SSimon Trimmer 		dsp->ops = &cs_dsp_halo_ao_ops;
30067062e1c7SSimon Trimmer 	else
3007f6bc909eSSimon Trimmer 		dsp->ops = &cs_dsp_halo_ops;
3008f6bc909eSSimon Trimmer 
3009f6bc909eSSimon Trimmer 	return cs_dsp_common_init(dsp);
3010f6bc909eSSimon Trimmer }
3011e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_halo_init, FW_CS_DSP);
3012f6bc909eSSimon Trimmer 
3013f6bc909eSSimon Trimmer /**
3014f6bc909eSSimon Trimmer  * cs_dsp_remove() - Clean a cs_dsp before deletion
3015f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
3016f6bc909eSSimon Trimmer  */
cs_dsp_remove(struct cs_dsp * dsp)3017f6bc909eSSimon Trimmer void cs_dsp_remove(struct cs_dsp *dsp)
3018f6bc909eSSimon Trimmer {
3019f6bc909eSSimon Trimmer 	struct cs_dsp_coeff_ctl *ctl;
3020f6bc909eSSimon Trimmer 
3021f6bc909eSSimon Trimmer 	while (!list_empty(&dsp->ctl_list)) {
3022f6bc909eSSimon Trimmer 		ctl = list_first_entry(&dsp->ctl_list, struct cs_dsp_coeff_ctl, list);
3023f6bc909eSSimon Trimmer 
3024f6bc909eSSimon Trimmer 		if (dsp->client_ops->control_remove)
3025f6bc909eSSimon Trimmer 			dsp->client_ops->control_remove(ctl);
3026f6bc909eSSimon Trimmer 
3027f6bc909eSSimon Trimmer 		list_del(&ctl->list);
3028f6bc909eSSimon Trimmer 		cs_dsp_free_ctl_blk(ctl);
3029f6bc909eSSimon Trimmer 	}
3030f6bc909eSSimon Trimmer }
3031e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_remove, FW_CS_DSP);
3032f6bc909eSSimon Trimmer 
3033f6bc909eSSimon Trimmer /**
3034f6bc909eSSimon Trimmer  * cs_dsp_read_raw_data_block() - Reads a block of data from DSP memory
3035f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
3036f6bc909eSSimon Trimmer  * @mem_type: the type of DSP memory containing the data to be read
3037f6bc909eSSimon Trimmer  * @mem_addr: the address of the data within the memory region
3038f6bc909eSSimon Trimmer  * @num_words: the length of the data to read
3039f6bc909eSSimon Trimmer  * @data: a buffer to store the fetched data
3040f6bc909eSSimon Trimmer  *
3041f6bc909eSSimon Trimmer  * If this is used to read unpacked 24-bit memory, each 24-bit DSP word will
3042f6bc909eSSimon Trimmer  * occupy 32-bits in data (MSbyte will be 0). This padding can be removed using
3043f6bc909eSSimon Trimmer  * cs_dsp_remove_padding()
3044f6bc909eSSimon Trimmer  *
3045f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
3046f6bc909eSSimon Trimmer  */
cs_dsp_read_raw_data_block(struct cs_dsp * dsp,int mem_type,unsigned int mem_addr,unsigned int num_words,__be32 * data)3047f6bc909eSSimon Trimmer int cs_dsp_read_raw_data_block(struct cs_dsp *dsp, int mem_type, unsigned int mem_addr,
3048f6bc909eSSimon Trimmer 			       unsigned int num_words, __be32 *data)
3049f6bc909eSSimon Trimmer {
3050f6bc909eSSimon Trimmer 	struct cs_dsp_region const *mem = cs_dsp_find_region(dsp, mem_type);
3051f6bc909eSSimon Trimmer 	unsigned int reg;
3052f6bc909eSSimon Trimmer 	int ret;
3053f6bc909eSSimon Trimmer 
30545065cfabSCharles Keepax 	lockdep_assert_held(&dsp->pwr_lock);
30555065cfabSCharles Keepax 
3056f6bc909eSSimon Trimmer 	if (!mem)
3057f6bc909eSSimon Trimmer 		return -EINVAL;
3058f6bc909eSSimon Trimmer 
3059f6bc909eSSimon Trimmer 	reg = dsp->ops->region_to_reg(mem, mem_addr);
3060f6bc909eSSimon Trimmer 
3061f6bc909eSSimon Trimmer 	ret = regmap_raw_read(dsp->regmap, reg, data,
3062f6bc909eSSimon Trimmer 			      sizeof(*data) * num_words);
3063f6bc909eSSimon Trimmer 	if (ret < 0)
3064f6bc909eSSimon Trimmer 		return ret;
3065f6bc909eSSimon Trimmer 
3066f6bc909eSSimon Trimmer 	return 0;
3067f6bc909eSSimon Trimmer }
3068e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_read_raw_data_block, FW_CS_DSP);
3069f6bc909eSSimon Trimmer 
3070f6bc909eSSimon Trimmer /**
3071f6bc909eSSimon Trimmer  * cs_dsp_read_data_word() - Reads a word from DSP memory
3072f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
3073f6bc909eSSimon Trimmer  * @mem_type: the type of DSP memory containing the data to be read
3074f6bc909eSSimon Trimmer  * @mem_addr: the address of the data within the memory region
3075f6bc909eSSimon Trimmer  * @data: a buffer to store the fetched data
3076f6bc909eSSimon Trimmer  *
3077f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
3078f6bc909eSSimon Trimmer  */
cs_dsp_read_data_word(struct cs_dsp * dsp,int mem_type,unsigned int mem_addr,u32 * data)3079f6bc909eSSimon Trimmer int cs_dsp_read_data_word(struct cs_dsp *dsp, int mem_type, unsigned int mem_addr, u32 *data)
3080f6bc909eSSimon Trimmer {
3081f6bc909eSSimon Trimmer 	__be32 raw;
3082f6bc909eSSimon Trimmer 	int ret;
3083f6bc909eSSimon Trimmer 
3084f6bc909eSSimon Trimmer 	ret = cs_dsp_read_raw_data_block(dsp, mem_type, mem_addr, 1, &raw);
3085f6bc909eSSimon Trimmer 	if (ret < 0)
3086f6bc909eSSimon Trimmer 		return ret;
3087f6bc909eSSimon Trimmer 
3088f6bc909eSSimon Trimmer 	*data = be32_to_cpu(raw) & 0x00ffffffu;
3089f6bc909eSSimon Trimmer 
3090f6bc909eSSimon Trimmer 	return 0;
3091f6bc909eSSimon Trimmer }
3092e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_read_data_word, FW_CS_DSP);
3093f6bc909eSSimon Trimmer 
3094f6bc909eSSimon Trimmer /**
3095f6bc909eSSimon Trimmer  * cs_dsp_write_data_word() - Writes a word to DSP memory
3096f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
3097f6bc909eSSimon Trimmer  * @mem_type: the type of DSP memory containing the data to be written
3098f6bc909eSSimon Trimmer  * @mem_addr: the address of the data within the memory region
3099f6bc909eSSimon Trimmer  * @data: the data to be written
3100f6bc909eSSimon Trimmer  *
3101f6bc909eSSimon Trimmer  * Return: Zero for success, a negative number on error.
3102f6bc909eSSimon Trimmer  */
cs_dsp_write_data_word(struct cs_dsp * dsp,int mem_type,unsigned int mem_addr,u32 data)3103f6bc909eSSimon Trimmer int cs_dsp_write_data_word(struct cs_dsp *dsp, int mem_type, unsigned int mem_addr, u32 data)
3104f6bc909eSSimon Trimmer {
3105f6bc909eSSimon Trimmer 	struct cs_dsp_region const *mem = cs_dsp_find_region(dsp, mem_type);
3106f6bc909eSSimon Trimmer 	__be32 val = cpu_to_be32(data & 0x00ffffffu);
3107f6bc909eSSimon Trimmer 	unsigned int reg;
3108f6bc909eSSimon Trimmer 
31095065cfabSCharles Keepax 	lockdep_assert_held(&dsp->pwr_lock);
31105065cfabSCharles Keepax 
3111f6bc909eSSimon Trimmer 	if (!mem)
3112f6bc909eSSimon Trimmer 		return -EINVAL;
3113f6bc909eSSimon Trimmer 
3114f6bc909eSSimon Trimmer 	reg = dsp->ops->region_to_reg(mem, mem_addr);
3115f6bc909eSSimon Trimmer 
3116f6bc909eSSimon Trimmer 	return regmap_raw_write(dsp->regmap, reg, &val, sizeof(val));
3117f6bc909eSSimon Trimmer }
3118e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_write_data_word, FW_CS_DSP);
3119f6bc909eSSimon Trimmer 
3120f6bc909eSSimon Trimmer /**
3121f6bc909eSSimon Trimmer  * cs_dsp_remove_padding() - Convert unpacked words to packed bytes
3122f6bc909eSSimon Trimmer  * @buf: buffer containing DSP words read from DSP memory
3123f6bc909eSSimon Trimmer  * @nwords: number of words to convert
3124f6bc909eSSimon Trimmer  *
3125f6bc909eSSimon Trimmer  * DSP words from the register map have pad bytes and the data bytes
3126f6bc909eSSimon Trimmer  * are in swapped order. This swaps to the native endian order and
3127f6bc909eSSimon Trimmer  * strips the pad bytes.
3128f6bc909eSSimon Trimmer  */
cs_dsp_remove_padding(u32 * buf,int nwords)3129f6bc909eSSimon Trimmer void cs_dsp_remove_padding(u32 *buf, int nwords)
3130f6bc909eSSimon Trimmer {
3131f6bc909eSSimon Trimmer 	const __be32 *pack_in = (__be32 *)buf;
3132f6bc909eSSimon Trimmer 	u8 *pack_out = (u8 *)buf;
3133f6bc909eSSimon Trimmer 	int i;
3134f6bc909eSSimon Trimmer 
3135f6bc909eSSimon Trimmer 	for (i = 0; i < nwords; i++) {
3136f6bc909eSSimon Trimmer 		u32 word = be32_to_cpu(*pack_in++);
3137f6bc909eSSimon Trimmer 		*pack_out++ = (u8)word;
3138f6bc909eSSimon Trimmer 		*pack_out++ = (u8)(word >> 8);
3139f6bc909eSSimon Trimmer 		*pack_out++ = (u8)(word >> 16);
3140f6bc909eSSimon Trimmer 	}
3141f6bc909eSSimon Trimmer }
3142e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_remove_padding, FW_CS_DSP);
3143f6bc909eSSimon Trimmer 
3144f6bc909eSSimon Trimmer /**
3145f6bc909eSSimon Trimmer  * cs_dsp_adsp2_bus_error() - Handle a DSP bus error interrupt
3146f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
3147f6bc909eSSimon Trimmer  *
3148f6bc909eSSimon Trimmer  * The firmware and DSP state will be logged for future analysis.
3149f6bc909eSSimon Trimmer  */
cs_dsp_adsp2_bus_error(struct cs_dsp * dsp)3150f6bc909eSSimon Trimmer void cs_dsp_adsp2_bus_error(struct cs_dsp *dsp)
3151f6bc909eSSimon Trimmer {
3152f6bc909eSSimon Trimmer 	unsigned int val;
3153f6bc909eSSimon Trimmer 	struct regmap *regmap = dsp->regmap;
3154f6bc909eSSimon Trimmer 	int ret = 0;
3155f6bc909eSSimon Trimmer 
3156f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
3157f6bc909eSSimon Trimmer 
3158f6bc909eSSimon Trimmer 	ret = regmap_read(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL, &val);
3159f6bc909eSSimon Trimmer 	if (ret) {
3160f6bc909eSSimon Trimmer 		cs_dsp_err(dsp,
3161f6bc909eSSimon Trimmer 			   "Failed to read Region Lock Ctrl register: %d\n", ret);
3162f6bc909eSSimon Trimmer 		goto error;
3163f6bc909eSSimon Trimmer 	}
3164f6bc909eSSimon Trimmer 
3165f6bc909eSSimon Trimmer 	if (val & ADSP2_WDT_TIMEOUT_STS_MASK) {
3166f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "watchdog timeout error\n");
3167f6bc909eSSimon Trimmer 		dsp->ops->stop_watchdog(dsp);
3168f6bc909eSSimon Trimmer 		if (dsp->client_ops->watchdog_expired)
3169f6bc909eSSimon Trimmer 			dsp->client_ops->watchdog_expired(dsp);
3170f6bc909eSSimon Trimmer 	}
3171f6bc909eSSimon Trimmer 
3172f6bc909eSSimon Trimmer 	if (val & (ADSP2_ADDR_ERR_MASK | ADSP2_REGION_LOCK_ERR_MASK)) {
3173f6bc909eSSimon Trimmer 		if (val & ADSP2_ADDR_ERR_MASK)
3174f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "bus error: address error\n");
3175f6bc909eSSimon Trimmer 		else
3176f6bc909eSSimon Trimmer 			cs_dsp_err(dsp, "bus error: region lock error\n");
3177f6bc909eSSimon Trimmer 
3178f6bc909eSSimon Trimmer 		ret = regmap_read(regmap, dsp->base + ADSP2_BUS_ERR_ADDR, &val);
3179f6bc909eSSimon Trimmer 		if (ret) {
3180f6bc909eSSimon Trimmer 			cs_dsp_err(dsp,
3181f6bc909eSSimon Trimmer 				   "Failed to read Bus Err Addr register: %d\n",
3182f6bc909eSSimon Trimmer 				   ret);
3183f6bc909eSSimon Trimmer 			goto error;
3184f6bc909eSSimon Trimmer 		}
3185f6bc909eSSimon Trimmer 
3186f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "bus error address = 0x%x\n",
3187f6bc909eSSimon Trimmer 			   val & ADSP2_BUS_ERR_ADDR_MASK);
3188f6bc909eSSimon Trimmer 
3189f6bc909eSSimon Trimmer 		ret = regmap_read(regmap,
3190f6bc909eSSimon Trimmer 				  dsp->base + ADSP2_PMEM_ERR_ADDR_XMEM_ERR_ADDR,
3191f6bc909eSSimon Trimmer 				  &val);
3192f6bc909eSSimon Trimmer 		if (ret) {
3193f6bc909eSSimon Trimmer 			cs_dsp_err(dsp,
3194f6bc909eSSimon Trimmer 				   "Failed to read Pmem Xmem Err Addr register: %d\n",
3195f6bc909eSSimon Trimmer 				   ret);
3196f6bc909eSSimon Trimmer 			goto error;
3197f6bc909eSSimon Trimmer 		}
3198f6bc909eSSimon Trimmer 
3199f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "xmem error address = 0x%x\n",
3200f6bc909eSSimon Trimmer 			   val & ADSP2_XMEM_ERR_ADDR_MASK);
3201f6bc909eSSimon Trimmer 		cs_dsp_err(dsp, "pmem error address = 0x%x\n",
3202f6bc909eSSimon Trimmer 			   (val & ADSP2_PMEM_ERR_ADDR_MASK) >>
3203f6bc909eSSimon Trimmer 			   ADSP2_PMEM_ERR_ADDR_SHIFT);
3204f6bc909eSSimon Trimmer 	}
3205f6bc909eSSimon Trimmer 
3206f6bc909eSSimon Trimmer 	regmap_update_bits(regmap, dsp->base + ADSP2_LOCK_REGION_CTRL,
3207f6bc909eSSimon Trimmer 			   ADSP2_CTRL_ERR_EINT, ADSP2_CTRL_ERR_EINT);
3208f6bc909eSSimon Trimmer 
3209f6bc909eSSimon Trimmer error:
3210f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
3211f6bc909eSSimon Trimmer }
3212e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_adsp2_bus_error, FW_CS_DSP);
3213f6bc909eSSimon Trimmer 
3214f6bc909eSSimon Trimmer /**
3215f6bc909eSSimon Trimmer  * cs_dsp_halo_bus_error() - Handle a DSP bus error interrupt
3216f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
3217f6bc909eSSimon Trimmer  *
3218f6bc909eSSimon Trimmer  * The firmware and DSP state will be logged for future analysis.
3219f6bc909eSSimon Trimmer  */
cs_dsp_halo_bus_error(struct cs_dsp * dsp)3220f6bc909eSSimon Trimmer void cs_dsp_halo_bus_error(struct cs_dsp *dsp)
3221f6bc909eSSimon Trimmer {
3222f6bc909eSSimon Trimmer 	struct regmap *regmap = dsp->regmap;
3223f6bc909eSSimon Trimmer 	unsigned int fault[6];
3224f6bc909eSSimon Trimmer 	struct reg_sequence clear[] = {
3225f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_XM_VIO_STATUS,     0x0 },
3226f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_YM_VIO_STATUS,     0x0 },
3227f6bc909eSSimon Trimmer 		{ dsp->base + HALO_MPU_PM_VIO_STATUS,     0x0 },
3228f6bc909eSSimon Trimmer 	};
3229f6bc909eSSimon Trimmer 	int ret;
3230f6bc909eSSimon Trimmer 
3231f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
3232f6bc909eSSimon Trimmer 
3233f6bc909eSSimon Trimmer 	ret = regmap_read(regmap, dsp->base_sysinfo + HALO_AHBM_WINDOW_DEBUG_1,
3234f6bc909eSSimon Trimmer 			  fault);
3235f6bc909eSSimon Trimmer 	if (ret) {
3236f6bc909eSSimon Trimmer 		cs_dsp_warn(dsp, "Failed to read AHB DEBUG_1: %d\n", ret);
3237f6bc909eSSimon Trimmer 		goto exit_unlock;
3238f6bc909eSSimon Trimmer 	}
3239f6bc909eSSimon Trimmer 
3240f6bc909eSSimon Trimmer 	cs_dsp_warn(dsp, "AHB: STATUS: 0x%x ADDR: 0x%x\n",
3241f6bc909eSSimon Trimmer 		    *fault & HALO_AHBM_FLAGS_ERR_MASK,
3242f6bc909eSSimon Trimmer 		    (*fault & HALO_AHBM_CORE_ERR_ADDR_MASK) >>
3243f6bc909eSSimon Trimmer 		    HALO_AHBM_CORE_ERR_ADDR_SHIFT);
3244f6bc909eSSimon Trimmer 
3245f6bc909eSSimon Trimmer 	ret = regmap_read(regmap, dsp->base_sysinfo + HALO_AHBM_WINDOW_DEBUG_0,
3246f6bc909eSSimon Trimmer 			  fault);
3247f6bc909eSSimon Trimmer 	if (ret) {
3248f6bc909eSSimon Trimmer 		cs_dsp_warn(dsp, "Failed to read AHB DEBUG_0: %d\n", ret);
3249f6bc909eSSimon Trimmer 		goto exit_unlock;
3250f6bc909eSSimon Trimmer 	}
3251f6bc909eSSimon Trimmer 
3252f6bc909eSSimon Trimmer 	cs_dsp_warn(dsp, "AHB: SYS_ADDR: 0x%x\n", *fault);
3253f6bc909eSSimon Trimmer 
3254f6bc909eSSimon Trimmer 	ret = regmap_bulk_read(regmap, dsp->base + HALO_MPU_XM_VIO_ADDR,
3255f6bc909eSSimon Trimmer 			       fault, ARRAY_SIZE(fault));
3256f6bc909eSSimon Trimmer 	if (ret) {
3257f6bc909eSSimon Trimmer 		cs_dsp_warn(dsp, "Failed to read MPU fault info: %d\n", ret);
3258f6bc909eSSimon Trimmer 		goto exit_unlock;
3259f6bc909eSSimon Trimmer 	}
3260f6bc909eSSimon Trimmer 
3261f6bc909eSSimon Trimmer 	cs_dsp_warn(dsp, "XM: STATUS:0x%x ADDR:0x%x\n", fault[1], fault[0]);
3262f6bc909eSSimon Trimmer 	cs_dsp_warn(dsp, "YM: STATUS:0x%x ADDR:0x%x\n", fault[3], fault[2]);
3263f6bc909eSSimon Trimmer 	cs_dsp_warn(dsp, "PM: STATUS:0x%x ADDR:0x%x\n", fault[5], fault[4]);
3264f6bc909eSSimon Trimmer 
3265f6bc909eSSimon Trimmer 	ret = regmap_multi_reg_write(dsp->regmap, clear, ARRAY_SIZE(clear));
3266f6bc909eSSimon Trimmer 	if (ret)
3267f6bc909eSSimon Trimmer 		cs_dsp_warn(dsp, "Failed to clear MPU status: %d\n", ret);
3268f6bc909eSSimon Trimmer 
3269f6bc909eSSimon Trimmer exit_unlock:
3270f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
3271f6bc909eSSimon Trimmer }
3272e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_halo_bus_error, FW_CS_DSP);
3273f6bc909eSSimon Trimmer 
3274f6bc909eSSimon Trimmer /**
3275f6bc909eSSimon Trimmer  * cs_dsp_halo_wdt_expire() - Handle DSP watchdog expiry
3276f6bc909eSSimon Trimmer  * @dsp: pointer to DSP structure
3277f6bc909eSSimon Trimmer  *
3278f6bc909eSSimon Trimmer  * This is logged for future analysis.
3279f6bc909eSSimon Trimmer  */
cs_dsp_halo_wdt_expire(struct cs_dsp * dsp)3280f6bc909eSSimon Trimmer void cs_dsp_halo_wdt_expire(struct cs_dsp *dsp)
3281f6bc909eSSimon Trimmer {
3282f6bc909eSSimon Trimmer 	mutex_lock(&dsp->pwr_lock);
3283f6bc909eSSimon Trimmer 
3284f6bc909eSSimon Trimmer 	cs_dsp_warn(dsp, "WDT Expiry Fault\n");
3285f6bc909eSSimon Trimmer 
3286f6bc909eSSimon Trimmer 	dsp->ops->stop_watchdog(dsp);
3287f6bc909eSSimon Trimmer 	if (dsp->client_ops->watchdog_expired)
3288f6bc909eSSimon Trimmer 		dsp->client_ops->watchdog_expired(dsp);
3289f6bc909eSSimon Trimmer 
3290f6bc909eSSimon Trimmer 	mutex_unlock(&dsp->pwr_lock);
3291f6bc909eSSimon Trimmer }
3292e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_halo_wdt_expire, FW_CS_DSP);
3293f6bc909eSSimon Trimmer 
3294f6bc909eSSimon Trimmer static const struct cs_dsp_ops cs_dsp_adsp1_ops = {
3295f6bc909eSSimon Trimmer 	.validate_version = cs_dsp_validate_version,
3296f6bc909eSSimon Trimmer 	.parse_sizes = cs_dsp_adsp1_parse_sizes,
3297f6bc909eSSimon Trimmer 	.region_to_reg = cs_dsp_region_to_reg,
3298f6bc909eSSimon Trimmer };
3299f6bc909eSSimon Trimmer 
3300f6bc909eSSimon Trimmer static const struct cs_dsp_ops cs_dsp_adsp2_ops[] = {
3301f6bc909eSSimon Trimmer 	{
3302f6bc909eSSimon Trimmer 		.parse_sizes = cs_dsp_adsp2_parse_sizes,
3303f6bc909eSSimon Trimmer 		.validate_version = cs_dsp_validate_version,
3304f6bc909eSSimon Trimmer 		.setup_algs = cs_dsp_adsp2_setup_algs,
3305f6bc909eSSimon Trimmer 		.region_to_reg = cs_dsp_region_to_reg,
3306f6bc909eSSimon Trimmer 
3307f6bc909eSSimon Trimmer 		.show_fw_status = cs_dsp_adsp2_show_fw_status,
3308f6bc909eSSimon Trimmer 
3309f6bc909eSSimon Trimmer 		.enable_memory = cs_dsp_adsp2_enable_memory,
3310f6bc909eSSimon Trimmer 		.disable_memory = cs_dsp_adsp2_disable_memory,
3311f6bc909eSSimon Trimmer 
3312f6bc909eSSimon Trimmer 		.enable_core = cs_dsp_adsp2_enable_core,
3313f6bc909eSSimon Trimmer 		.disable_core = cs_dsp_adsp2_disable_core,
3314f6bc909eSSimon Trimmer 
3315f6bc909eSSimon Trimmer 		.start_core = cs_dsp_adsp2_start_core,
3316f6bc909eSSimon Trimmer 		.stop_core = cs_dsp_adsp2_stop_core,
3317f6bc909eSSimon Trimmer 
3318f6bc909eSSimon Trimmer 	},
3319f6bc909eSSimon Trimmer 	{
3320f6bc909eSSimon Trimmer 		.parse_sizes = cs_dsp_adsp2_parse_sizes,
3321f6bc909eSSimon Trimmer 		.validate_version = cs_dsp_validate_version,
3322f6bc909eSSimon Trimmer 		.setup_algs = cs_dsp_adsp2_setup_algs,
3323f6bc909eSSimon Trimmer 		.region_to_reg = cs_dsp_region_to_reg,
3324f6bc909eSSimon Trimmer 
3325f6bc909eSSimon Trimmer 		.show_fw_status = cs_dsp_adsp2v2_show_fw_status,
3326f6bc909eSSimon Trimmer 
3327f6bc909eSSimon Trimmer 		.enable_memory = cs_dsp_adsp2_enable_memory,
3328f6bc909eSSimon Trimmer 		.disable_memory = cs_dsp_adsp2_disable_memory,
3329f6bc909eSSimon Trimmer 		.lock_memory = cs_dsp_adsp2_lock,
3330f6bc909eSSimon Trimmer 
3331f6bc909eSSimon Trimmer 		.enable_core = cs_dsp_adsp2v2_enable_core,
3332f6bc909eSSimon Trimmer 		.disable_core = cs_dsp_adsp2v2_disable_core,
3333f6bc909eSSimon Trimmer 
3334f6bc909eSSimon Trimmer 		.start_core = cs_dsp_adsp2_start_core,
3335f6bc909eSSimon Trimmer 		.stop_core = cs_dsp_adsp2_stop_core,
3336f6bc909eSSimon Trimmer 	},
3337f6bc909eSSimon Trimmer 	{
3338f6bc909eSSimon Trimmer 		.parse_sizes = cs_dsp_adsp2_parse_sizes,
3339f6bc909eSSimon Trimmer 		.validate_version = cs_dsp_validate_version,
3340f6bc909eSSimon Trimmer 		.setup_algs = cs_dsp_adsp2_setup_algs,
3341f6bc909eSSimon Trimmer 		.region_to_reg = cs_dsp_region_to_reg,
3342f6bc909eSSimon Trimmer 
3343f6bc909eSSimon Trimmer 		.show_fw_status = cs_dsp_adsp2v2_show_fw_status,
3344f6bc909eSSimon Trimmer 		.stop_watchdog = cs_dsp_stop_watchdog,
3345f6bc909eSSimon Trimmer 
3346f6bc909eSSimon Trimmer 		.enable_memory = cs_dsp_adsp2_enable_memory,
3347f6bc909eSSimon Trimmer 		.disable_memory = cs_dsp_adsp2_disable_memory,
3348f6bc909eSSimon Trimmer 		.lock_memory = cs_dsp_adsp2_lock,
3349f6bc909eSSimon Trimmer 
3350f6bc909eSSimon Trimmer 		.enable_core = cs_dsp_adsp2v2_enable_core,
3351f6bc909eSSimon Trimmer 		.disable_core = cs_dsp_adsp2v2_disable_core,
3352f6bc909eSSimon Trimmer 
3353f6bc909eSSimon Trimmer 		.start_core = cs_dsp_adsp2_start_core,
3354f6bc909eSSimon Trimmer 		.stop_core = cs_dsp_adsp2_stop_core,
3355f6bc909eSSimon Trimmer 	},
3356f6bc909eSSimon Trimmer };
3357f6bc909eSSimon Trimmer 
3358f6bc909eSSimon Trimmer static const struct cs_dsp_ops cs_dsp_halo_ops = {
3359f6bc909eSSimon Trimmer 	.parse_sizes = cs_dsp_adsp2_parse_sizes,
3360f6bc909eSSimon Trimmer 	.validate_version = cs_dsp_halo_validate_version,
3361f6bc909eSSimon Trimmer 	.setup_algs = cs_dsp_halo_setup_algs,
3362f6bc909eSSimon Trimmer 	.region_to_reg = cs_dsp_halo_region_to_reg,
3363f6bc909eSSimon Trimmer 
3364f6bc909eSSimon Trimmer 	.show_fw_status = cs_dsp_halo_show_fw_status,
3365f6bc909eSSimon Trimmer 	.stop_watchdog = cs_dsp_halo_stop_watchdog,
3366f6bc909eSSimon Trimmer 
3367f6bc909eSSimon Trimmer 	.lock_memory = cs_dsp_halo_configure_mpu,
3368f6bc909eSSimon Trimmer 
3369f6bc909eSSimon Trimmer 	.start_core = cs_dsp_halo_start_core,
3370f6bc909eSSimon Trimmer 	.stop_core = cs_dsp_halo_stop_core,
3371f6bc909eSSimon Trimmer };
3372f6bc909eSSimon Trimmer 
33737062e1c7SSimon Trimmer static const struct cs_dsp_ops cs_dsp_halo_ao_ops = {
33747062e1c7SSimon Trimmer 	.parse_sizes = cs_dsp_adsp2_parse_sizes,
33757062e1c7SSimon Trimmer 	.validate_version = cs_dsp_halo_validate_version,
33767062e1c7SSimon Trimmer 	.setup_algs = cs_dsp_halo_setup_algs,
33777062e1c7SSimon Trimmer 	.region_to_reg = cs_dsp_halo_region_to_reg,
33787062e1c7SSimon Trimmer 	.show_fw_status = cs_dsp_halo_show_fw_status,
33797062e1c7SSimon Trimmer };
33807062e1c7SSimon Trimmer 
3381a4b97655SCharles Keepax /**
3382a4b97655SCharles Keepax  * cs_dsp_chunk_write() - Format data to a DSP memory chunk
3383a4b97655SCharles Keepax  * @ch: Pointer to the chunk structure
3384a4b97655SCharles Keepax  * @nbits: Number of bits to write
3385a4b97655SCharles Keepax  * @val: Value to write
3386a4b97655SCharles Keepax  *
3387a4b97655SCharles Keepax  * This function sequentially writes values into the format required for DSP
3388a4b97655SCharles Keepax  * memory, it handles both inserting of the padding bytes and converting to
3389a4b97655SCharles Keepax  * big endian. Note that data is only committed to the chunk when a whole DSP
3390a4b97655SCharles Keepax  * words worth of data is available.
3391a4b97655SCharles Keepax  *
3392a4b97655SCharles Keepax  * Return: Zero for success, a negative number on error.
3393a4b97655SCharles Keepax  */
cs_dsp_chunk_write(struct cs_dsp_chunk * ch,int nbits,u32 val)3394a4b97655SCharles Keepax int cs_dsp_chunk_write(struct cs_dsp_chunk *ch, int nbits, u32 val)
3395a4b97655SCharles Keepax {
3396a4b97655SCharles Keepax 	int nwrite, i;
3397a4b97655SCharles Keepax 
3398a4b97655SCharles Keepax 	nwrite = min(CS_DSP_DATA_WORD_BITS - ch->cachebits, nbits);
3399a4b97655SCharles Keepax 
3400a4b97655SCharles Keepax 	ch->cache <<= nwrite;
3401a4b97655SCharles Keepax 	ch->cache |= val >> (nbits - nwrite);
3402a4b97655SCharles Keepax 	ch->cachebits += nwrite;
3403a4b97655SCharles Keepax 	nbits -= nwrite;
3404a4b97655SCharles Keepax 
3405a4b97655SCharles Keepax 	if (ch->cachebits == CS_DSP_DATA_WORD_BITS) {
3406a4b97655SCharles Keepax 		if (cs_dsp_chunk_end(ch))
3407a4b97655SCharles Keepax 			return -ENOSPC;
3408a4b97655SCharles Keepax 
3409a4b97655SCharles Keepax 		ch->cache &= 0xFFFFFF;
3410a4b97655SCharles Keepax 		for (i = 0; i < sizeof(ch->cache); i++, ch->cache <<= BITS_PER_BYTE)
3411a4b97655SCharles Keepax 			*ch->data++ = (ch->cache & 0xFF000000) >> CS_DSP_DATA_WORD_BITS;
3412a4b97655SCharles Keepax 
3413a4b97655SCharles Keepax 		ch->bytes += sizeof(ch->cache);
3414a4b97655SCharles Keepax 		ch->cachebits = 0;
3415a4b97655SCharles Keepax 	}
3416a4b97655SCharles Keepax 
3417a4b97655SCharles Keepax 	if (nbits)
3418a4b97655SCharles Keepax 		return cs_dsp_chunk_write(ch, nbits, val);
3419a4b97655SCharles Keepax 
3420a4b97655SCharles Keepax 	return 0;
3421a4b97655SCharles Keepax }
3422e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_chunk_write, FW_CS_DSP);
3423a4b97655SCharles Keepax 
3424a4b97655SCharles Keepax /**
3425a4b97655SCharles Keepax  * cs_dsp_chunk_flush() - Pad remaining data with zero and commit to chunk
3426a4b97655SCharles Keepax  * @ch: Pointer to the chunk structure
3427a4b97655SCharles Keepax  *
3428a4b97655SCharles Keepax  * As cs_dsp_chunk_write only writes data when a whole DSP word is ready to
3429a4b97655SCharles Keepax  * be written out it is possible that some data will remain in the cache, this
3430a4b97655SCharles Keepax  * function will pad that data with zeros upto a whole DSP word and write out.
3431a4b97655SCharles Keepax  *
3432a4b97655SCharles Keepax  * Return: Zero for success, a negative number on error.
3433a4b97655SCharles Keepax  */
cs_dsp_chunk_flush(struct cs_dsp_chunk * ch)3434a4b97655SCharles Keepax int cs_dsp_chunk_flush(struct cs_dsp_chunk *ch)
3435a4b97655SCharles Keepax {
3436a4b97655SCharles Keepax 	if (!ch->cachebits)
3437a4b97655SCharles Keepax 		return 0;
3438a4b97655SCharles Keepax 
3439a4b97655SCharles Keepax 	return cs_dsp_chunk_write(ch, CS_DSP_DATA_WORD_BITS - ch->cachebits, 0);
3440a4b97655SCharles Keepax }
3441e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_chunk_flush, FW_CS_DSP);
3442a4b97655SCharles Keepax 
3443a4b97655SCharles Keepax /**
3444a4b97655SCharles Keepax  * cs_dsp_chunk_read() - Parse data from a DSP memory chunk
3445a4b97655SCharles Keepax  * @ch: Pointer to the chunk structure
3446a4b97655SCharles Keepax  * @nbits: Number of bits to read
3447a4b97655SCharles Keepax  *
3448a4b97655SCharles Keepax  * This function sequentially reads values from a DSP memory formatted buffer,
3449a4b97655SCharles Keepax  * it handles both removing of the padding bytes and converting from big endian.
3450a4b97655SCharles Keepax  *
3451a4b97655SCharles Keepax  * Return: A negative number is returned on error, otherwise the read value.
3452a4b97655SCharles Keepax  */
cs_dsp_chunk_read(struct cs_dsp_chunk * ch,int nbits)3453a4b97655SCharles Keepax int cs_dsp_chunk_read(struct cs_dsp_chunk *ch, int nbits)
3454a4b97655SCharles Keepax {
3455a4b97655SCharles Keepax 	int nread, i;
3456a4b97655SCharles Keepax 	u32 result;
3457a4b97655SCharles Keepax 
3458a4b97655SCharles Keepax 	if (!ch->cachebits) {
3459a4b97655SCharles Keepax 		if (cs_dsp_chunk_end(ch))
3460a4b97655SCharles Keepax 			return -ENOSPC;
3461a4b97655SCharles Keepax 
3462a4b97655SCharles Keepax 		ch->cache = 0;
3463a4b97655SCharles Keepax 		ch->cachebits = CS_DSP_DATA_WORD_BITS;
3464a4b97655SCharles Keepax 
3465a4b97655SCharles Keepax 		for (i = 0; i < sizeof(ch->cache); i++, ch->cache <<= BITS_PER_BYTE)
3466a4b97655SCharles Keepax 			ch->cache |= *ch->data++;
3467a4b97655SCharles Keepax 
3468a4b97655SCharles Keepax 		ch->bytes += sizeof(ch->cache);
3469a4b97655SCharles Keepax 	}
3470a4b97655SCharles Keepax 
3471a4b97655SCharles Keepax 	nread = min(ch->cachebits, nbits);
3472a4b97655SCharles Keepax 	nbits -= nread;
3473a4b97655SCharles Keepax 
3474a4b97655SCharles Keepax 	result = ch->cache >> ((sizeof(ch->cache) * BITS_PER_BYTE) - nread);
3475a4b97655SCharles Keepax 	ch->cache <<= nread;
3476a4b97655SCharles Keepax 	ch->cachebits -= nread;
3477a4b97655SCharles Keepax 
3478a4b97655SCharles Keepax 	if (nbits)
3479a4b97655SCharles Keepax 		result = (result << nbits) | cs_dsp_chunk_read(ch, nbits);
3480a4b97655SCharles Keepax 
3481a4b97655SCharles Keepax 	return result;
3482a4b97655SCharles Keepax }
3483e57d904aSRichard Fitzgerald EXPORT_SYMBOL_NS_GPL(cs_dsp_chunk_read, FW_CS_DSP);
3484a4b97655SCharles Keepax 
3485205fdba5SJames Ogletree 
3486205fdba5SJames Ogletree struct cs_dsp_wseq_op {
3487205fdba5SJames Ogletree 	struct list_head list;
3488205fdba5SJames Ogletree 	u32 address;
3489205fdba5SJames Ogletree 	u32 data;
3490205fdba5SJames Ogletree 	u16 offset;
3491205fdba5SJames Ogletree 	u8 operation;
3492205fdba5SJames Ogletree };
3493205fdba5SJames Ogletree 
cs_dsp_wseq_clear(struct cs_dsp * dsp,struct cs_dsp_wseq * wseq)3494205fdba5SJames Ogletree static void cs_dsp_wseq_clear(struct cs_dsp *dsp, struct cs_dsp_wseq *wseq)
3495205fdba5SJames Ogletree {
3496205fdba5SJames Ogletree 	struct cs_dsp_wseq_op *op, *op_tmp;
3497205fdba5SJames Ogletree 
3498205fdba5SJames Ogletree 	list_for_each_entry_safe(op, op_tmp, &wseq->ops, list) {
3499205fdba5SJames Ogletree 		list_del(&op->list);
3500205fdba5SJames Ogletree 		devm_kfree(dsp->dev, op);
3501205fdba5SJames Ogletree 	}
3502205fdba5SJames Ogletree }
3503205fdba5SJames Ogletree 
cs_dsp_populate_wseq(struct cs_dsp * dsp,struct cs_dsp_wseq * wseq)3504205fdba5SJames Ogletree static int cs_dsp_populate_wseq(struct cs_dsp *dsp, struct cs_dsp_wseq *wseq)
3505205fdba5SJames Ogletree {
3506205fdba5SJames Ogletree 	struct cs_dsp_wseq_op *op = NULL;
3507205fdba5SJames Ogletree 	struct cs_dsp_chunk chunk;
3508205fdba5SJames Ogletree 	u8 *words;
3509205fdba5SJames Ogletree 	int ret;
3510205fdba5SJames Ogletree 
3511205fdba5SJames Ogletree 	if (!wseq->ctl) {
3512205fdba5SJames Ogletree 		cs_dsp_err(dsp, "No control for write sequence\n");
3513205fdba5SJames Ogletree 		return -EINVAL;
3514205fdba5SJames Ogletree 	}
3515205fdba5SJames Ogletree 
3516205fdba5SJames Ogletree 	words = kzalloc(wseq->ctl->len, GFP_KERNEL);
3517205fdba5SJames Ogletree 	if (!words)
3518205fdba5SJames Ogletree 		return -ENOMEM;
3519205fdba5SJames Ogletree 
3520205fdba5SJames Ogletree 	ret = cs_dsp_coeff_read_ctrl(wseq->ctl, 0, words, wseq->ctl->len);
3521205fdba5SJames Ogletree 	if (ret) {
3522205fdba5SJames Ogletree 		cs_dsp_err(dsp, "Failed to read %s: %d\n", wseq->ctl->subname, ret);
3523205fdba5SJames Ogletree 		goto err_free;
3524205fdba5SJames Ogletree 	}
3525205fdba5SJames Ogletree 
3526205fdba5SJames Ogletree 	INIT_LIST_HEAD(&wseq->ops);
3527205fdba5SJames Ogletree 
3528205fdba5SJames Ogletree 	chunk = cs_dsp_chunk(words, wseq->ctl->len);
3529205fdba5SJames Ogletree 
3530205fdba5SJames Ogletree 	while (!cs_dsp_chunk_end(&chunk)) {
3531205fdba5SJames Ogletree 		op = devm_kzalloc(dsp->dev, sizeof(*op), GFP_KERNEL);
3532205fdba5SJames Ogletree 		if (!op) {
3533205fdba5SJames Ogletree 			ret = -ENOMEM;
3534205fdba5SJames Ogletree 			goto err_free;
3535205fdba5SJames Ogletree 		}
3536205fdba5SJames Ogletree 
3537205fdba5SJames Ogletree 		op->offset = cs_dsp_chunk_bytes(&chunk);
3538205fdba5SJames Ogletree 		op->operation = cs_dsp_chunk_read(&chunk, 8);
3539205fdba5SJames Ogletree 
3540205fdba5SJames Ogletree 		switch (op->operation) {
3541205fdba5SJames Ogletree 		case CS_DSP_WSEQ_END:
3542205fdba5SJames Ogletree 			op->data = WSEQ_END_OF_SCRIPT;
3543205fdba5SJames Ogletree 			break;
3544205fdba5SJames Ogletree 		case CS_DSP_WSEQ_UNLOCK:
3545205fdba5SJames Ogletree 			op->data = cs_dsp_chunk_read(&chunk, 16);
3546205fdba5SJames Ogletree 			break;
3547205fdba5SJames Ogletree 		case CS_DSP_WSEQ_ADDR8:
3548205fdba5SJames Ogletree 			op->address = cs_dsp_chunk_read(&chunk, 8);
3549205fdba5SJames Ogletree 			op->data = cs_dsp_chunk_read(&chunk, 32);
3550205fdba5SJames Ogletree 			break;
3551205fdba5SJames Ogletree 		case CS_DSP_WSEQ_H16:
3552205fdba5SJames Ogletree 		case CS_DSP_WSEQ_L16:
3553205fdba5SJames Ogletree 			op->address = cs_dsp_chunk_read(&chunk, 24);
3554205fdba5SJames Ogletree 			op->data = cs_dsp_chunk_read(&chunk, 16);
3555205fdba5SJames Ogletree 			break;
3556205fdba5SJames Ogletree 		case CS_DSP_WSEQ_FULL:
3557205fdba5SJames Ogletree 			op->address = cs_dsp_chunk_read(&chunk, 32);
3558205fdba5SJames Ogletree 			op->data = cs_dsp_chunk_read(&chunk, 32);
3559205fdba5SJames Ogletree 			break;
3560205fdba5SJames Ogletree 		default:
3561205fdba5SJames Ogletree 			ret = -EINVAL;
3562205fdba5SJames Ogletree 			cs_dsp_err(dsp, "Unsupported op: %X\n", op->operation);
3563205fdba5SJames Ogletree 			devm_kfree(dsp->dev, op);
3564205fdba5SJames Ogletree 			goto err_free;
3565205fdba5SJames Ogletree 		}
3566205fdba5SJames Ogletree 
3567205fdba5SJames Ogletree 		list_add_tail(&op->list, &wseq->ops);
3568205fdba5SJames Ogletree 
3569205fdba5SJames Ogletree 		if (op->operation == CS_DSP_WSEQ_END)
3570205fdba5SJames Ogletree 			break;
3571205fdba5SJames Ogletree 	}
3572205fdba5SJames Ogletree 
3573205fdba5SJames Ogletree 	if (op && op->operation != CS_DSP_WSEQ_END) {
3574205fdba5SJames Ogletree 		cs_dsp_err(dsp, "%s missing end terminator\n", wseq->ctl->subname);
3575205fdba5SJames Ogletree 		ret = -ENOENT;
3576205fdba5SJames Ogletree 	}
3577205fdba5SJames Ogletree 
3578205fdba5SJames Ogletree err_free:
3579205fdba5SJames Ogletree 	kfree(words);
3580205fdba5SJames Ogletree 
3581205fdba5SJames Ogletree 	return ret;
3582205fdba5SJames Ogletree }
3583205fdba5SJames Ogletree 
3584205fdba5SJames Ogletree /**
3585205fdba5SJames Ogletree  * cs_dsp_wseq_init() - Initialize write sequences contained within the loaded DSP firmware
3586205fdba5SJames Ogletree  * @dsp: Pointer to DSP structure
3587205fdba5SJames Ogletree  * @wseqs: List of write sequences to initialize
3588205fdba5SJames Ogletree  * @num_wseqs: Number of write sequences to initialize
3589205fdba5SJames Ogletree  *
3590205fdba5SJames Ogletree  * Return: Zero for success, a negative number on error.
3591205fdba5SJames Ogletree  */
cs_dsp_wseq_init(struct cs_dsp * dsp,struct cs_dsp_wseq * wseqs,unsigned int num_wseqs)3592205fdba5SJames Ogletree int cs_dsp_wseq_init(struct cs_dsp *dsp, struct cs_dsp_wseq *wseqs, unsigned int num_wseqs)
3593205fdba5SJames Ogletree {
3594205fdba5SJames Ogletree 	int i, ret;
3595205fdba5SJames Ogletree 
3596205fdba5SJames Ogletree 	lockdep_assert_held(&dsp->pwr_lock);
3597205fdba5SJames Ogletree 
3598205fdba5SJames Ogletree 	for (i = 0; i < num_wseqs; i++) {
3599205fdba5SJames Ogletree 		ret = cs_dsp_populate_wseq(dsp, &wseqs[i]);
3600205fdba5SJames Ogletree 		if (ret) {
3601205fdba5SJames Ogletree 			cs_dsp_wseq_clear(dsp, &wseqs[i]);
3602205fdba5SJames Ogletree 			return ret;
3603205fdba5SJames Ogletree 		}
3604205fdba5SJames Ogletree 	}
3605205fdba5SJames Ogletree 
3606205fdba5SJames Ogletree 	return 0;
3607205fdba5SJames Ogletree }
3608205fdba5SJames Ogletree EXPORT_SYMBOL_NS_GPL(cs_dsp_wseq_init, FW_CS_DSP);
3609205fdba5SJames Ogletree 
cs_dsp_wseq_find_op(u32 addr,u8 op_code,struct list_head * wseq_ops)3610205fdba5SJames Ogletree static struct cs_dsp_wseq_op *cs_dsp_wseq_find_op(u32 addr, u8 op_code,
3611205fdba5SJames Ogletree 						  struct list_head *wseq_ops)
3612205fdba5SJames Ogletree {
3613205fdba5SJames Ogletree 	struct cs_dsp_wseq_op *op;
3614205fdba5SJames Ogletree 
3615205fdba5SJames Ogletree 	list_for_each_entry(op, wseq_ops, list) {
3616205fdba5SJames Ogletree 		if (op->operation == op_code && op->address == addr)
3617205fdba5SJames Ogletree 			return op;
3618205fdba5SJames Ogletree 	}
3619205fdba5SJames Ogletree 
3620205fdba5SJames Ogletree 	return NULL;
3621205fdba5SJames Ogletree }
3622205fdba5SJames Ogletree 
3623205fdba5SJames Ogletree /**
3624205fdba5SJames Ogletree  * cs_dsp_wseq_write() - Add or update an entry in a write sequence
3625205fdba5SJames Ogletree  * @dsp: Pointer to a DSP structure
3626205fdba5SJames Ogletree  * @wseq: Write sequence to write to
3627205fdba5SJames Ogletree  * @addr: Address of the register to be written to
3628205fdba5SJames Ogletree  * @data: Data to be written
3629205fdba5SJames Ogletree  * @op_code: The type of operation of the new entry
3630205fdba5SJames Ogletree  * @update: If true, searches for the first entry in the write sequence with
3631205fdba5SJames Ogletree  * the same address and op_code, and replaces it. If false, creates a new entry
3632205fdba5SJames Ogletree  * at the tail
3633205fdba5SJames Ogletree  *
3634205fdba5SJames Ogletree  * This function formats register address and value pairs into the format
3635205fdba5SJames Ogletree  * required for write sequence entries, and either updates or adds the
3636205fdba5SJames Ogletree  * new entry into the write sequence.
3637205fdba5SJames Ogletree  *
3638205fdba5SJames Ogletree  * If update is set to true and no matching entry is found, it will add a new entry.
3639205fdba5SJames Ogletree  *
3640205fdba5SJames Ogletree  * Return: Zero for success, a negative number on error.
3641205fdba5SJames Ogletree  */
cs_dsp_wseq_write(struct cs_dsp * dsp,struct cs_dsp_wseq * wseq,u32 addr,u32 data,u8 op_code,bool update)3642205fdba5SJames Ogletree int cs_dsp_wseq_write(struct cs_dsp *dsp, struct cs_dsp_wseq *wseq,
3643205fdba5SJames Ogletree 		      u32 addr, u32 data, u8 op_code, bool update)
3644205fdba5SJames Ogletree {
3645205fdba5SJames Ogletree 	struct cs_dsp_wseq_op *op_end, *op_new = NULL;
3646205fdba5SJames Ogletree 	u32 words[WSEQ_OP_MAX_WORDS];
3647205fdba5SJames Ogletree 	struct cs_dsp_chunk chunk;
3648205fdba5SJames Ogletree 	int new_op_size, ret;
3649205fdba5SJames Ogletree 
3650205fdba5SJames Ogletree 	if (update)
3651205fdba5SJames Ogletree 		op_new = cs_dsp_wseq_find_op(addr, op_code, &wseq->ops);
3652205fdba5SJames Ogletree 
3653205fdba5SJames Ogletree 	/* If entry to update is not found, treat it as a new operation */
3654205fdba5SJames Ogletree 	if (!op_new) {
3655205fdba5SJames Ogletree 		op_end = cs_dsp_wseq_find_op(0, CS_DSP_WSEQ_END, &wseq->ops);
3656205fdba5SJames Ogletree 		if (!op_end) {
3657205fdba5SJames Ogletree 			cs_dsp_err(dsp, "Missing terminator for %s\n", wseq->ctl->subname);
3658205fdba5SJames Ogletree 			return -EINVAL;
3659205fdba5SJames Ogletree 		}
3660205fdba5SJames Ogletree 
3661205fdba5SJames Ogletree 		op_new = devm_kzalloc(dsp->dev, sizeof(*op_new), GFP_KERNEL);
3662205fdba5SJames Ogletree 		if (!op_new)
3663205fdba5SJames Ogletree 			return -ENOMEM;
3664205fdba5SJames Ogletree 
3665205fdba5SJames Ogletree 		op_new->operation = op_code;
3666205fdba5SJames Ogletree 		op_new->address = addr;
3667205fdba5SJames Ogletree 		op_new->offset = op_end->offset;
3668205fdba5SJames Ogletree 		update = false;
3669205fdba5SJames Ogletree 	}
3670205fdba5SJames Ogletree 
3671205fdba5SJames Ogletree 	op_new->data = data;
3672205fdba5SJames Ogletree 
3673205fdba5SJames Ogletree 	chunk = cs_dsp_chunk(words, sizeof(words));
3674205fdba5SJames Ogletree 	cs_dsp_chunk_write(&chunk, 8, op_new->operation);
3675205fdba5SJames Ogletree 
3676205fdba5SJames Ogletree 	switch (op_code) {
3677205fdba5SJames Ogletree 	case CS_DSP_WSEQ_FULL:
3678205fdba5SJames Ogletree 		cs_dsp_chunk_write(&chunk, 32, op_new->address);
3679205fdba5SJames Ogletree 		cs_dsp_chunk_write(&chunk, 32, op_new->data);
3680205fdba5SJames Ogletree 		break;
3681205fdba5SJames Ogletree 	case CS_DSP_WSEQ_L16:
3682205fdba5SJames Ogletree 	case CS_DSP_WSEQ_H16:
3683205fdba5SJames Ogletree 		cs_dsp_chunk_write(&chunk, 24, op_new->address);
3684205fdba5SJames Ogletree 		cs_dsp_chunk_write(&chunk, 16, op_new->data);
3685205fdba5SJames Ogletree 		break;
3686205fdba5SJames Ogletree 	default:
3687205fdba5SJames Ogletree 		ret = -EINVAL;
3688205fdba5SJames Ogletree 		cs_dsp_err(dsp, "Operation %X not supported\n", op_code);
3689205fdba5SJames Ogletree 		goto op_new_free;
3690205fdba5SJames Ogletree 	}
3691205fdba5SJames Ogletree 
3692205fdba5SJames Ogletree 	new_op_size = cs_dsp_chunk_bytes(&chunk);
3693205fdba5SJames Ogletree 
3694205fdba5SJames Ogletree 	if (!update) {
3695205fdba5SJames Ogletree 		if (wseq->ctl->len - op_end->offset < new_op_size) {
3696205fdba5SJames Ogletree 			cs_dsp_err(dsp, "Not enough memory in %s for entry\n", wseq->ctl->subname);
3697205fdba5SJames Ogletree 			ret = -E2BIG;
3698205fdba5SJames Ogletree 			goto op_new_free;
3699205fdba5SJames Ogletree 		}
3700205fdba5SJames Ogletree 
3701205fdba5SJames Ogletree 		op_end->offset += new_op_size;
3702205fdba5SJames Ogletree 
3703205fdba5SJames Ogletree 		ret = cs_dsp_coeff_write_ctrl(wseq->ctl, op_end->offset / sizeof(u32),
3704205fdba5SJames Ogletree 					      &op_end->data, sizeof(u32));
3705205fdba5SJames Ogletree 		if (ret)
3706205fdba5SJames Ogletree 			goto op_new_free;
3707205fdba5SJames Ogletree 
3708205fdba5SJames Ogletree 		list_add_tail(&op_new->list, &op_end->list);
3709205fdba5SJames Ogletree 	}
3710205fdba5SJames Ogletree 
3711205fdba5SJames Ogletree 	ret = cs_dsp_coeff_write_ctrl(wseq->ctl, op_new->offset / sizeof(u32),
3712205fdba5SJames Ogletree 				      words, new_op_size);
3713205fdba5SJames Ogletree 	if (ret)
3714205fdba5SJames Ogletree 		goto op_new_free;
3715205fdba5SJames Ogletree 
3716205fdba5SJames Ogletree 	return 0;
3717205fdba5SJames Ogletree 
3718205fdba5SJames Ogletree op_new_free:
3719205fdba5SJames Ogletree 	devm_kfree(dsp->dev, op_new);
3720205fdba5SJames Ogletree 
3721205fdba5SJames Ogletree 	return ret;
3722205fdba5SJames Ogletree }
3723205fdba5SJames Ogletree EXPORT_SYMBOL_NS_GPL(cs_dsp_wseq_write, FW_CS_DSP);
3724205fdba5SJames Ogletree 
3725205fdba5SJames Ogletree /**
3726205fdba5SJames Ogletree  * cs_dsp_wseq_multi_write() - Add or update multiple entries in a write sequence
3727205fdba5SJames Ogletree  * @dsp: Pointer to a DSP structure
3728205fdba5SJames Ogletree  * @wseq: Write sequence to write to
3729205fdba5SJames Ogletree  * @reg_seq: List of address-data pairs
3730205fdba5SJames Ogletree  * @num_regs: Number of address-data pairs
3731205fdba5SJames Ogletree  * @op_code: The types of operations of the new entries
3732205fdba5SJames Ogletree  * @update: If true, searches for the first entry in the write sequence with
3733205fdba5SJames Ogletree  * the same address and op_code, and replaces it. If false, creates a new entry
3734205fdba5SJames Ogletree  * at the tail
3735205fdba5SJames Ogletree  *
3736205fdba5SJames Ogletree  * This function calls cs_dsp_wseq_write() for multiple address-data pairs.
3737205fdba5SJames Ogletree  *
3738205fdba5SJames Ogletree  * Return: Zero for success, a negative number on error.
3739205fdba5SJames Ogletree  */
cs_dsp_wseq_multi_write(struct cs_dsp * dsp,struct cs_dsp_wseq * wseq,const struct reg_sequence * reg_seq,int num_regs,u8 op_code,bool update)3740205fdba5SJames Ogletree int cs_dsp_wseq_multi_write(struct cs_dsp *dsp, struct cs_dsp_wseq *wseq,
3741205fdba5SJames Ogletree 			    const struct reg_sequence *reg_seq, int num_regs,
3742205fdba5SJames Ogletree 			    u8 op_code, bool update)
3743205fdba5SJames Ogletree {
3744205fdba5SJames Ogletree 	int i, ret;
3745205fdba5SJames Ogletree 
3746205fdba5SJames Ogletree 	for (i = 0; i < num_regs; i++) {
3747205fdba5SJames Ogletree 		ret = cs_dsp_wseq_write(dsp, wseq, reg_seq[i].reg,
3748205fdba5SJames Ogletree 					reg_seq[i].def, op_code, update);
3749205fdba5SJames Ogletree 		if (ret)
3750205fdba5SJames Ogletree 			return ret;
3751205fdba5SJames Ogletree 	}
3752205fdba5SJames Ogletree 
3753205fdba5SJames Ogletree 	return 0;
3754205fdba5SJames Ogletree }
3755205fdba5SJames Ogletree EXPORT_SYMBOL_NS_GPL(cs_dsp_wseq_multi_write, FW_CS_DSP);
3756205fdba5SJames Ogletree 
3757f6bc909eSSimon Trimmer MODULE_DESCRIPTION("Cirrus Logic DSP Support");
3758f6bc909eSSimon Trimmer MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
3759f6bc909eSSimon Trimmer MODULE_LICENSE("GPL v2");
3760