Commit | Line | Data |
---|---|---|
fffa1cca VK |
1 | /* |
2 | * intel_sst_dsp.c - Intel SST Driver for audio engine | |
3 | * | |
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 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
10 | * | |
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. | |
14 | * | |
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. | |
19 | * | |
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. | |
23 | * | |
24 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
25 | * | |
26 | * This driver exposes the audio engine functionalities to the ALSA | |
27 | * and middleware. | |
28 | * | |
29 | * This file contains all dsp controlling functions like firmware download, | |
30 | * setting/resetting dsp cores, etc | |
31 | */ | |
d0f40c50 JP |
32 | |
33 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
34 | ||
fffa1cca VK |
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" | |
42 | ||
43 | ||
44 | /** | |
45 | * intel_sst_reset_dsp_mrst - Resetting SST DSP | |
46 | * | |
47 | * This resets DSP in case of MRST platfroms | |
48 | */ | |
49 | static int intel_sst_reset_dsp_mrst(void) | |
50 | { | |
51 | union config_status_reg csr; | |
52 | ||
d0f40c50 | 53 | pr_debug("Resetting the DSP in mrst\n"); |
fffa1cca VK |
54 | csr.full = 0x3a2; |
55 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
56 | csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR); | |
57 | csr.part.strb_cntr_rst = 0; | |
58 | csr.part.run_stall = 0x1; | |
59 | csr.part.bypass = 0x7; | |
60 | csr.part.sst_reset = 0x1; | |
61 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
62 | return 0; | |
63 | } | |
64 | ||
65 | /** | |
66 | * intel_sst_reset_dsp_medfield - Resetting SST DSP | |
67 | * | |
68 | * This resets DSP in case of Medfield platfroms | |
69 | */ | |
70 | static int intel_sst_reset_dsp_medfield(void) | |
71 | { | |
72 | union config_status_reg csr; | |
73 | ||
d0f40c50 | 74 | pr_debug("Resetting the DSP in medfield\n"); |
fffa1cca VK |
75 | csr.full = 0x048303E2; |
76 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
77 | ||
78 | return 0; | |
79 | } | |
80 | ||
81 | /** | |
82 | * sst_start_mrst - Start the SST DSP processor | |
83 | * | |
84 | * This starts the DSP in MRST platfroms | |
85 | */ | |
86 | static int sst_start_mrst(void) | |
87 | { | |
88 | union config_status_reg csr; | |
89 | ||
90 | csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR); | |
91 | csr.part.bypass = 0; | |
92 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
93 | csr.part.run_stall = 0; | |
94 | csr.part.sst_reset = 0; | |
95 | csr.part.strb_cntr_rst = 1; | |
d0f40c50 | 96 | pr_debug("Setting SST to execute_mrst 0x%x\n", csr.full); |
fffa1cca VK |
97 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | /** | |
103 | * sst_start_medfield - Start the SST DSP processor | |
104 | * | |
105 | * This starts the DSP in MRST platfroms | |
106 | */ | |
107 | static int sst_start_medfield(void) | |
108 | { | |
109 | union config_status_reg csr; | |
110 | ||
111 | csr.full = 0x04830062; | |
112 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
113 | csr.full = 0x04830063; | |
114 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
115 | csr.full = 0x04830061; | |
116 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
d0f40c50 | 117 | pr_debug("Starting the DSP_medfld\n"); |
fffa1cca VK |
118 | |
119 | return 0; | |
120 | } | |
121 | ||
122 | /** | |
123 | * sst_parse_module - Parse audio FW modules | |
124 | * | |
125 | * @module: FW module header | |
126 | * | |
127 | * Parses modules that need to be placed in SST IRAM and DRAM | |
128 | * returns error or 0 if module sizes are proper | |
129 | */ | |
130 | static int sst_parse_module(struct fw_module_header *module) | |
131 | { | |
132 | struct dma_block_info *block; | |
133 | u32 count; | |
134 | void __iomem *ram; | |
135 | ||
d0f40c50 | 136 | pr_debug("module sign %s size %x blocks %x type %x\n", |
fffa1cca VK |
137 | module->signature, module->mod_size, |
138 | module->blocks, module->type); | |
d0f40c50 | 139 | pr_debug("module entrypoint 0x%x\n", module->entry_point); |
fffa1cca VK |
140 | |
141 | block = (void *)module + sizeof(*module); | |
142 | ||
143 | for (count = 0; count < module->blocks; count++) { | |
144 | if (block->size <= 0) { | |
d0f40c50 | 145 | pr_err("block size invalid\n"); |
fffa1cca VK |
146 | return -EINVAL; |
147 | } | |
148 | switch (block->type) { | |
149 | case SST_IRAM: | |
150 | ram = sst_drv_ctx->iram; | |
151 | break; | |
152 | case SST_DRAM: | |
153 | ram = sst_drv_ctx->dram; | |
154 | break; | |
155 | default: | |
d0f40c50 | 156 | pr_err("wrong ram type0x%x in block0x%x\n", |
fffa1cca VK |
157 | block->type, count); |
158 | return -EINVAL; | |
159 | } | |
160 | memcpy_toio(ram + block->ram_offset, | |
161 | (void *)block + sizeof(*block), block->size); | |
162 | block = (void *)block + sizeof(*block) + block->size; | |
163 | } | |
164 | return 0; | |
165 | } | |
166 | ||
167 | /** | |
168 | * sst_parse_fw_image - parse and load FW | |
169 | * | |
170 | * @sst_fw: pointer to audio fw | |
171 | * | |
172 | * This function is called to parse and download the FW image | |
173 | */ | |
174 | static int sst_parse_fw_image(const struct firmware *sst_fw) | |
175 | { | |
176 | struct fw_header *header; | |
177 | u32 count; | |
178 | int ret_val; | |
179 | struct fw_module_header *module; | |
180 | ||
181 | BUG_ON(!sst_fw); | |
182 | ||
183 | /* Read the header information from the data pointer */ | |
184 | header = (struct fw_header *)sst_fw->data; | |
185 | ||
186 | /* verify FW */ | |
187 | if ((strncmp(header->signature, SST_FW_SIGN, 4) != 0) || | |
188 | (sst_fw->size != header->file_size + sizeof(*header))) { | |
189 | /* Invalid FW signature */ | |
d0f40c50 | 190 | pr_err("Invalid FW sign/filesize mismatch\n"); |
fffa1cca VK |
191 | return -EINVAL; |
192 | } | |
d0f40c50 | 193 | pr_debug("header sign=%s size=%x modules=%x fmt=%x size=%x\n", |
fffa1cca VK |
194 | header->signature, header->file_size, header->modules, |
195 | header->file_format, sizeof(*header)); | |
196 | module = (void *)sst_fw->data + sizeof(*header); | |
197 | for (count = 0; count < header->modules; count++) { | |
198 | /* module */ | |
199 | ret_val = sst_parse_module(module); | |
200 | if (ret_val) | |
201 | return ret_val; | |
202 | module = (void *)module + sizeof(*module) + module->mod_size ; | |
203 | } | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
208 | /** | |
209 | * sst_load_fw - function to load FW into DSP | |
210 | * | |
211 | * @fw: Pointer to driver loaded FW | |
212 | * @context: driver context | |
213 | * | |
214 | * This function is called by OS when the FW is loaded into kernel | |
215 | */ | |
216 | int sst_load_fw(const struct firmware *fw, void *context) | |
217 | { | |
218 | int ret_val; | |
219 | ||
d0f40c50 | 220 | pr_debug("load_fw called\n"); |
fffa1cca VK |
221 | BUG_ON(!fw); |
222 | ||
223 | if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) | |
224 | ret_val = intel_sst_reset_dsp_mrst(); | |
225 | else if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID) | |
226 | ret_val = intel_sst_reset_dsp_medfield(); | |
227 | if (ret_val) | |
228 | return ret_val; | |
229 | ||
230 | ret_val = sst_parse_fw_image(fw); | |
231 | if (ret_val) | |
232 | return ret_val; | |
233 | mutex_lock(&sst_drv_ctx->sst_lock); | |
234 | sst_drv_ctx->sst_state = SST_FW_LOADED; | |
235 | mutex_unlock(&sst_drv_ctx->sst_lock); | |
236 | /* 7. ask scu to reset the bypass bits */ | |
237 | /* 8.bring sst out of reset */ | |
238 | if (sst_drv_ctx->pci_id == SST_MRST_PCI_ID) | |
239 | ret_val = sst_start_mrst(); | |
240 | else if (sst_drv_ctx->pci_id == SST_MFLD_PCI_ID) | |
241 | ret_val = sst_start_medfield(); | |
242 | if (ret_val) | |
243 | return ret_val; | |
244 | ||
d0f40c50 | 245 | pr_debug("fw loaded successful!!!\n"); |
fffa1cca VK |
246 | return ret_val; |
247 | } | |
248 | ||
249 | /*This function is called when any codec/post processing library | |
250 | needs to be downloaded*/ | |
251 | static int sst_download_library(const struct firmware *fw_lib, | |
252 | struct snd_sst_lib_download_info *lib) | |
253 | { | |
254 | /* send IPC message and wait */ | |
255 | int i; | |
256 | u8 pvt_id; | |
257 | struct ipc_post *msg = NULL; | |
258 | union config_status_reg csr; | |
259 | struct snd_sst_str_type str_type = {0}; | |
260 | int retval = 0; | |
261 | ||
262 | if (sst_create_large_msg(&msg)) | |
263 | return -ENOMEM; | |
264 | ||
265 | pvt_id = sst_assign_pvt_id(sst_drv_ctx); | |
266 | i = sst_get_block_stream(sst_drv_ctx); | |
d0f40c50 | 267 | pr_debug("alloc block allocated = %d, pvt_id %d\n", i, pvt_id); |
fffa1cca VK |
268 | if (i < 0) { |
269 | kfree(msg); | |
270 | return -ENOMEM; | |
271 | } | |
272 | sst_drv_ctx->alloc_block[i].sst_id = pvt_id; | |
273 | sst_fill_header(&msg->header, IPC_IA_PREP_LIB_DNLD, 1, pvt_id); | |
274 | msg->header.part.data = sizeof(u32) + sizeof(str_type); | |
275 | str_type.codec_type = lib->dload_lib.lib_info.lib_type; | |
276 | /*str_type.pvt_id = pvt_id;*/ | |
277 | memcpy(msg->mailbox_data, &msg->header, sizeof(u32)); | |
278 | memcpy(msg->mailbox_data + sizeof(u32), &str_type, sizeof(str_type)); | |
279 | spin_lock(&sst_drv_ctx->list_spin_lock); | |
280 | list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list); | |
281 | spin_unlock(&sst_drv_ctx->list_spin_lock); | |
282 | sst_post_message(&sst_drv_ctx->ipc_post_msg_wq); | |
283 | retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[i]); | |
284 | if (retval) { | |
285 | /* error */ | |
286 | sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT; | |
d0f40c50 | 287 | pr_err("Prep codec downloaded failed %d\n", |
fffa1cca VK |
288 | retval); |
289 | return -EIO; | |
290 | } | |
d0f40c50 | 291 | pr_debug("FW responded, ready for download now...\n"); |
fffa1cca VK |
292 | /* downloading on success */ |
293 | mutex_lock(&sst_drv_ctx->sst_lock); | |
294 | sst_drv_ctx->sst_state = SST_FW_LOADED; | |
295 | mutex_unlock(&sst_drv_ctx->sst_lock); | |
296 | csr.full = readl(sst_drv_ctx->shim + SST_CSR); | |
297 | csr.part.run_stall = 1; | |
298 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
299 | ||
300 | csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR); | |
301 | csr.part.bypass = 0x7; | |
302 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
303 | ||
304 | sst_parse_fw_image(fw_lib); | |
305 | ||
306 | /* set the FW to running again */ | |
307 | csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR); | |
308 | csr.part.bypass = 0x0; | |
309 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
310 | ||
311 | csr.full = sst_shim_read(sst_drv_ctx->shim, SST_CSR); | |
312 | csr.part.run_stall = 0; | |
313 | sst_shim_write(sst_drv_ctx->shim, SST_CSR, csr.full); | |
314 | ||
315 | /* send download complete and wait */ | |
316 | if (sst_create_large_msg(&msg)) { | |
317 | sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT; | |
318 | return -ENOMEM; | |
319 | } | |
320 | ||
321 | sst_fill_header(&msg->header, IPC_IA_LIB_DNLD_CMPLT, 1, pvt_id); | |
322 | sst_drv_ctx->alloc_block[i].sst_id = pvt_id; | |
323 | msg->header.part.data = sizeof(u32) + sizeof(*lib); | |
324 | lib->pvt_id = pvt_id; | |
325 | memcpy(msg->mailbox_data, &msg->header, sizeof(u32)); | |
326 | memcpy(msg->mailbox_data + sizeof(u32), lib, sizeof(*lib)); | |
327 | spin_lock(&sst_drv_ctx->list_spin_lock); | |
328 | list_add_tail(&msg->node, &sst_drv_ctx->ipc_dispatch_list); | |
329 | spin_unlock(&sst_drv_ctx->list_spin_lock); | |
330 | sst_post_message(&sst_drv_ctx->ipc_post_msg_wq); | |
d0f40c50 | 331 | pr_debug("Waiting for FW response Download complete\n"); |
fffa1cca VK |
332 | sst_drv_ctx->alloc_block[i].ops_block.condition = false; |
333 | retval = sst_wait_timeout(sst_drv_ctx, &sst_drv_ctx->alloc_block[i]); | |
334 | if (retval) { | |
335 | /* error */ | |
336 | mutex_lock(&sst_drv_ctx->sst_lock); | |
337 | sst_drv_ctx->sst_state = SST_UN_INIT; | |
338 | mutex_unlock(&sst_drv_ctx->sst_lock); | |
339 | sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT; | |
340 | return -EIO; | |
341 | } | |
342 | ||
d0f40c50 | 343 | pr_debug("FW success on Download complete\n"); |
fffa1cca VK |
344 | sst_drv_ctx->alloc_block[i].sst_id = BLOCK_UNINIT; |
345 | mutex_lock(&sst_drv_ctx->sst_lock); | |
346 | sst_drv_ctx->sst_state = SST_FW_RUNNING; | |
347 | mutex_unlock(&sst_drv_ctx->sst_lock); | |
348 | return 0; | |
349 | ||
350 | } | |
351 | ||
352 | /* This function is called befoer downloading the codec/postprocessing | |
353 | library is set for download to SST DSP*/ | |
354 | static int sst_validate_library(const struct firmware *fw_lib, | |
355 | struct lib_slot_info *slot, | |
356 | u32 *entry_point) | |
357 | { | |
358 | struct fw_header *header; | |
359 | struct fw_module_header *module; | |
360 | struct dma_block_info *block; | |
361 | unsigned int n_blk, isize = 0, dsize = 0; | |
362 | int err = 0; | |
363 | ||
364 | header = (struct fw_header *)fw_lib->data; | |
365 | if (header->modules != 1) { | |
d0f40c50 | 366 | pr_err("Module no mismatch found\n"); |
fffa1cca VK |
367 | err = -EINVAL; |
368 | goto exit; | |
369 | } | |
370 | module = (void *)fw_lib->data + sizeof(*header); | |
371 | *entry_point = module->entry_point; | |
d0f40c50 JP |
372 | pr_debug("Module entry point 0x%x\n", *entry_point); |
373 | pr_debug("Module Sign %s, Size 0x%x, Blocks 0x%x Type 0x%x\n", | |
fffa1cca VK |
374 | module->signature, module->mod_size, |
375 | module->blocks, module->type); | |
376 | ||
377 | block = (void *)module + sizeof(*module); | |
378 | for (n_blk = 0; n_blk < module->blocks; n_blk++) { | |
379 | switch (block->type) { | |
380 | case SST_IRAM: | |
381 | isize += block->size; | |
382 | break; | |
383 | case SST_DRAM: | |
384 | dsize += block->size; | |
385 | break; | |
386 | default: | |
d0f40c50 | 387 | pr_err("Invalid block type for 0x%x\n", n_blk); |
fffa1cca VK |
388 | err = -EINVAL; |
389 | goto exit; | |
390 | } | |
391 | block = (void *)block + sizeof(*block) + block->size; | |
392 | } | |
393 | if (isize > slot->iram_size || dsize > slot->dram_size) { | |
d0f40c50 | 394 | pr_err("library exceeds size allocated\n"); |
fffa1cca VK |
395 | err = -EINVAL; |
396 | goto exit; | |
397 | } else | |
d0f40c50 | 398 | pr_debug("Library is safe for download...\n"); |
fffa1cca | 399 | |
d0f40c50 | 400 | pr_debug("iram 0x%x, dram 0x%x, iram 0x%x, dram 0x%x\n", |
fffa1cca VK |
401 | isize, dsize, slot->iram_size, slot->dram_size); |
402 | exit: | |
403 | return err; | |
404 | ||
405 | } | |
406 | ||
407 | /* This function is called when FW requests for a particular libary download | |
408 | This function prepares the library to download*/ | |
409 | int sst_load_library(struct snd_sst_lib_download *lib, u8 ops) | |
410 | { | |
411 | char buf[20]; | |
412 | const char *type, *dir; | |
413 | int len = 0, error = 0; | |
414 | u32 entry_point; | |
415 | const struct firmware *fw_lib; | |
416 | struct snd_sst_lib_download_info dload_info = {{{0},},}; | |
417 | ||
418 | memset(buf, 0, sizeof(buf)); | |
419 | ||
d0f40c50 | 420 | pr_debug("Lib Type 0x%x, Slot 0x%x, ops 0x%x\n", |
fffa1cca | 421 | lib->lib_info.lib_type, lib->slot_info.slot_num, ops); |
d0f40c50 | 422 | pr_debug("Version 0x%x, name %s, caps 0x%x media type 0x%x\n", |
fffa1cca VK |
423 | lib->lib_info.lib_version, lib->lib_info.lib_name, |
424 | lib->lib_info.lib_caps, lib->lib_info.media_type); | |
425 | ||
d0f40c50 | 426 | pr_debug("IRAM Size 0x%x, offset 0x%x\n", |
fffa1cca | 427 | lib->slot_info.iram_size, lib->slot_info.iram_offset); |
d0f40c50 | 428 | pr_debug("DRAM Size 0x%x, offset 0x%x\n", |
fffa1cca VK |
429 | lib->slot_info.dram_size, lib->slot_info.dram_offset); |
430 | ||
431 | switch (lib->lib_info.lib_type) { | |
432 | case SST_CODEC_TYPE_MP3: | |
433 | type = "mp3_"; | |
434 | break; | |
435 | case SST_CODEC_TYPE_AAC: | |
436 | type = "aac_"; | |
437 | break; | |
438 | case SST_CODEC_TYPE_AACP: | |
439 | type = "aac_v1_"; | |
440 | break; | |
441 | case SST_CODEC_TYPE_eAACP: | |
442 | type = "aac_v2_"; | |
443 | break; | |
444 | case SST_CODEC_TYPE_WMA9: | |
445 | type = "wma9_"; | |
446 | break; | |
447 | default: | |
d0f40c50 | 448 | pr_err("Invalid codec type\n"); |
fffa1cca VK |
449 | error = -EINVAL; |
450 | goto wake; | |
451 | } | |
452 | ||
453 | if (ops == STREAM_OPS_CAPTURE) | |
454 | dir = "enc_"; | |
455 | else | |
456 | dir = "dec_"; | |
457 | len = strlen(type) + strlen(dir); | |
458 | strncpy(buf, type, sizeof(buf)-1); | |
459 | strncpy(buf + strlen(type), dir, sizeof(buf)-strlen(type)-1); | |
460 | len += snprintf(buf + len, sizeof(buf) - len, "%d", | |
461 | lib->slot_info.slot_num); | |
462 | len += snprintf(buf + len, sizeof(buf) - len, ".bin"); | |
463 | ||
d0f40c50 | 464 | pr_debug("Requesting %s\n", buf); |
fffa1cca VK |
465 | |
466 | error = request_firmware(&fw_lib, buf, &sst_drv_ctx->pci->dev); | |
467 | if (error) { | |
d0f40c50 | 468 | pr_err("library load failed %d\n", error); |
fffa1cca VK |
469 | goto wake; |
470 | } | |
471 | error = sst_validate_library(fw_lib, &lib->slot_info, &entry_point); | |
472 | if (error) | |
473 | goto wake_free; | |
474 | ||
475 | lib->mod_entry_pt = entry_point; | |
476 | memcpy(&dload_info.dload_lib, lib, sizeof(*lib)); | |
477 | error = sst_download_library(fw_lib, &dload_info); | |
478 | if (error) | |
479 | goto wake_free; | |
480 | ||
481 | /* lib is downloaded and init send alloc again */ | |
d0f40c50 | 482 | pr_debug("Library is downloaded now...\n"); |
fffa1cca VK |
483 | wake_free: |
484 | /* sst_wake_up_alloc_block(sst_drv_ctx, pvt_id, error, NULL); */ | |
485 | release_firmware(fw_lib); | |
486 | wake: | |
487 | return error; | |
488 | } | |
489 |