sst-haswell-dsp.c 14 KB

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