2 * intel_sst_dsp.c - Intel SST Driver for audio engine
4 * Copyright (C) 2008-10 Intel Corp
5 * Authors: Vinod Koul <vinod.koul@intel.com>
6 * Harsha Priya <priya.harsha@intel.com>
7 * Dharageswari R <dharageswari.r@intel.com>
8 * KP Jeeja <jeeja.kp@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 * This driver exposes the audio engine functionalities to the ALSA
29 * This file contains all dsp controlling functions like firmware download,
30 * setting/resetting dsp cores, etc
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/pci.h>
37 #include <linux/firmware.h>
38 #include "intel_sst.h"
39 #include "intel_sst_ioctl.h"
40 #include "intel_sst_fw_ipc.h"
41 #include "intel_sst_common.h"
45 * intel_sst_reset_dsp_mrst - Resetting SST DSP
47 * This resets DSP in case of MRST platfroms
49 static int intel_sst_reset_dsp_mrst(void)
51 union config_status_reg csr
;
53 pr_debug("Resetting the DSP in mrst\n");
54 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
56 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
57 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
58 csr
.part
.strb_cntr_rst
= 0;
59 csr
.part
.run_stall
= 0x1;
60 csr
.part
.bypass
= 0x7;
61 csr
.part
.sst_reset
= 0x1;
62 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
67 * intel_sst_reset_dsp_medfield - Resetting SST DSP
69 * This resets DSP in case of Medfield platfroms
71 static int intel_sst_reset_dsp_medfield(void)
73 union config_status_reg csr
;
75 pr_debug("Resetting the DSP in medfield\n");
76 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
78 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
84 * sst_start_mrst - Start the SST DSP processor
86 * This starts the DSP in MRST platfroms
88 static int sst_start_mrst(void)
90 union config_status_reg csr
;
92 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
94 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
95 csr
.part
.run_stall
= 0;
96 csr
.part
.sst_reset
= 0;
97 csr
.part
.strb_cntr_rst
= 1;
98 pr_debug("Setting SST to execute_mrst 0x%x\n", csr
.full
);
99 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
105 * sst_start_medfield - Start the SST DSP processor
107 * This starts the DSP in MRST platfroms
109 static int sst_start_medfield(void)
111 union config_status_reg csr
;
113 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
115 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
116 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
117 csr
.part
.mfld_strb
= 1;
118 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
119 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
120 csr
.part
.run_stall
= 0;
121 csr
.part
.sst_reset
= 0;
122 pr_debug("Starting the DSP_medfld %x\n", csr
.full
);
123 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
124 pr_debug("Starting the DSP_medfld\n");
130 * sst_parse_module - Parse audio FW modules
132 * @module: FW module header
134 * Parses modules that need to be placed in SST IRAM and DRAM
135 * returns error or 0 if module sizes are proper
137 static int sst_parse_module(struct fw_module_header
*module
)
139 struct dma_block_info
*block
;
143 pr_debug("module sign %s size %x blocks %x type %x\n",
144 module
->signature
, module
->mod_size
,
145 module
->blocks
, module
->type
);
146 pr_debug("module entrypoint 0x%x\n", module
->entry_point
);
148 block
= (void *)module
+ sizeof(*module
);
150 for (count
= 0; count
< module
->blocks
; count
++) {
151 if (block
->size
<= 0) {
152 pr_err("block size invalid\n");
155 switch (block
->type
) {
157 ram
= sst_drv_ctx
->iram
;
160 ram
= sst_drv_ctx
->dram
;
163 pr_err("wrong ram type0x%x in block0x%x\n",
167 memcpy_toio(ram
+ block
->ram_offset
,
168 (void *)block
+ sizeof(*block
), block
->size
);
169 block
= (void *)block
+ sizeof(*block
) + block
->size
;
175 * sst_parse_fw_image - parse and load FW
177 * @sst_fw: pointer to audio fw
179 * This function is called to parse and download the FW image
181 static int sst_parse_fw_image(const struct firmware
*sst_fw
)
183 struct fw_header
*header
;
186 struct fw_module_header
*module
;
190 /* Read the header information from the data pointer */
191 header
= (struct fw_header
*)sst_fw
->data
;
194 if ((strncmp(header
->signature
, SST_FW_SIGN
, 4) != 0) ||
195 (sst_fw
->size
!= header
->file_size
+ sizeof(*header
))) {
196 /* Invalid FW signature */
197 pr_err("Invalid FW sign/filesize mismatch\n");
200 pr_debug("header sign=%s size=%x modules=%x fmt=%x size=%x\n",
201 header
->signature
, header
->file_size
, header
->modules
,
202 header
->file_format
, sizeof(*header
));
203 module
= (void *)sst_fw
->data
+ sizeof(*header
);
204 for (count
= 0; count
< header
->modules
; count
++) {
206 ret_val
= sst_parse_module(module
);
209 module
= (void *)module
+ sizeof(*module
) + module
->mod_size
;
216 * sst_load_fw - function to load FW into DSP
218 * @fw: Pointer to driver loaded FW
219 * @context: driver context
221 * This function is called by OS when the FW is loaded into kernel
223 int sst_load_fw(const struct firmware
*fw
, void *context
)
227 pr_debug("load_fw called\n");
230 if (sst_drv_ctx
->pci_id
== SST_MRST_PCI_ID
)
231 ret_val
= intel_sst_reset_dsp_mrst();
232 else if (sst_drv_ctx
->pci_id
== SST_MFLD_PCI_ID
)
233 ret_val
= intel_sst_reset_dsp_medfield();
237 ret_val
= sst_parse_fw_image(fw
);
240 mutex_lock(&sst_drv_ctx
->sst_lock
);
241 sst_drv_ctx
->sst_state
= SST_FW_LOADED
;
242 mutex_unlock(&sst_drv_ctx
->sst_lock
);
243 /* 7. ask scu to reset the bypass bits */
244 /* 8.bring sst out of reset */
245 if (sst_drv_ctx
->pci_id
== SST_MRST_PCI_ID
)
246 ret_val
= sst_start_mrst();
247 else if (sst_drv_ctx
->pci_id
== SST_MFLD_PCI_ID
)
248 ret_val
= sst_start_medfield();
252 pr_debug("fw loaded successful!!!\n");
256 /*This function is called when any codec/post processing library
257 needs to be downloaded*/
258 static int sst_download_library(const struct firmware
*fw_lib
,
259 struct snd_sst_lib_download_info
*lib
)
261 /* send IPC message and wait */
264 struct ipc_post
*msg
= NULL
;
265 union config_status_reg csr
;
266 struct snd_sst_str_type str_type
= {0};
269 if (sst_create_large_msg(&msg
))
272 pvt_id
= sst_assign_pvt_id(sst_drv_ctx
);
273 i
= sst_get_block_stream(sst_drv_ctx
);
274 pr_debug("alloc block allocated = %d, pvt_id %d\n", i
, pvt_id
);
279 sst_drv_ctx
->alloc_block
[i
].sst_id
= pvt_id
;
280 sst_fill_header(&msg
->header
, IPC_IA_PREP_LIB_DNLD
, 1, pvt_id
);
281 msg
->header
.part
.data
= sizeof(u32
) + sizeof(str_type
);
282 str_type
.codec_type
= lib
->dload_lib
.lib_info
.lib_type
;
283 /*str_type.pvt_id = pvt_id;*/
284 memcpy(msg
->mailbox_data
, &msg
->header
, sizeof(u32
));
285 memcpy(msg
->mailbox_data
+ sizeof(u32
), &str_type
, sizeof(str_type
));
286 spin_lock(&sst_drv_ctx
->list_spin_lock
);
287 list_add_tail(&msg
->node
, &sst_drv_ctx
->ipc_dispatch_list
);
288 spin_unlock(&sst_drv_ctx
->list_spin_lock
);
289 sst_post_message(&sst_drv_ctx
->ipc_post_msg_wq
);
290 retval
= sst_wait_timeout(sst_drv_ctx
, &sst_drv_ctx
->alloc_block
[i
]);
293 sst_drv_ctx
->alloc_block
[i
].sst_id
= BLOCK_UNINIT
;
294 pr_err("Prep codec downloaded failed %d\n",
298 pr_debug("FW responded, ready for download now...\n");
299 /* downloading on success */
300 mutex_lock(&sst_drv_ctx
->sst_lock
);
301 sst_drv_ctx
->sst_state
= SST_FW_LOADED
;
302 mutex_unlock(&sst_drv_ctx
->sst_lock
);
303 csr
.full
= readl(sst_drv_ctx
->shim
+ SST_CSR
);
304 csr
.part
.run_stall
= 1;
305 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
307 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
308 csr
.part
.bypass
= 0x7;
309 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
311 sst_parse_fw_image(fw_lib
);
313 /* set the FW to running again */
314 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
315 csr
.part
.bypass
= 0x0;
316 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
318 csr
.full
= sst_shim_read(sst_drv_ctx
->shim
, SST_CSR
);
319 csr
.part
.run_stall
= 0;
320 sst_shim_write(sst_drv_ctx
->shim
, SST_CSR
, csr
.full
);
322 /* send download complete and wait */
323 if (sst_create_large_msg(&msg
)) {
324 sst_drv_ctx
->alloc_block
[i
].sst_id
= BLOCK_UNINIT
;
328 sst_fill_header(&msg
->header
, IPC_IA_LIB_DNLD_CMPLT
, 1, pvt_id
);
329 sst_drv_ctx
->alloc_block
[i
].sst_id
= pvt_id
;
330 msg
->header
.part
.data
= sizeof(u32
) + sizeof(*lib
);
331 lib
->pvt_id
= pvt_id
;
332 memcpy(msg
->mailbox_data
, &msg
->header
, sizeof(u32
));
333 memcpy(msg
->mailbox_data
+ sizeof(u32
), lib
, sizeof(*lib
));
334 spin_lock(&sst_drv_ctx
->list_spin_lock
);
335 list_add_tail(&msg
->node
, &sst_drv_ctx
->ipc_dispatch_list
);
336 spin_unlock(&sst_drv_ctx
->list_spin_lock
);
337 sst_post_message(&sst_drv_ctx
->ipc_post_msg_wq
);
338 pr_debug("Waiting for FW response Download complete\n");
339 sst_drv_ctx
->alloc_block
[i
].ops_block
.condition
= false;
340 retval
= sst_wait_timeout(sst_drv_ctx
, &sst_drv_ctx
->alloc_block
[i
]);
343 mutex_lock(&sst_drv_ctx
->sst_lock
);
344 sst_drv_ctx
->sst_state
= SST_UN_INIT
;
345 mutex_unlock(&sst_drv_ctx
->sst_lock
);
346 sst_drv_ctx
->alloc_block
[i
].sst_id
= BLOCK_UNINIT
;
350 pr_debug("FW success on Download complete\n");
351 sst_drv_ctx
->alloc_block
[i
].sst_id
= BLOCK_UNINIT
;
352 mutex_lock(&sst_drv_ctx
->sst_lock
);
353 sst_drv_ctx
->sst_state
= SST_FW_RUNNING
;
354 mutex_unlock(&sst_drv_ctx
->sst_lock
);
359 /* This function is called before downloading the codec/postprocessing
360 library is set for download to SST DSP*/
361 static int sst_validate_library(const struct firmware
*fw_lib
,
362 struct lib_slot_info
*slot
,
365 struct fw_header
*header
;
366 struct fw_module_header
*module
;
367 struct dma_block_info
*block
;
368 unsigned int n_blk
, isize
= 0, dsize
= 0;
371 header
= (struct fw_header
*)fw_lib
->data
;
372 if (header
->modules
!= 1) {
373 pr_err("Module no mismatch found\n");
377 module
= (void *)fw_lib
->data
+ sizeof(*header
);
378 *entry_point
= module
->entry_point
;
379 pr_debug("Module entry point 0x%x\n", *entry_point
);
380 pr_debug("Module Sign %s, Size 0x%x, Blocks 0x%x Type 0x%x\n",
381 module
->signature
, module
->mod_size
,
382 module
->blocks
, module
->type
);
384 block
= (void *)module
+ sizeof(*module
);
385 for (n_blk
= 0; n_blk
< module
->blocks
; n_blk
++) {
386 switch (block
->type
) {
388 isize
+= block
->size
;
391 dsize
+= block
->size
;
394 pr_err("Invalid block type for 0x%x\n", n_blk
);
398 block
= (void *)block
+ sizeof(*block
) + block
->size
;
400 if (isize
> slot
->iram_size
|| dsize
> slot
->dram_size
) {
401 pr_err("library exceeds size allocated\n");
405 pr_debug("Library is safe for download...\n");
407 pr_debug("iram 0x%x, dram 0x%x, iram 0x%x, dram 0x%x\n",
408 isize
, dsize
, slot
->iram_size
, slot
->dram_size
);
414 /* This function is called when FW requests for a particular library download
415 This function prepares the library to download*/
416 int sst_load_library(struct snd_sst_lib_download
*lib
, u8 ops
)
419 const char *type
, *dir
;
420 int len
= 0, error
= 0;
422 const struct firmware
*fw_lib
;
423 struct snd_sst_lib_download_info dload_info
= {{{0},},};
425 memset(buf
, 0, sizeof(buf
));
427 pr_debug("Lib Type 0x%x, Slot 0x%x, ops 0x%x\n",
428 lib
->lib_info
.lib_type
, lib
->slot_info
.slot_num
, ops
);
429 pr_debug("Version 0x%x, name %s, caps 0x%x media type 0x%x\n",
430 lib
->lib_info
.lib_version
, lib
->lib_info
.lib_name
,
431 lib
->lib_info
.lib_caps
, lib
->lib_info
.media_type
);
433 pr_debug("IRAM Size 0x%x, offset 0x%x\n",
434 lib
->slot_info
.iram_size
, lib
->slot_info
.iram_offset
);
435 pr_debug("DRAM Size 0x%x, offset 0x%x\n",
436 lib
->slot_info
.dram_size
, lib
->slot_info
.dram_offset
);
438 switch (lib
->lib_info
.lib_type
) {
439 case SST_CODEC_TYPE_MP3
:
442 case SST_CODEC_TYPE_AAC
:
445 case SST_CODEC_TYPE_AACP
:
448 case SST_CODEC_TYPE_eAACP
:
451 case SST_CODEC_TYPE_WMA9
:
455 pr_err("Invalid codec type\n");
460 if (ops
== STREAM_OPS_CAPTURE
)
464 len
= strlen(type
) + strlen(dir
);
465 strncpy(buf
, type
, sizeof(buf
)-1);
466 strncpy(buf
+ strlen(type
), dir
, sizeof(buf
)-strlen(type
)-1);
467 len
+= snprintf(buf
+ len
, sizeof(buf
) - len
, "%d",
468 lib
->slot_info
.slot_num
);
469 len
+= snprintf(buf
+ len
, sizeof(buf
) - len
, ".bin");
471 pr_debug("Requesting %s\n", buf
);
473 error
= request_firmware(&fw_lib
, buf
, &sst_drv_ctx
->pci
->dev
);
475 pr_err("library load failed %d\n", error
);
478 error
= sst_validate_library(fw_lib
, &lib
->slot_info
, &entry_point
);
482 lib
->mod_entry_pt
= entry_point
;
483 memcpy(&dload_info
.dload_lib
, lib
, sizeof(*lib
));
484 error
= sst_download_library(fw_lib
, &dload_info
);
488 /* lib is downloaded and init send alloc again */
489 pr_debug("Library is downloaded now...\n");
491 /* sst_wake_up_alloc_block(sst_drv_ctx, pvt_id, error, NULL); */
492 release_firmware(fw_lib
);