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 = 0x048303E2;
77 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
83 * sst_start_mrst - Start the SST DSP processor
85 * This starts the DSP in MRST platfroms
87 static int sst_start_mrst(void)
89 union config_status_reg csr;
91 csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
93 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
94 csr.part.run_stall = 0;
95 csr.part.sst_reset = 0;
96 csr.part.strb_cntr_rst = 1;
97 pr_debug("Setting SST to execute_mrst 0x%x\n", csr.full);
98 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
104 * sst_start_medfield - Start the SST DSP processor
106 * This starts the DSP in MRST platfroms
108 static int sst_start_medfield(void)
110 union config_status_reg csr;
112 csr.full = 0x04830062;
113 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
114 csr.full = 0x04830063;
115 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
116 csr.full = 0x04830061;
117 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
118 pr_debug("Starting the DSP_medfld\n");
124 * sst_parse_module - Parse audio FW modules
126 * @module: FW module header
128 * Parses modules that need to be placed in SST IRAM and DRAM
129 * returns error or 0 if module sizes are proper
131 static int sst_parse_module(struct fw_module_header *module)
133 struct dma_block_info *block;
137 pr_debug("module sign %s size %x blocks %x type %x\n",
138 module->signature, module->mod_size,
139 module->blocks, module->type);
140 pr_debug("module entrypoint 0x%x\n", module->entry_point);
142 block = (void *)module + sizeof(*module);
144 for (count = 0; count < module->blocks; count++) {
145 if (block->size <= 0) {
146 pr_err("block size invalid\n");
149 switch (block->type) {
151 ram = sst_drv_ctx->iram;
154 ram = sst_drv_ctx->dram;
157 pr_err("wrong ram type0x%x in block0x%x\n",
161 memcpy_toio(ram + block->ram_offset,
162 (void *)block + sizeof(*block), block->size);
163 block = (void *)block + sizeof(*block) + block->size;
169 * sst_parse_fw_image - parse and load FW
171 * @sst_fw: pointer to audio fw
173 * This function is called to parse and download the FW image
175 static int sst_parse_fw_image(const struct firmware *sst_fw)
177 struct fw_header *header;
180 struct fw_module_header *module;
184 /* Read the header information from the data pointer */
185 header = (struct fw_header *)sst_fw->data;
188 if ((strncmp(header->signature, SST_FW_SIGN, 4) != 0) ||
189 (sst_fw->size != header->file_size + sizeof(*header))) {
190 /* Invalid FW signature */
191 pr_err("Invalid FW sign/filesize mismatch\n");
194 pr_debug("header sign=%s size=%x modules=%x fmt=%x size=%x\n",
195 header->signature, header->file_size, header->modules,
196 header->file_format, sizeof(*header));
197 module = (void *)sst_fw->data + sizeof(*header);
198 for (count = 0; count < header->modules; count++) {
200 ret_val = sst_parse_module(module);
203 module = (void *)module + sizeof(*module) + module->mod_size ;
210 * sst_load_fw - function to load FW into DSP
212 * @fw: Pointer to driver loaded FW
213 * @context: driver context
215 * This function is called by OS when the FW is loaded into kernel
217 int sst_load_fw(const struct firmware *fw, void *context)
221 pr_debug("load_fw called\n");
224 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
225 ret_val = intel_sst_reset_dsp_mrst();
226 else if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID)
227 ret_val = intel_sst_reset_dsp_medfield();
231 ret_val = sst_parse_fw_image(fw);
234 mutex_lock(&sst_drv_ctx->sst_lock);
235 sst_drv_ctx->sst_state = SST_FW_LOADED;
236 mutex_unlock(&sst_drv_ctx->sst_lock);
237 /* 7. ask scu to reset the bypass bits */
238 /* 8.bring sst out of reset */
239 if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID)
240 ret_val = sst_start_mrst();
241 else if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID)
242 ret_val = sst_start_medfield();
246 pr_debug("fw loaded successful!!!\n");
250 /*This function is called when any codec/post processing library
251 needs to be downloaded*/
252 static int sst_download_library(const struct firmware *fw_lib,
253 struct snd_sst_lib_download_info *lib)
255 /* send IPC message and wait */
258 struct ipc_post *msg = NULL;
259 union config_status_reg csr;
260 struct snd_sst_str_type str_type = {0};
263 if (sst_create_large_msg(&msg))
266 pvt_id = sst_assign_pvt_id(sst_drv_ctx);
267 i = sst_get_block_stream(sst_drv_ctx);
268 pr_debug("alloc block allocated = %d, pvt_id %d\n", i, pvt_id);
273 sst_drv_ctx->alloc_block[i].sst_id = pvt_id;
274 sst_fill_header(&msg->header, IPC_IA_PREP_LIB_DNLD, 1, pvt_id);
275 msg->header.part.data = sizeof(u32) + sizeof(str_type);
276 str_type.codec_type = lib->dload_lib.lib_info.lib_type;
277 /*str_type.pvt_id = pvt_id;*/
278 memcpy(msg->mailbox_data, &msg->header, sizeof(u32));
279 memcpy(msg->mailbox_data + sizeof(u32), &str_type, sizeof(str_type));
280 spin_lock(&sst_drv_ctx->list_spin_lock);
281 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
282 spin_unlock(&sst_drv_ctx->list_spin_lock);
283 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
284 retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[i]);
287 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
288 pr_err("Prep codec downloaded failed %d\n",
292 pr_debug("FW responded, ready for download now...\n");
293 /* downloading on success */
294 mutex_lock(&sst_drv_ctx->sst_lock);
295 sst_drv_ctx->sst_state = SST_FW_LOADED;
296 mutex_unlock(&sst_drv_ctx->sst_lock);
297 csr.full = readl(sst_drv_ctx->shim + SST_CSR);
298 csr.part.run_stall = 1;
299 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
301 csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
302 csr.part.bypass = 0x7;
303 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
305 sst_parse_fw_image(fw_lib);
307 /* set the FW to running again */
308 csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
309 csr.part.bypass = 0x0;
310 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
312 csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR);
313 csr.part.run_stall = 0;
314 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
316 /* send download complete and wait */
317 if (sst_create_large_msg(&msg)) {
318 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
322 sst_fill_header(&msg->header, IPC_IA_LIB_DNLD_CMPLT, 1, pvt_id);
323 sst_drv_ctx->alloc_block[i].sst_id = pvt_id;
324 msg->header.part.data = sizeof(u32) + sizeof(*lib);
325 lib->pvt_id = pvt_id;
326 memcpy(msg->mailbox_data, &msg->header, sizeof(u32));
327 memcpy(msg->mailbox_data + sizeof(u32), lib, sizeof(*lib));
328 spin_lock(&sst_drv_ctx->list_spin_lock);
329 list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list);
330 spin_unlock(&sst_drv_ctx->list_spin_lock);
331 sst_post_message(&sst_drv_ctx->ipc_post_msg_wq);
332 pr_debug("Waiting for FW response Download complete\n");
333 sst_drv_ctx->alloc_block[i].ops_block.condition = false;
334 retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[i]);
337 mutex_lock(&sst_drv_ctx->sst_lock);
338 sst_drv_ctx->sst_state = SST_UN_INIT;
339 mutex_unlock(&sst_drv_ctx->sst_lock);
340 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
344 pr_debug("FW success on Download complete\n");
345 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
346 mutex_lock(&sst_drv_ctx->sst_lock);
347 sst_drv_ctx->sst_state = SST_FW_RUNNING;
348 mutex_unlock(&sst_drv_ctx->sst_lock);
353 /* This function is called before downloading the codec/postprocessing
354 library is set for download to SST DSP*/
355 static int sst_validate_library(const struct firmware *fw_lib,
356 struct lib_slot_info *slot,
359 struct fw_header *header;
360 struct fw_module_header *module;
361 struct dma_block_info *block;
362 unsigned int n_blk, isize = 0, dsize = 0;
365 header = (struct fw_header *)fw_lib->data;
366 if (header->modules != 1) {
367 pr_err("Module no mismatch found\n");
371 module = (void *)fw_lib->data + sizeof(*header);
372 *entry_point = module->entry_point;
373 pr_debug("Module entry point 0x%x\n", *entry_point);
374 pr_debug("Module Sign %s, Size 0x%x, Blocks 0x%x Type 0x%x\n",
375 module->signature, module->mod_size,
376 module->blocks, module->type);
378 block = (void *)module + sizeof(*module);
379 for (n_blk = 0; n_blk < module->blocks; n_blk++) {
380 switch (block->type) {
382 isize += block->size;
385 dsize += block->size;
388 pr_err("Invalid block type for 0x%x\n", n_blk);
392 block = (void *)block + sizeof(*block) + block->size;
394 if (isize > slot->iram_size || dsize > slot->dram_size) {
395 pr_err("library exceeds size allocated\n");
399 pr_debug("Library is safe for download...\n");
401 pr_debug("iram 0x%x, dram 0x%x, iram 0x%x, dram 0x%x\n",
402 isize, dsize, slot->iram_size, slot->dram_size);
408 /* This function is called when FW requests for a particular library download
409 This function prepares the library to download*/
410 int sst_load_library(struct snd_sst_lib_download *lib, u8 ops)
413 const char *type, *dir;
414 int len = 0, error = 0;
416 const struct firmware *fw_lib;
417 struct snd_sst_lib_download_info dload_info = {{{0},},};
419 memset(buf, 0, sizeof(buf));
421 pr_debug("Lib Type 0x%x, Slot 0x%x, ops 0x%x\n",
422 lib->lib_info.lib_type, lib->slot_info.slot_num, ops);
423 pr_debug("Version 0x%x, name %s, caps 0x%x media type 0x%x\n",
424 lib->lib_info.lib_version, lib->lib_info.lib_name,
425 lib->lib_info.lib_caps, lib->lib_info.media_type);
427 pr_debug("IRAM Size 0x%x, offset 0x%x\n",
428 lib->slot_info.iram_size, lib->slot_info.iram_offset);
429 pr_debug("DRAM Size 0x%x, offset 0x%x\n",
430 lib->slot_info.dram_size, lib->slot_info.dram_offset);
432 switch (lib->lib_info.lib_type) {
433 case SST_CODEC_TYPE_MP3:
436 case SST_CODEC_TYPE_AAC:
439 case SST_CODEC_TYPE_AACP:
442 case SST_CODEC_TYPE_eAACP:
445 case SST_CODEC_TYPE_WMA9:
449 pr_err("Invalid codec type\n");
454 if (ops == STREAM_OPS_CAPTURE)
458 len = strlen(type) + strlen(dir);
459 strncpy(buf, type, sizeof(buf)-1);
460 strncpy(buf + strlen(type), dir, sizeof(buf)-strlen(type)-1);
461 len += snprintf(buf + len, sizeof(buf) - len, "%d",
462 lib->slot_info.slot_num);
463 len += snprintf(buf + len, sizeof(buf) - len, ".bin");
465 pr_debug("Requesting %s\n", buf);
467 error = request_firmware(&fw_lib, buf, &sst_drv_ctx->pci->dev);
469 pr_err("library load failed %d\n", error);
472 error = sst_validate_library(fw_lib, &lib->slot_info, &entry_point);
476 lib->mod_entry_pt = entry_point;
477 memcpy(&dload_info.dload_lib, lib, sizeof(*lib));
478 error = sst_download_library(fw_lib, &dload_info);
482 /* lib is downloaded and init send alloc again */
483 pr_debug("Library is downloaded now...\n");
485 /* sst_wake_up_alloc_block(sst_drv_ctx, pvt_id, error, NULL); */
486 release_firmware(fw_lib);