blob: 622da5d3e3b32d631356d0ac69ffc810883b044f [file] [log] [blame]
Jeeja KP92eb4f62016-03-11 10:12:56 +05301/*
2 * bxt-sst.c - DSP library functions for BXT platform
3 *
4 * Copyright (C) 2015-16 Intel Corp
5 * Author:Rafal Redzimski <rafal.f.redzimski@intel.com>
6 * Jeeja KP <jeeja.kp@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18#include <linux/module.h>
19#include <linux/delay.h>
20#include <linux/firmware.h>
21#include <linux/device.h>
22
23#include "../common/sst-dsp.h"
24#include "../common/sst-dsp-priv.h"
25#include "skl-sst-ipc.h"
26
27#define BXT_BASEFW_TIMEOUT 3000
28#define BXT_INIT_TIMEOUT 500
29#define BXT_IPC_PURGE_FW 0x01004000
30
31#define BXT_ROM_INIT 0x5
32#define BXT_ADSP_SRAM0_BASE 0x80000
33
34/* Firmware status window */
35#define BXT_ADSP_FW_STATUS BXT_ADSP_SRAM0_BASE
36#define BXT_ADSP_ERROR_CODE (BXT_ADSP_FW_STATUS + 0x4)
37
38#define BXT_ADSP_SRAM1_BASE 0xA0000
39
40static unsigned int bxt_get_errorcode(struct sst_dsp *ctx)
41{
42 return sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE);
43}
44
45static int sst_bxt_prepare_fw(struct sst_dsp *ctx,
46 const void *fwdata, u32 fwsize)
47{
48 int stream_tag, ret, i;
49 u32 reg;
50
51 stream_tag = ctx->dsp_ops.prepare(ctx->dev, 0x40, fwsize, &ctx->dmab);
52 if (stream_tag < 0) {
53 dev_err(ctx->dev, "Failed to prepare DMA FW loading err: %x\n",
54 stream_tag);
55 return stream_tag;
56 }
57
58 ctx->dsp_ops.stream_tag = stream_tag;
59 memcpy(ctx->dmab.area, fwdata, fwsize);
60
Senthilnathan Veppur20235762016-06-13 17:59:02 +053061 ret = skl_dsp_core_power_up(ctx);
Jeeja KP92eb4f62016-03-11 10:12:56 +053062 if (ret < 0) {
63 dev_err(ctx->dev, "Boot dsp core failed ret: %d\n", ret);
Senthilnathan Veppur20235762016-06-13 17:59:02 +053064 goto base_fw_load_failed;
65 }
66
67 /* Purge FW request */
68 sst_dsp_shim_write(ctx, SKL_ADSP_REG_HIPCI, SKL_ADSP_REG_HIPCI_BUSY |
69 (BXT_IPC_PURGE_FW | ((stream_tag - 1) << 9)));
70
71 ret = skl_dsp_start_core(ctx);
72 if (ret < 0) {
73 dev_err(ctx->dev, "Start dsp core failed ret: %d\n", ret);
Jeeja KP92eb4f62016-03-11 10:12:56 +053074 ret = -EIO;
75 goto base_fw_load_failed;
76 }
77
78 for (i = BXT_INIT_TIMEOUT; i > 0; --i) {
79 reg = sst_dsp_shim_read(ctx, SKL_ADSP_REG_HIPCIE);
80
81 if (reg & SKL_ADSP_REG_HIPCIE_DONE) {
82 sst_dsp_shim_update_bits_forced(ctx,
83 SKL_ADSP_REG_HIPCIE,
84 SKL_ADSP_REG_HIPCIE_DONE,
85 SKL_ADSP_REG_HIPCIE_DONE);
86 break;
87 }
88 mdelay(1);
89 }
90 if (!i) {
91 dev_info(ctx->dev, "Waiting for HIPCIE done, reg: 0x%x\n", reg);
92 sst_dsp_shim_update_bits(ctx, SKL_ADSP_REG_HIPCIE,
93 SKL_ADSP_REG_HIPCIE_DONE,
94 SKL_ADSP_REG_HIPCIE_DONE);
95 }
96
97 /* enable Interrupt */
98 skl_ipc_int_enable(ctx);
99 skl_ipc_op_int_enable(ctx);
100
101 for (i = BXT_INIT_TIMEOUT; i > 0; --i) {
102 if (SKL_FW_INIT ==
103 (sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS) &
104 SKL_FW_STS_MASK)) {
105
106 dev_info(ctx->dev, "ROM loaded, continue FW loading\n");
107 break;
108 }
109 mdelay(1);
110 }
111 if (!i) {
112 dev_err(ctx->dev, "Timeout for ROM init, HIPCIE: 0x%x\n", reg);
113 ret = -EIO;
114 goto base_fw_load_failed;
115 }
116
117 return ret;
118
119base_fw_load_failed:
120 ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, stream_tag);
121 skl_dsp_disable_core(ctx);
122 return ret;
123}
124
125static int sst_transfer_fw_host_dma(struct sst_dsp *ctx)
126{
127 int ret;
128
129 ctx->dsp_ops.trigger(ctx->dev, true, ctx->dsp_ops.stream_tag);
130 ret = sst_dsp_register_poll(ctx, BXT_ADSP_FW_STATUS, SKL_FW_STS_MASK,
131 BXT_ROM_INIT, BXT_BASEFW_TIMEOUT, "Firmware boot");
132
133 ctx->dsp_ops.trigger(ctx->dev, false, ctx->dsp_ops.stream_tag);
134 ctx->dsp_ops.cleanup(ctx->dev, &ctx->dmab, ctx->dsp_ops.stream_tag);
135
136 return ret;
137}
138
Vinod Koul3467a642016-05-30 17:43:01 +0530139#define BXT_ADSP_FW_BIN_HDR_OFFSET 0x2000
140
Jeeja KP92eb4f62016-03-11 10:12:56 +0530141static int bxt_load_base_firmware(struct sst_dsp *ctx)
142{
Vinod Koulbf242d12016-05-30 17:42:58 +0530143 struct firmware stripped_fw;
Jeeja KP92eb4f62016-03-11 10:12:56 +0530144 struct skl_sst *skl = ctx->thread_context;
145 int ret;
146
Vinod Koulfdfa82e2016-05-30 17:42:56 +0530147 ret = request_firmware(&ctx->fw, ctx->fw_name, ctx->dev);
Jeeja KP92eb4f62016-03-11 10:12:56 +0530148 if (ret < 0) {
149 dev_err(ctx->dev, "Request firmware failed %d\n", ret);
150 goto sst_load_base_firmware_failed;
151 }
152
Vinod Koulbf242d12016-05-30 17:42:58 +0530153 /* check for extended manifest */
154 if (ctx->fw == NULL)
155 goto sst_load_base_firmware_failed;
156
Vinod Koul3467a642016-05-30 17:43:01 +0530157 ret = snd_skl_parse_uuids(ctx, BXT_ADSP_FW_BIN_HDR_OFFSET);
158 if (ret < 0)
159 goto sst_load_base_firmware_failed;
Vinod Koulbf242d12016-05-30 17:42:58 +0530160
161 stripped_fw.data = ctx->fw->data;
162 stripped_fw.size = ctx->fw->size;
163 skl_dsp_strip_extended_manifest(&stripped_fw);
164
165 ret = sst_bxt_prepare_fw(ctx, stripped_fw.data, stripped_fw.size);
Jeeja KP92eb4f62016-03-11 10:12:56 +0530166 /* Retry Enabling core and ROM load. Retry seemed to help */
167 if (ret < 0) {
Vinod Koulbf242d12016-05-30 17:42:58 +0530168 ret = sst_bxt_prepare_fw(ctx, stripped_fw.data, stripped_fw.size);
Jeeja KP92eb4f62016-03-11 10:12:56 +0530169 if (ret < 0) {
Senthilnathan Veppur20235762016-06-13 17:59:02 +0530170 dev_err(ctx->dev, "Error code=0x%x: FW status=0x%x\n",
171 sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE),
172 sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS));
173
Jeeja KP92eb4f62016-03-11 10:12:56 +0530174 dev_err(ctx->dev, "Core En/ROM load fail:%d\n", ret);
175 goto sst_load_base_firmware_failed;
176 }
177 }
178
179 ret = sst_transfer_fw_host_dma(ctx);
180 if (ret < 0) {
181 dev_err(ctx->dev, "Transfer firmware failed %d\n", ret);
182 dev_info(ctx->dev, "Error code=0x%x: FW status=0x%x\n",
183 sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE),
184 sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS));
185
186 skl_dsp_disable_core(ctx);
187 } else {
188 dev_dbg(ctx->dev, "Firmware download successful\n");
189 ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
190 msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
191 if (ret == 0) {
192 dev_err(ctx->dev, "DSP boot fail, FW Ready timeout\n");
193 skl_dsp_disable_core(ctx);
194 ret = -EIO;
195 } else {
196 skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
197 ret = 0;
Jayachandran B1665c172016-06-13 17:59:01 +0530198 skl->fw_loaded = true;
Jeeja KP92eb4f62016-03-11 10:12:56 +0530199 }
200 }
201
202sst_load_base_firmware_failed:
Vinod Koulfdfa82e2016-05-30 17:42:56 +0530203 release_firmware(ctx->fw);
Jeeja KP92eb4f62016-03-11 10:12:56 +0530204 return ret;
205}
206
207static int bxt_set_dsp_D0(struct sst_dsp *ctx)
208{
209 struct skl_sst *skl = ctx->thread_context;
210 int ret;
211
212 skl->boot_complete = false;
213
Jayachandran B1665c172016-06-13 17:59:01 +0530214 if (skl->fw_loaded == false) {
215 dev_dbg(ctx->dev, "Re-loading fw\n");
216 ret = bxt_load_base_firmware(ctx);
217 if (ret < 0)
218 dev_err(ctx->dev, "reload fw failed: %d\n", ret);
219 return ret;
220 }
221
Jeeja KP92eb4f62016-03-11 10:12:56 +0530222 ret = skl_dsp_enable_core(ctx);
223 if (ret < 0) {
224 dev_err(ctx->dev, "enable dsp core failed ret: %d\n", ret);
225 return ret;
226 }
227
228 /* enable interrupt */
229 skl_ipc_int_enable(ctx);
230 skl_ipc_op_int_enable(ctx);
231
232 ret = wait_event_timeout(skl->boot_wait, skl->boot_complete,
233 msecs_to_jiffies(SKL_IPC_BOOT_MSECS));
234 if (ret == 0) {
235 dev_err(ctx->dev, "ipc: error DSP boot timeout\n");
236 dev_err(ctx->dev, "Error code=0x%x: FW status=0x%x\n",
237 sst_dsp_shim_read(ctx, BXT_ADSP_ERROR_CODE),
238 sst_dsp_shim_read(ctx, BXT_ADSP_FW_STATUS));
239 return -EIO;
240 }
241
242 skl_dsp_set_state_locked(ctx, SKL_DSP_RUNNING);
243 return 0;
244}
245
246static int bxt_set_dsp_D3(struct sst_dsp *ctx)
247{
248 struct skl_ipc_dxstate_info dx;
249 struct skl_sst *skl = ctx->thread_context;
250 int ret = 0;
251
252 if (!is_skl_dsp_running(ctx))
253 return ret;
254
255 dx.core_mask = SKL_DSP_CORE0_MASK;
256 dx.dx_mask = SKL_IPC_D3_MASK;
257
258 ret = skl_ipc_set_dx(&skl->ipc, SKL_INSTANCE_ID,
259 SKL_BASE_FW_MODULE_ID, &dx);
260 if (ret < 0) {
261 dev_err(ctx->dev, "Failed to set DSP to D3 state: %d\n", ret);
262 return ret;
263 }
264
265 ret = skl_dsp_disable_core(ctx);
266 if (ret < 0) {
267 dev_err(ctx->dev, "disbale dsp core failed: %d\n", ret);
268 ret = -EIO;
269 }
270
271 skl_dsp_set_state_locked(ctx, SKL_DSP_RESET);
272 return 0;
273}
274
275static struct skl_dsp_fw_ops bxt_fw_ops = {
276 .set_state_D0 = bxt_set_dsp_D0,
277 .set_state_D3 = bxt_set_dsp_D3,
278 .load_fw = bxt_load_base_firmware,
279 .get_fw_errcode = bxt_get_errorcode,
280};
281
282static struct sst_ops skl_ops = {
283 .irq_handler = skl_dsp_sst_interrupt,
284 .write = sst_shim32_write,
285 .read = sst_shim32_read,
286 .ram_read = sst_memcpy_fromio_32,
287 .ram_write = sst_memcpy_toio_32,
288 .free = skl_dsp_free,
289};
290
291static struct sst_dsp_device skl_dev = {
292 .thread = skl_dsp_irq_thread_handler,
293 .ops = &skl_ops,
294};
295
296int bxt_sst_dsp_init(struct device *dev, void __iomem *mmio_base, int irq,
297 const char *fw_name, struct skl_dsp_loader_ops dsp_ops,
298 struct skl_sst **dsp)
299{
300 struct skl_sst *skl;
301 struct sst_dsp *sst;
302 int ret;
303
304 skl = devm_kzalloc(dev, sizeof(*skl), GFP_KERNEL);
305 if (skl == NULL)
306 return -ENOMEM;
307
308 skl->dev = dev;
309 skl_dev.thread_context = skl;
Vinod Koul3467a642016-05-30 17:43:01 +0530310 INIT_LIST_HEAD(&skl->uuid_list);
Jeeja KP92eb4f62016-03-11 10:12:56 +0530311
312 skl->dsp = skl_dsp_ctx_init(dev, &skl_dev, irq);
313 if (!skl->dsp) {
314 dev_err(skl->dev, "skl_dsp_ctx_init failed\n");
315 return -ENODEV;
316 }
317
318 sst = skl->dsp;
319 sst->fw_name = fw_name;
320 sst->dsp_ops = dsp_ops;
321 sst->fw_ops = bxt_fw_ops;
322 sst->addr.lpe = mmio_base;
323 sst->addr.shim = mmio_base;
324
325 sst_dsp_mailbox_init(sst, (BXT_ADSP_SRAM0_BASE + SKL_ADSP_W0_STAT_SZ),
326 SKL_ADSP_W0_UP_SZ, BXT_ADSP_SRAM1_BASE, SKL_ADSP_W1_SZ);
327
328 ret = skl_ipc_init(dev, skl);
329 if (ret)
330 return ret;
331
332 skl->boot_complete = false;
333 init_waitqueue_head(&skl->boot_wait);
334
335 ret = sst->fw_ops.load_fw(sst);
336 if (ret < 0) {
337 dev_err(dev, "Load base fw failed: %x", ret);
338 return ret;
339 }
340
341 if (dsp)
342 *dsp = skl;
343
344 return 0;
345}
346EXPORT_SYMBOL_GPL(bxt_sst_dsp_init);
347
348
349void bxt_sst_dsp_cleanup(struct device *dev, struct skl_sst *ctx)
350{
Vinod Koul3467a642016-05-30 17:43:01 +0530351 skl_freeup_uuid_list(ctx);
Jeeja KP92eb4f62016-03-11 10:12:56 +0530352 skl_ipc_free(&ctx->ipc);
353 ctx->dsp->cl_dev.ops.cl_cleanup_controller(ctx->dsp);
354
355 if (ctx->dsp->addr.lpe)
356 iounmap(ctx->dsp->addr.lpe);
357
358 ctx->dsp->ops->free(ctx->dsp);
359}
360EXPORT_SYMBOL_GPL(bxt_sst_dsp_cleanup);
361
362MODULE_LICENSE("GPL v2");
363MODULE_DESCRIPTION("Intel Broxton IPC driver");