sst-baytrail-dsp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Intel Baytrail SST DSP driver
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/fs.h>
  16. #include <linux/slab.h>
  17. #include <linux/device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/firmware.h>
  23. #include "sst-dsp.h"
  24. #include "sst-dsp-priv.h"
  25. #include "sst-baytrail-ipc.h"
  26. #define SST_BYT_FW_SIGNATURE_SIZE 4
  27. #define SST_BYT_FW_SIGN "$SST"
  28. #define SST_BYT_IRAM_OFFSET 0xC0000
  29. #define SST_BYT_DRAM_OFFSET 0x100000
  30. #define SST_BYT_SHIM_OFFSET 0x140000
  31. enum sst_ram_type {
  32. SST_BYT_IRAM = 1,
  33. SST_BYT_DRAM = 2,
  34. SST_BYT_CACHE = 3,
  35. };
  36. struct dma_block_info {
  37. enum sst_ram_type type; /* IRAM/DRAM */
  38. u32 size; /* Bytes */
  39. u32 ram_offset; /* Offset in I/DRAM */
  40. u32 rsvd; /* Reserved field */
  41. };
  42. struct fw_header {
  43. unsigned char signature[SST_BYT_FW_SIGNATURE_SIZE];
  44. u32 file_size; /* size of fw minus this header */
  45. u32 modules; /* # of modules */
  46. u32 file_format; /* version of header format */
  47. u32 reserved[4];
  48. };
  49. struct sst_byt_fw_module_header {
  50. unsigned char signature[SST_BYT_FW_SIGNATURE_SIZE];
  51. u32 mod_size; /* size of module */
  52. u32 blocks; /* # of blocks */
  53. u32 type; /* codec type, pp lib */
  54. u32 entry_point;
  55. };
  56. static int sst_byt_parse_module(struct sst_dsp *dsp, struct sst_fw *fw,
  57. struct sst_byt_fw_module_header *module)
  58. {
  59. struct dma_block_info *block;
  60. struct sst_module *mod;
  61. struct sst_module_data block_data;
  62. struct sst_module_template template;
  63. int count;
  64. memset(&template, 0, sizeof(template));
  65. template.id = module->type;
  66. template.entry = module->entry_point;
  67. template.p.type = SST_MEM_DRAM;
  68. template.p.data_type = SST_DATA_P;
  69. template.s.type = SST_MEM_DRAM;
  70. template.s.data_type = SST_DATA_S;
  71. mod = sst_module_new(fw, &template, NULL);
  72. if (mod == NULL)
  73. return -ENOMEM;
  74. block = (void *)module + sizeof(*module);
  75. for (count = 0; count < module->blocks; count++) {
  76. if (block->size <= 0) {
  77. dev_err(dsp->dev, "block %d size invalid\n", count);
  78. return -EINVAL;
  79. }
  80. switch (block->type) {
  81. case SST_BYT_IRAM:
  82. block_data.offset = block->ram_offset +
  83. dsp->addr.iram_offset;
  84. block_data.type = SST_MEM_IRAM;
  85. break;
  86. case SST_BYT_DRAM:
  87. block_data.offset = block->ram_offset +
  88. dsp->addr.dram_offset;
  89. block_data.type = SST_MEM_DRAM;
  90. break;
  91. case SST_BYT_CACHE:
  92. block_data.offset = block->ram_offset +
  93. (dsp->addr.fw_ext - dsp->addr.lpe);
  94. block_data.type = SST_MEM_CACHE;
  95. break;
  96. default:
  97. dev_err(dsp->dev, "wrong ram type 0x%x in block0x%x\n",
  98. block->type, count);
  99. return -EINVAL;
  100. }
  101. block_data.size = block->size;
  102. block_data.data_type = SST_DATA_M;
  103. block_data.data = (void *)block + sizeof(*block);
  104. sst_module_insert_fixed_block(mod, &block_data);
  105. block = (void *)block + sizeof(*block) + block->size;
  106. }
  107. return 0;
  108. }
  109. static int sst_byt_parse_fw_image(struct sst_fw *sst_fw)
  110. {
  111. struct fw_header *header;
  112. struct sst_byt_fw_module_header *module;
  113. struct sst_dsp *dsp = sst_fw->dsp;
  114. int ret, count;
  115. /* Read the header information from the data pointer */
  116. header = (struct fw_header *)sst_fw->dma_buf;
  117. /* verify FW */
  118. if ((strncmp(header->signature, SST_BYT_FW_SIGN, 4) != 0) ||
  119. (sst_fw->size != header->file_size + sizeof(*header))) {
  120. /* Invalid FW signature */
  121. dev_err(dsp->dev, "Invalid FW sign/filesize mismatch\n");
  122. return -EINVAL;
  123. }
  124. dev_dbg(dsp->dev,
  125. "header sign=%4s size=0x%x modules=0x%x fmt=0x%x size=%zu\n",
  126. header->signature, header->file_size, header->modules,
  127. header->file_format, sizeof(*header));
  128. module = (void *)sst_fw->dma_buf + sizeof(*header);
  129. for (count = 0; count < header->modules; count++) {
  130. /* module */
  131. ret = sst_byt_parse_module(dsp, sst_fw, module);
  132. if (ret < 0) {
  133. dev_err(dsp->dev, "invalid module %d\n", count);
  134. return ret;
  135. }
  136. module = (void *)module + sizeof(*module) + module->mod_size;
  137. }
  138. return 0;
  139. }
  140. static void sst_byt_dump_shim(struct sst_dsp *sst)
  141. {
  142. int i;
  143. u64 reg;
  144. for (i = 0; i <= 0xF0; i += 8) {
  145. reg = sst_dsp_shim_read64_unlocked(sst, i);
  146. if (reg)
  147. dev_dbg(sst->dev, "shim 0x%2.2x value 0x%16.16llx\n",
  148. i, reg);
  149. }
  150. for (i = 0x00; i <= 0xff; i += 4) {
  151. reg = readl(sst->addr.pci_cfg + i);
  152. if (reg)
  153. dev_dbg(sst->dev, "pci 0x%2.2x value 0x%8.8x\n",
  154. i, (u32)reg);
  155. }
  156. }
  157. static irqreturn_t sst_byt_irq(int irq, void *context)
  158. {
  159. struct sst_dsp *sst = (struct sst_dsp *) context;
  160. u64 isrx;
  161. irqreturn_t ret = IRQ_NONE;
  162. spin_lock(&sst->spinlock);
  163. isrx = sst_dsp_shim_read64_unlocked(sst, SST_ISRX);
  164. if (isrx & SST_ISRX_DONE) {
  165. /* ADSP has processed the message request from IA */
  166. sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCX,
  167. SST_BYT_IPCX_DONE, 0);
  168. ret = IRQ_WAKE_THREAD;
  169. }
  170. if (isrx & SST_BYT_ISRX_REQUEST) {
  171. /* mask message request from ADSP and do processing later */
  172. sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX,
  173. SST_BYT_IMRX_REQUEST,
  174. SST_BYT_IMRX_REQUEST);
  175. ret = IRQ_WAKE_THREAD;
  176. }
  177. spin_unlock(&sst->spinlock);
  178. return ret;
  179. }
  180. static void sst_byt_boot(struct sst_dsp *sst)
  181. {
  182. int tries = 10;
  183. /*
  184. * save the physical address of extended firmware block in the first
  185. * 4 bytes of the mailbox
  186. */
  187. memcpy_toio(sst->addr.lpe + SST_BYT_MAILBOX_OFFSET,
  188. &sst->pdata->fw_base, sizeof(u32));
  189. /* release stall and wait to unstall */
  190. sst_dsp_shim_update_bits64(sst, SST_CSR, SST_BYT_CSR_STALL, 0x0);
  191. while (tries--) {
  192. if (!(sst_dsp_shim_read64(sst, SST_CSR) &
  193. SST_BYT_CSR_PWAITMODE))
  194. break;
  195. msleep(100);
  196. }
  197. if (tries < 0) {
  198. dev_err(sst->dev, "unable to start DSP\n");
  199. sst_byt_dump_shim(sst);
  200. }
  201. }
  202. static void sst_byt_reset(struct sst_dsp *sst)
  203. {
  204. /* put DSP into reset, set reset vector and stall */
  205. sst_dsp_shim_update_bits64(sst, SST_CSR,
  206. SST_BYT_CSR_RST | SST_BYT_CSR_VECTOR_SEL | SST_BYT_CSR_STALL,
  207. SST_BYT_CSR_RST | SST_BYT_CSR_VECTOR_SEL | SST_BYT_CSR_STALL);
  208. udelay(10);
  209. /* take DSP out of reset and keep stalled for FW loading */
  210. sst_dsp_shim_update_bits64(sst, SST_CSR, SST_BYT_CSR_RST, 0);
  211. }
  212. struct sst_adsp_memregion {
  213. u32 start;
  214. u32 end;
  215. int blocks;
  216. enum sst_mem_type type;
  217. };
  218. /* BYT test stuff */
  219. static const struct sst_adsp_memregion byt_region[] = {
  220. {0xC0000, 0x100000, 8, SST_MEM_IRAM}, /* I-SRAM - 8 * 32kB */
  221. {0x100000, 0x140000, 8, SST_MEM_DRAM}, /* D-SRAM0 - 8 * 32kB */
  222. };
  223. static int sst_byt_resource_map(struct sst_dsp *sst, struct sst_pdata *pdata)
  224. {
  225. sst->addr.lpe_base = pdata->lpe_base;
  226. sst->addr.lpe = ioremap(pdata->lpe_base, pdata->lpe_size);
  227. if (!sst->addr.lpe)
  228. return -ENODEV;
  229. /* ADSP PCI MMIO config space */
  230. sst->addr.pci_cfg = ioremap(pdata->pcicfg_base, pdata->pcicfg_size);
  231. if (!sst->addr.pci_cfg) {
  232. iounmap(sst->addr.lpe);
  233. return -ENODEV;
  234. }
  235. /* SST Extended FW allocation */
  236. sst->addr.fw_ext = ioremap(pdata->fw_base, pdata->fw_size);
  237. if (!sst->addr.fw_ext) {
  238. iounmap(sst->addr.pci_cfg);
  239. iounmap(sst->addr.lpe);
  240. return -ENODEV;
  241. }
  242. /* SST Shim */
  243. sst->addr.shim = sst->addr.lpe + sst->addr.shim_offset;
  244. sst_dsp_mailbox_init(sst, SST_BYT_MAILBOX_OFFSET + 0x204,
  245. SST_BYT_IPC_MAX_PAYLOAD_SIZE,
  246. SST_BYT_MAILBOX_OFFSET,
  247. SST_BYT_IPC_MAX_PAYLOAD_SIZE);
  248. sst->irq = pdata->irq;
  249. return 0;
  250. }
  251. static int sst_byt_init(struct sst_dsp *sst, struct sst_pdata *pdata)
  252. {
  253. const struct sst_adsp_memregion *region;
  254. struct device *dev;
  255. int ret = -ENODEV, i, j, region_count;
  256. u32 offset, size;
  257. dev = sst->dev;
  258. switch (sst->id) {
  259. case SST_DEV_ID_BYT:
  260. region = byt_region;
  261. region_count = ARRAY_SIZE(byt_region);
  262. sst->addr.iram_offset = SST_BYT_IRAM_OFFSET;
  263. sst->addr.dram_offset = SST_BYT_DRAM_OFFSET;
  264. sst->addr.shim_offset = SST_BYT_SHIM_OFFSET;
  265. break;
  266. default:
  267. dev_err(dev, "failed to get mem resources\n");
  268. return ret;
  269. }
  270. ret = sst_byt_resource_map(sst, pdata);
  271. if (ret < 0) {
  272. dev_err(dev, "failed to map resources\n");
  273. return ret;
  274. }
  275. ret = dma_coerce_mask_and_coherent(sst->dma_dev, DMA_BIT_MASK(32));
  276. if (ret)
  277. return ret;
  278. /* enable Interrupt from both sides */
  279. sst_dsp_shim_update_bits64(sst, SST_IMRX, 0x3, 0x0);
  280. sst_dsp_shim_update_bits64(sst, SST_IMRD, 0x3, 0x0);
  281. /* register DSP memory blocks - ideally we should get this from ACPI */
  282. for (i = 0; i < region_count; i++) {
  283. offset = region[i].start;
  284. size = (region[i].end - region[i].start) / region[i].blocks;
  285. /* register individual memory blocks */
  286. for (j = 0; j < region[i].blocks; j++) {
  287. sst_mem_block_register(sst, offset, size,
  288. region[i].type, NULL, j, sst);
  289. offset += size;
  290. }
  291. }
  292. return 0;
  293. }
  294. static void sst_byt_free(struct sst_dsp *sst)
  295. {
  296. sst_mem_block_unregister_all(sst);
  297. iounmap(sst->addr.lpe);
  298. iounmap(sst->addr.pci_cfg);
  299. iounmap(sst->addr.fw_ext);
  300. }
  301. struct sst_ops sst_byt_ops = {
  302. .reset = sst_byt_reset,
  303. .boot = sst_byt_boot,
  304. .write = sst_shim32_write,
  305. .read = sst_shim32_read,
  306. .write64 = sst_shim32_write64,
  307. .read64 = sst_shim32_read64,
  308. .ram_read = sst_memcpy_fromio_32,
  309. .ram_write = sst_memcpy_toio_32,
  310. .irq_handler = sst_byt_irq,
  311. .init = sst_byt_init,
  312. .free = sst_byt_free,
  313. .parse_fw = sst_byt_parse_fw_image,
  314. };