Commit | Line | Data |
---|---|---|
a4b12990 MB |
1 | /* |
2 | * Intel Haswell SST DSP driver | |
3 | * | |
4 | * Copyright (C) 2013, Intel Corporation. All rights reserved. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License version | |
8 | * 2 as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | */ | |
16 | ||
17 | #include <linux/delay.h> | |
18 | #include <linux/fs.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/device.h> | |
21 | #include <linux/sched.h> | |
22 | #include <linux/export.h> | |
23 | #include <linux/interrupt.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/dma-mapping.h> | |
26 | #include <linux/platform_device.h> | |
27 | #include <linux/pci.h> | |
28 | #include <linux/firmware.h> | |
29 | #include <linux/pm_runtime.h> | |
30 | ||
31 | #include <linux/acpi.h> | |
32 | #include <acpi/acpi_bus.h> | |
33 | ||
34 | #include "sst-dsp.h" | |
35 | #include "sst-dsp-priv.h" | |
36 | #include "sst-haswell-ipc.h" | |
37 | ||
38 | #include <trace/events/hswadsp.h> | |
39 | ||
40 | #define SST_HSW_FW_SIGNATURE_SIZE 4 | |
41 | #define SST_HSW_FW_SIGN "$SST" | |
42 | #define SST_HSW_FW_LIB_SIGN "$LIB" | |
43 | ||
44 | #define SST_WPT_SHIM_OFFSET 0xFB000 | |
45 | #define SST_LP_SHIM_OFFSET 0xE7000 | |
46 | #define SST_WPT_IRAM_OFFSET 0xA0000 | |
47 | #define SST_LP_IRAM_OFFSET 0x80000 | |
48 | ||
49 | #define SST_SHIM_PM_REG 0x84 | |
50 | ||
51 | #define SST_HSW_IRAM 1 | |
52 | #define SST_HSW_DRAM 2 | |
53 | #define SST_HSW_REGS 3 | |
54 | ||
55 | struct dma_block_info { | |
56 | __le32 type; /* IRAM/DRAM */ | |
57 | __le32 size; /* Bytes */ | |
58 | __le32 ram_offset; /* Offset in I/DRAM */ | |
59 | __le32 rsvd; /* Reserved field */ | |
60 | } __attribute__((packed)); | |
61 | ||
62 | struct fw_module_info { | |
63 | __le32 persistent_size; | |
64 | __le32 scratch_size; | |
65 | } __attribute__((packed)); | |
66 | ||
67 | struct fw_header { | |
68 | unsigned char signature[SST_HSW_FW_SIGNATURE_SIZE]; /* FW signature */ | |
69 | __le32 file_size; /* size of fw minus this header */ | |
70 | __le32 modules; /* # of modules */ | |
71 | __le32 file_format; /* version of header format */ | |
72 | __le32 reserved[4]; | |
73 | } __attribute__((packed)); | |
74 | ||
75 | struct fw_module_header { | |
76 | unsigned char signature[SST_HSW_FW_SIGNATURE_SIZE]; /* module signature */ | |
77 | __le32 mod_size; /* size of module */ | |
78 | __le32 blocks; /* # of blocks */ | |
79 | __le16 padding; | |
80 | __le16 type; /* codec type, pp lib */ | |
81 | __le32 entry_point; | |
82 | struct fw_module_info info; | |
83 | } __attribute__((packed)); | |
84 | ||
85 | static void hsw_free(struct sst_dsp *sst); | |
86 | ||
87 | static int hsw_parse_module(struct sst_dsp *dsp, struct sst_fw *fw, | |
88 | struct fw_module_header *module) | |
89 | { | |
90 | struct dma_block_info *block; | |
91 | struct sst_module *mod; | |
92 | struct sst_module_data block_data; | |
93 | struct sst_module_template template; | |
94 | int count; | |
95 | void __iomem *ram; | |
96 | ||
97 | /* TODO: allowed module types need to be configurable */ | |
98 | if (module->type != SST_HSW_MODULE_BASE_FW | |
99 | && module->type != SST_HSW_MODULE_PCM_SYSTEM | |
100 | && module->type != SST_HSW_MODULE_PCM | |
101 | && module->type != SST_HSW_MODULE_PCM_REFERENCE | |
102 | && module->type != SST_HSW_MODULE_PCM_CAPTURE | |
103 | && module->type != SST_HSW_MODULE_LPAL) | |
104 | return 0; | |
105 | ||
106 | dev_dbg(dsp->dev, "new module sign 0x%s size 0x%x blocks 0x%x type 0x%x\n", | |
107 | module->signature, module->mod_size, | |
108 | module->blocks, module->type); | |
109 | dev_dbg(dsp->dev, " entrypoint 0x%x\n", module->entry_point); | |
110 | dev_dbg(dsp->dev, " persistent 0x%x scratch 0x%x\n", | |
111 | module->info.persistent_size, module->info.scratch_size); | |
112 | ||
113 | memset(&template, 0, sizeof(template)); | |
114 | template.id = module->type; | |
115 | template.entry = module->entry_point; | |
116 | template.p.size = module->info.persistent_size; | |
117 | template.p.type = SST_MEM_DRAM; | |
118 | template.p.data_type = SST_DATA_P; | |
119 | template.s.size = module->info.scratch_size; | |
120 | template.s.type = SST_MEM_DRAM; | |
121 | template.s.data_type = SST_DATA_S; | |
122 | ||
123 | mod = sst_module_new(fw, &template, NULL); | |
124 | if (mod == NULL) | |
125 | return -ENOMEM; | |
126 | ||
127 | block = (void *)module + sizeof(*module); | |
128 | ||
129 | for (count = 0; count < module->blocks; count++) { | |
130 | ||
131 | if (block->size <= 0) { | |
132 | dev_err(dsp->dev, | |
133 | "error: block %d size invalid\n", count); | |
134 | sst_module_free(mod); | |
135 | return -EINVAL; | |
136 | } | |
137 | ||
138 | switch (block->type) { | |
139 | case SST_HSW_IRAM: | |
140 | ram = dsp->addr.lpe; | |
141 | block_data.offset = | |
142 | block->ram_offset + dsp->addr.iram_offset; | |
143 | block_data.type = SST_MEM_IRAM; | |
144 | break; | |
145 | case SST_HSW_DRAM: | |
146 | ram = dsp->addr.lpe; | |
147 | block_data.offset = block->ram_offset; | |
148 | block_data.type = SST_MEM_DRAM; | |
149 | break; | |
150 | default: | |
151 | dev_err(dsp->dev, "error: bad type 0x%x for block 0x%x\n", | |
152 | block->type, count); | |
153 | sst_module_free(mod); | |
154 | return -EINVAL; | |
155 | } | |
156 | ||
157 | block_data.size = block->size; | |
158 | block_data.data_type = SST_DATA_M; | |
159 | block_data.data = (void *)block + sizeof(*block); | |
160 | block_data.data_offset = block_data.data - fw->dma_buf; | |
161 | ||
162 | dev_dbg(dsp->dev, "copy firmware block %d type 0x%x " | |
163 | "size 0x%x ==> ram %p offset 0x%x\n", | |
164 | count, block->type, block->size, ram, | |
165 | block->ram_offset); | |
166 | ||
167 | sst_module_insert_fixed_block(mod, &block_data); | |
168 | ||
169 | block = (void *)block + sizeof(*block) + block->size; | |
170 | } | |
171 | return 0; | |
172 | } | |
173 | ||
174 | static int hsw_parse_fw_image(struct sst_fw *sst_fw) | |
175 | { | |
176 | struct fw_header *header; | |
177 | struct sst_module *scratch; | |
178 | struct fw_module_header *module; | |
179 | struct sst_dsp *dsp = sst_fw->dsp; | |
180 | struct sst_hsw *hsw = sst_fw->private; | |
181 | int ret, count; | |
182 | ||
183 | /* Read the header information from the data pointer */ | |
184 | header = (struct fw_header *)sst_fw->dma_buf; | |
185 | ||
186 | /* verify FW */ | |
187 | if ((strncmp(header->signature, SST_HSW_FW_SIGN, 4) != 0) || | |
188 | (sst_fw->size != header->file_size + sizeof(*header))) { | |
189 | dev_err(dsp->dev, "error: invalid fw sign/filesize mismatch\n"); | |
190 | return -EINVAL; | |
191 | } | |
192 | ||
193 | dev_dbg(dsp->dev, "header size=0x%x modules=0x%x fmt=0x%x size=%zu\n", | |
194 | header->file_size, header->modules, | |
195 | header->file_format, sizeof(*header)); | |
196 | ||
197 | /* parse each module */ | |
198 | module = (void *)sst_fw->dma_buf + sizeof(*header); | |
199 | for (count = 0; count < header->modules; count++) { | |
200 | ||
201 | /* module */ | |
202 | ret = hsw_parse_module(dsp, sst_fw, module); | |
203 | if (ret < 0) { | |
204 | dev_err(dsp->dev, "error: invalid module %d\n", count); | |
205 | return ret; | |
206 | } | |
207 | module = (void *)module + sizeof(*module) + module->mod_size; | |
208 | } | |
209 | ||
210 | /* allocate persistent/scratch mem regions */ | |
211 | scratch = sst_mem_block_alloc_scratch(dsp); | |
212 | if (scratch == NULL) | |
213 | return -ENOMEM; | |
214 | ||
215 | sst_hsw_set_scratch_module(hsw, scratch); | |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
220 | static irqreturn_t hsw_irq(int irq, void *context) | |
221 | { | |
222 | struct sst_dsp *sst = (struct sst_dsp *) context; | |
223 | u32 isr; | |
224 | int ret = IRQ_NONE; | |
225 | ||
226 | spin_lock(&sst->spinlock); | |
227 | ||
228 | /* Interrupt arrived, check src */ | |
229 | isr = sst_dsp_shim_read_unlocked(sst, SST_ISRX); | |
230 | if (isr & SST_ISRX_DONE) { | |
231 | trace_sst_irq_done(isr, | |
232 | sst_dsp_shim_read_unlocked(sst, SST_IMRX)); | |
233 | ||
234 | /* Mask Done interrupt before return */ | |
235 | sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX, | |
236 | SST_IMRX_DONE, SST_IMRX_DONE); | |
237 | ret = IRQ_WAKE_THREAD; | |
238 | } | |
239 | ||
240 | if (isr & SST_ISRX_BUSY) { | |
241 | trace_sst_irq_busy(isr, | |
242 | sst_dsp_shim_read_unlocked(sst, SST_IMRX)); | |
243 | ||
244 | /* Mask Busy interrupt before return */ | |
245 | sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX, | |
246 | SST_IMRX_BUSY, SST_IMRX_BUSY); | |
247 | ret = IRQ_WAKE_THREAD; | |
248 | } | |
249 | ||
250 | spin_unlock(&sst->spinlock); | |
251 | return ret; | |
252 | } | |
253 | ||
254 | static void hsw_boot(struct sst_dsp *sst) | |
255 | { | |
256 | /* select SSP1 19.2MHz base clock, SSP clock 0, turn off Low Power Clock */ | |
257 | sst_dsp_shim_update_bits_unlocked(sst, SST_CSR, | |
258 | SST_CSR_S1IOCS | SST_CSR_SBCS1 | SST_CSR_LPCS, 0x0); | |
259 | ||
260 | /* stall DSP core, set clk to 192/96Mhz */ | |
261 | sst_dsp_shim_update_bits_unlocked(sst, | |
262 | SST_CSR, SST_CSR_STALL | SST_CSR_DCS_MASK, | |
263 | SST_CSR_STALL | SST_CSR_DCS(4)); | |
264 | ||
265 | /* Set 24MHz MCLK, prevent local clock gating, enable SSP0 clock */ | |
266 | sst_dsp_shim_update_bits_unlocked(sst, SST_CLKCTL, | |
267 | SST_CLKCTL_MASK | SST_CLKCTL_DCPLCG | SST_CLKCTL_SCOE0, | |
268 | SST_CLKCTL_MASK | SST_CLKCTL_DCPLCG | SST_CLKCTL_SCOE0); | |
269 | ||
270 | /* disable DMA finish function for SSP0 & SSP1 */ | |
271 | sst_dsp_shim_update_bits_unlocked(sst, SST_CSR2, SST_CSR2_SDFD_SSP1, | |
272 | SST_CSR2_SDFD_SSP1); | |
273 | ||
274 | /* enable DMA engine 0,1 all channels to access host memory */ | |
275 | sst_dsp_shim_update_bits_unlocked(sst, SST_HDMC, | |
276 | SST_HDMC_HDDA1(0xff) | SST_HDMC_HDDA0(0xff), | |
277 | SST_HDMC_HDDA1(0xff) | SST_HDMC_HDDA0(0xff)); | |
278 | ||
279 | /* disable all clock gating */ | |
280 | writel(0x0, sst->addr.pci_cfg + SST_VDRTCTL2); | |
281 | ||
282 | /* set DSP to RUN */ | |
283 | sst_dsp_shim_update_bits_unlocked(sst, SST_CSR, SST_CSR_STALL, 0x0); | |
284 | } | |
285 | ||
286 | static void hsw_reset(struct sst_dsp *sst) | |
287 | { | |
288 | /* put DSP into reset and stall */ | |
289 | sst_dsp_shim_update_bits_unlocked(sst, SST_CSR, | |
290 | SST_CSR_RST | SST_CSR_STALL, SST_CSR_RST | SST_CSR_STALL); | |
291 | ||
292 | /* keep in reset for 10ms */ | |
293 | mdelay(10); | |
294 | ||
295 | /* take DSP out of reset and keep stalled for FW loading */ | |
296 | sst_dsp_shim_update_bits_unlocked(sst, SST_CSR, | |
297 | SST_CSR_RST | SST_CSR_STALL, SST_CSR_STALL); | |
298 | } | |
299 | ||
300 | struct sst_adsp_memregion { | |
301 | u32 start; | |
302 | u32 end; | |
303 | int blocks; | |
304 | enum sst_mem_type type; | |
305 | }; | |
306 | ||
307 | /* lynx point ADSP mem regions */ | |
308 | static const struct sst_adsp_memregion lp_region[] = { | |
309 | {0x00000, 0x40000, 8, SST_MEM_DRAM}, /* D-SRAM0 - 8 * 32kB */ | |
310 | {0x40000, 0x80000, 8, SST_MEM_DRAM}, /* D-SRAM1 - 8 * 32kB */ | |
311 | {0x80000, 0xE0000, 12, SST_MEM_IRAM}, /* I-SRAM - 12 * 32kB */ | |
312 | }; | |
313 | ||
314 | /* wild cat point ADSP mem regions */ | |
315 | static const struct sst_adsp_memregion wpt_region[] = { | |
15446c0b | 316 | {0x00000, 0xA0000, 20, SST_MEM_DRAM}, /* D-SRAM0,D-SRAM1,D-SRAM2 - 20 * 32kB */ |
a4b12990 MB |
317 | {0xA0000, 0xF0000, 10, SST_MEM_IRAM}, /* I-SRAM - 10 * 32kB */ |
318 | }; | |
319 | ||
320 | static int hsw_acpi_resource_map(struct sst_dsp *sst, struct sst_pdata *pdata) | |
321 | { | |
322 | /* ADSP DRAM & IRAM */ | |
323 | sst->addr.lpe_base = pdata->lpe_base; | |
324 | sst->addr.lpe = ioremap(pdata->lpe_base, pdata->lpe_size); | |
325 | if (!sst->addr.lpe) | |
326 | return -ENODEV; | |
327 | ||
328 | /* ADSP PCI MMIO config space */ | |
329 | sst->addr.pci_cfg = ioremap(pdata->pcicfg_base, pdata->pcicfg_size); | |
330 | if (!sst->addr.pci_cfg) { | |
331 | iounmap(sst->addr.lpe); | |
332 | return -ENODEV; | |
333 | } | |
334 | ||
335 | /* SST Shim */ | |
336 | sst->addr.shim = sst->addr.lpe + sst->addr.shim_offset; | |
337 | return 0; | |
338 | } | |
339 | ||
340 | static u32 hsw_block_get_bit(struct sst_mem_block *block) | |
341 | { | |
342 | u32 bit = 0, shift = 0; | |
343 | ||
344 | switch (block->type) { | |
345 | case SST_MEM_DRAM: | |
346 | shift = 16; | |
347 | break; | |
348 | case SST_MEM_IRAM: | |
349 | shift = 6; | |
350 | break; | |
351 | default: | |
352 | return 0; | |
353 | } | |
354 | ||
355 | bit = 1 << (block->index + shift); | |
356 | ||
357 | return bit; | |
358 | } | |
359 | ||
360 | /* enable 32kB memory block - locks held by caller */ | |
361 | static int hsw_block_enable(struct sst_mem_block *block) | |
362 | { | |
363 | struct sst_dsp *sst = block->dsp; | |
364 | u32 bit, val; | |
365 | ||
366 | if (block->users++ > 0) | |
367 | return 0; | |
368 | ||
369 | dev_dbg(block->dsp->dev, " enabled block %d:%d at offset 0x%x\n", | |
370 | block->type, block->index, block->offset); | |
371 | ||
372 | val = readl(sst->addr.pci_cfg + SST_VDRTCTL0); | |
373 | bit = hsw_block_get_bit(block); | |
374 | writel(val & ~bit, sst->addr.pci_cfg + SST_VDRTCTL0); | |
375 | ||
376 | /* wait 18 DSP clock ticks */ | |
377 | udelay(10); | |
378 | ||
379 | return 0; | |
380 | } | |
381 | ||
382 | /* disable 32kB memory block - locks held by caller */ | |
383 | static int hsw_block_disable(struct sst_mem_block *block) | |
384 | { | |
385 | struct sst_dsp *sst = block->dsp; | |
386 | u32 bit, val; | |
387 | ||
388 | if (--block->users > 0) | |
389 | return 0; | |
390 | ||
391 | dev_dbg(block->dsp->dev, " disabled block %d:%d at offset 0x%x\n", | |
392 | block->type, block->index, block->offset); | |
393 | ||
394 | val = readl(sst->addr.pci_cfg + SST_VDRTCTL0); | |
395 | bit = hsw_block_get_bit(block); | |
396 | writel(val | bit, sst->addr.pci_cfg + SST_VDRTCTL0); | |
397 | ||
398 | return 0; | |
399 | } | |
400 | ||
401 | static struct sst_block_ops sst_hsw_ops = { | |
402 | .enable = hsw_block_enable, | |
403 | .disable = hsw_block_disable, | |
404 | }; | |
405 | ||
406 | static int hsw_enable_shim(struct sst_dsp *sst) | |
407 | { | |
408 | int tries = 10; | |
409 | u32 reg; | |
410 | ||
411 | /* enable shim */ | |
412 | reg = readl(sst->addr.pci_cfg + SST_SHIM_PM_REG); | |
413 | writel(reg & ~0x3, sst->addr.pci_cfg + SST_SHIM_PM_REG); | |
414 | ||
415 | /* check that ADSP shim is enabled */ | |
416 | while (tries--) { | |
417 | reg = sst_dsp_shim_read_unlocked(sst, SST_CSR); | |
418 | if (reg != 0xffffffff) | |
419 | return 0; | |
420 | ||
421 | msleep(1); | |
422 | } | |
423 | ||
424 | return -ENODEV; | |
425 | } | |
426 | ||
427 | static int hsw_init(struct sst_dsp *sst, struct sst_pdata *pdata) | |
428 | { | |
429 | const struct sst_adsp_memregion *region; | |
430 | struct device *dev; | |
431 | int ret = -ENODEV, i, j, region_count; | |
432 | u32 offset, size; | |
433 | ||
10df3509 | 434 | dev = sst->dma_dev; |
a4b12990 MB |
435 | |
436 | switch (sst->id) { | |
437 | case SST_DEV_ID_LYNX_POINT: | |
438 | region = lp_region; | |
439 | region_count = ARRAY_SIZE(lp_region); | |
440 | sst->addr.iram_offset = SST_LP_IRAM_OFFSET; | |
441 | sst->addr.shim_offset = SST_LP_SHIM_OFFSET; | |
442 | break; | |
443 | case SST_DEV_ID_WILDCAT_POINT: | |
444 | region = wpt_region; | |
445 | region_count = ARRAY_SIZE(wpt_region); | |
446 | sst->addr.iram_offset = SST_WPT_IRAM_OFFSET; | |
447 | sst->addr.shim_offset = SST_WPT_SHIM_OFFSET; | |
448 | break; | |
449 | default: | |
450 | dev_err(dev, "error: failed to get mem resources\n"); | |
451 | return ret; | |
452 | } | |
453 | ||
454 | ret = hsw_acpi_resource_map(sst, pdata); | |
455 | if (ret < 0) { | |
456 | dev_err(dev, "error: failed to map resources\n"); | |
457 | return ret; | |
458 | } | |
459 | ||
460 | /* enable the DSP SHIM */ | |
461 | ret = hsw_enable_shim(sst); | |
462 | if (ret < 0) { | |
463 | dev_err(dev, "error: failed to set DSP D0 and reset SHIM\n"); | |
464 | return ret; | |
465 | } | |
466 | ||
10df3509 | 467 | ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(31)); |
a4b12990 MB |
468 | if (ret) |
469 | return ret; | |
470 | ||
471 | /* Enable Interrupt from both sides */ | |
472 | sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX, 0x3, 0x0); | |
473 | sst_dsp_shim_update_bits_unlocked(sst, SST_IMRD, | |
474 | (0x3 | 0x1 << 16 | 0x3 << 21), 0x0); | |
475 | ||
476 | /* register DSP memory blocks - ideally we should get this from ACPI */ | |
477 | for (i = 0; i < region_count; i++) { | |
478 | offset = region[i].start; | |
479 | size = (region[i].end - region[i].start) / region[i].blocks; | |
480 | ||
481 | /* register individual memory blocks */ | |
482 | for (j = 0; j < region[i].blocks; j++) { | |
483 | sst_mem_block_register(sst, offset, size, | |
484 | region[i].type, &sst_hsw_ops, j, sst); | |
485 | offset += size; | |
486 | } | |
487 | } | |
488 | ||
489 | /* set default power gating mask */ | |
490 | writel(0x0, sst->addr.pci_cfg + SST_VDRTCTL0); | |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
495 | static void hsw_free(struct sst_dsp *sst) | |
496 | { | |
497 | sst_mem_block_unregister_all(sst); | |
498 | iounmap(sst->addr.lpe); | |
499 | iounmap(sst->addr.pci_cfg); | |
500 | } | |
501 | ||
502 | struct sst_ops haswell_ops = { | |
503 | .reset = hsw_reset, | |
504 | .boot = hsw_boot, | |
505 | .write = sst_shim32_write, | |
506 | .read = sst_shim32_read, | |
507 | .write64 = sst_shim32_write64, | |
508 | .read64 = sst_shim32_read64, | |
509 | .ram_read = sst_memcpy_fromio_32, | |
510 | .ram_write = sst_memcpy_toio_32, | |
511 | .irq_handler = hsw_irq, | |
512 | .init = hsw_init, | |
513 | .free = hsw_free, | |
514 | .parse_fw = hsw_parse_fw_image, | |
515 | }; |