dm table: share target argument parsing functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / intel_sst / intel_sst_dsp.c
bloba89e1ade847403b8ceec2353c27b9aba8cc49dd9
1 /*
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
27 * and middleware.
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>
36 #include <linux/fs.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"
44 /**
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);
55 csr.full |= 0x382;
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);
63 return 0;
66 /**
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);
77 csr.full |= 0x382;
78 sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full);
80 return 0;
83 /**
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);
93 csr.part.bypass = 0;
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);
101 return 0;
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);
114 csr.part.bypass = 0;
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");
126 return 0;
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;
140 u32 count;
141 void __iomem *ram;
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");
153 return -EINVAL;
155 switch (block->type) {
156 case SST_IRAM:
157 ram = sst_drv_ctx->iram;
158 break;
159 case SST_DRAM:
160 ram = sst_drv_ctx->dram;
161 break;
162 default:
163 pr_err("wrong ram type0x%x in block0x%x\n",
164 block->type, count);
165 return -EINVAL;
167 memcpy_toio(ram + block->ram_offset,
168 (void *)block + sizeof(*block), block->size);
169 block = (void *)block + sizeof(*block) + block->size;
171 return 0;
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;
184 u32 count;
185 int ret_val;
186 struct fw_module_header *module;
188 BUG_ON(!sst_fw);
190 /* Read the header information from the data pointer */
191 header = (struct fw_header *)sst_fw->data;
193 /* verify FW */
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");
198 return -EINVAL;
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++) {
205 /* module */
206 ret_val = sst_parse_module(module);
207 if (ret_val)
208 return ret_val;
209 module = (void *)module + sizeof(*module) + module->mod_size ;
212 return 0;
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)
225 int ret_val;
227 pr_debug("load_fw called\n");
228 BUG_ON(!fw);
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();
234 if (ret_val)
235 return ret_val;
237 ret_val = sst_parse_fw_image(fw);
238 if (ret_val)
239 return ret_val;
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();
249 if (ret_val)
250 return ret_val;
252 pr_debug("fw loaded successful!!!\n");
253 return ret_val;
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 */
262 int i;
263 u8 pvt_id;
264 struct ipc_post *msg = NULL;
265 union config_status_reg csr;
266 struct snd_sst_str_type str_type = {0};
267 int retval = 0;
269 if (sst_create_large_msg(&msg))
270 return -ENOMEM;
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);
275 if (i < 0) {
276 kfree(msg);
277 return -ENOMEM;
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]);
291 if (retval) {
292 /* error */
293 sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT;
294 pr_err("Prep codec downloaded failed %d\n",
295 retval);
296 return -EIO;
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;
325 return -ENOMEM;
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]);
341 if (retval) {
342 /* error */
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;
347 return -EIO;
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);
355 return 0;
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,
363 u32 *entry_point)
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;
369 int err = 0;
371 header = (struct fw_header *)fw_lib->data;
372 if (header->modules != 1) {
373 pr_err("Module no mismatch found\n");
374 err = -EINVAL;
375 goto exit;
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) {
387 case SST_IRAM:
388 isize += block->size;
389 break;
390 case SST_DRAM:
391 dsize += block->size;
392 break;
393 default:
394 pr_err("Invalid block type for 0x%x\n", n_blk);
395 err = -EINVAL;
396 goto exit;
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");
402 err = -EINVAL;
403 goto exit;
404 } else
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);
409 exit:
410 return err;
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)
418 char buf[20];
419 const char *type, *dir;
420 int len = 0, error = 0;
421 u32 entry_point;
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:
440 type = "mp3_";
441 break;
442 case SST_CODEC_TYPE_AAC:
443 type = "aac_";
444 break;
445 case SST_CODEC_TYPE_AACP:
446 type = "aac_v1_";
447 break;
448 case SST_CODEC_TYPE_eAACP:
449 type = "aac_v2_";
450 break;
451 case SST_CODEC_TYPE_WMA9:
452 type = "wma9_";
453 break;
454 default:
455 pr_err("Invalid codec type\n");
456 error = -EINVAL;
457 goto wake;
460 if (ops == STREAM_OPS_CAPTURE)
461 dir = "enc_";
462 else
463 dir = "dec_";
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);
474 if (error) {
475 pr_err("library load failed %d\n", error);
476 goto wake;
478 error = sst_validate_library(fw_lib, &lib->slot_info, &entry_point);
479 if (error)
480 goto wake_free;
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);
485 if (error)
486 goto wake_free;
488 /* lib is downloaded and init send alloc again */
489 pr_debug("Library is downloaded now...\n");
490 wake_free:
491 /* sst_wake_up_alloc_block(sst_drv_ctx, pvt_id, error, NULL); */
492 release_firmware(fw_lib);
493 wake:
494 return error;