|
| 1 | +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) |
| 2 | +// |
| 3 | +// Copyright(c) 2022 Mediatek Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Author: Allen-KH Cheng <allen-kh.cheng@mediatek.com> |
| 6 | +// Tinghan Shen <tinghan.shen@mediatek.com> |
| 7 | + |
| 8 | +/* |
| 9 | + * Hardware interface for audio DSP on mt8186 |
| 10 | + */ |
| 11 | + |
| 12 | +#include <linux/delay.h> |
| 13 | +#include <linux/firmware.h> |
| 14 | +#include <linux/io.h> |
| 15 | +#include <linux/of_address.h> |
| 16 | +#include <linux/of_irq.h> |
| 17 | +#include <linux/of_platform.h> |
| 18 | +#include <linux/of_reserved_mem.h> |
| 19 | +#include <linux/module.h> |
| 20 | + |
| 21 | +#include <sound/sof.h> |
| 22 | +#include <sound/sof/xtensa.h> |
| 23 | +#include "../../ops.h" |
| 24 | +#include "../../sof-of-dev.h" |
| 25 | +#include "../../sof-audio.h" |
| 26 | +#include "../adsp_helper.h" |
| 27 | +#include "mt8186.h" |
| 28 | + |
| 29 | +static int platform_parse_resource(struct platform_device *pdev, void *data) |
| 30 | +{ |
| 31 | + struct resource *mmio; |
| 32 | + struct resource res; |
| 33 | + struct device_node *mem_region; |
| 34 | + struct device *dev = &pdev->dev; |
| 35 | + struct mtk_adsp_chip_info *adsp = data; |
| 36 | + int ret; |
| 37 | + |
| 38 | + mem_region = of_parse_phandle(dev->of_node, "memory-region", 0); |
| 39 | + if (!mem_region) { |
| 40 | + dev_err(dev, "no dma memory-region phandle\n"); |
| 41 | + return -ENODEV; |
| 42 | + } |
| 43 | + |
| 44 | + ret = of_address_to_resource(mem_region, 0, &res); |
| 45 | + of_node_put(mem_region); |
| 46 | + if (ret) { |
| 47 | + dev_err(dev, "of_address_to_resource dma failed\n"); |
| 48 | + return ret; |
| 49 | + } |
| 50 | + |
| 51 | + dev_dbg(dev, "DMA %pR\n", &res); |
| 52 | + |
| 53 | + ret = of_reserved_mem_device_init(dev); |
| 54 | + if (ret) { |
| 55 | + dev_err(dev, "of_reserved_mem_device_init failed\n"); |
| 56 | + return ret; |
| 57 | + } |
| 58 | + |
| 59 | + mem_region = of_parse_phandle(dev->of_node, "memory-region", 1); |
| 60 | + if (!mem_region) { |
| 61 | + dev_err(dev, "no memory-region sysmem phandle\n"); |
| 62 | + return -ENODEV; |
| 63 | + } |
| 64 | + |
| 65 | + ret = of_address_to_resource(mem_region, 0, &res); |
| 66 | + of_node_put(mem_region); |
| 67 | + if (ret) { |
| 68 | + dev_err(dev, "of_address_to_resource sysmem failed\n"); |
| 69 | + return ret; |
| 70 | + } |
| 71 | + |
| 72 | + adsp->pa_dram = (phys_addr_t)res.start; |
| 73 | + if (adsp->pa_dram & DRAM_REMAP_MASK) { |
| 74 | + dev_err(dev, "adsp memory(%#x) is not 4K-aligned\n", |
| 75 | + (u32)adsp->pa_dram); |
| 76 | + return -EINVAL; |
| 77 | + } |
| 78 | + |
| 79 | + adsp->dramsize = resource_size(&res); |
| 80 | + if (adsp->dramsize < TOTAL_SIZE_SHARED_DRAM_FROM_TAIL) { |
| 81 | + dev_err(dev, "adsp memory(%#x) is not enough for share\n", |
| 82 | + adsp->dramsize); |
| 83 | + return -EINVAL; |
| 84 | + } |
| 85 | + |
| 86 | + dev_dbg(dev, "dram pbase=%pa size=%#x\n", &adsp->pa_dram, adsp->dramsize); |
| 87 | + |
| 88 | + mmio = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg"); |
| 89 | + if (!mmio) { |
| 90 | + dev_err(dev, "no ADSP-CFG register resource\n"); |
| 91 | + return -ENXIO; |
| 92 | + } |
| 93 | + |
| 94 | + adsp->va_cfgreg = devm_ioremap_resource(dev, mmio); |
| 95 | + if (IS_ERR(adsp->va_cfgreg)) |
| 96 | + return PTR_ERR(adsp->va_cfgreg); |
| 97 | + |
| 98 | + adsp->pa_cfgreg = (phys_addr_t)mmio->start; |
| 99 | + adsp->cfgregsize = resource_size(mmio); |
| 100 | + |
| 101 | + dev_dbg(dev, "cfgreg pbase=%pa size=%#x\n", &adsp->pa_cfgreg, adsp->cfgregsize); |
| 102 | + |
| 103 | + mmio = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram"); |
| 104 | + if (!mmio) { |
| 105 | + dev_err(dev, "no SRAM resource\n"); |
| 106 | + return -ENXIO; |
| 107 | + } |
| 108 | + |
| 109 | + adsp->pa_sram = (phys_addr_t)mmio->start; |
| 110 | + adsp->sramsize = resource_size(mmio); |
| 111 | + |
| 112 | + dev_dbg(dev, "sram pbase=%pa size=%#x\n", &adsp->pa_sram, adsp->sramsize); |
| 113 | + |
| 114 | + mmio = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sec"); |
| 115 | + if (!mmio) { |
| 116 | + dev_err(dev, "no SEC register resource\n"); |
| 117 | + return -ENXIO; |
| 118 | + } |
| 119 | + |
| 120 | + adsp->va_secreg = devm_ioremap_resource(dev, mmio); |
| 121 | + if (IS_ERR(adsp->va_secreg)) |
| 122 | + return PTR_ERR(adsp->va_secreg); |
| 123 | + |
| 124 | + adsp->pa_secreg = (phys_addr_t)mmio->start; |
| 125 | + adsp->secregsize = resource_size(mmio); |
| 126 | + |
| 127 | + dev_dbg(dev, "secreg pbase=%pa size=%#x\n", &adsp->pa_secreg, adsp->secregsize); |
| 128 | + |
| 129 | + mmio = platform_get_resource_byname(pdev, IORESOURCE_MEM, "bus"); |
| 130 | + if (!mmio) { |
| 131 | + dev_err(dev, "no BUS register resource\n"); |
| 132 | + return -ENXIO; |
| 133 | + } |
| 134 | + |
| 135 | + adsp->va_busreg = devm_ioremap_resource(dev, mmio); |
| 136 | + if (IS_ERR(adsp->va_busreg)) |
| 137 | + return PTR_ERR(adsp->va_busreg); |
| 138 | + |
| 139 | + adsp->pa_busreg = (phys_addr_t)mmio->start; |
| 140 | + adsp->busregsize = resource_size(mmio); |
| 141 | + |
| 142 | + dev_dbg(dev, "busreg pbase=%pa size=%#x\n", &adsp->pa_busreg, adsp->busregsize); |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
| 146 | + |
| 147 | +static void adsp_sram_power_on(struct snd_sof_dev *sdev) |
| 148 | +{ |
| 149 | + snd_sof_dsp_update_bits(sdev, DSP_BUSREG_BAR, ADSP_SRAM_POOL_CON, |
| 150 | + DSP_SRAM_POOL_PD_MASK, 0); |
| 151 | +} |
| 152 | + |
| 153 | +static void adsp_sram_power_off(struct snd_sof_dev *sdev) |
| 154 | +{ |
| 155 | + snd_sof_dsp_update_bits(sdev, DSP_BUSREG_BAR, ADSP_SRAM_POOL_CON, |
| 156 | + DSP_SRAM_POOL_PD_MASK, DSP_SRAM_POOL_PD_MASK); |
| 157 | +} |
| 158 | + |
| 159 | +/* Init the basic DSP DRAM address */ |
| 160 | +static int adsp_memory_remap_init(struct snd_sof_dev *sdev, struct mtk_adsp_chip_info *adsp) |
| 161 | +{ |
| 162 | + u32 offset; |
| 163 | + |
| 164 | + offset = adsp->pa_dram - DRAM_PHYS_BASE_FROM_DSP_VIEW; |
| 165 | + adsp->dram_offset = offset; |
| 166 | + offset >>= DRAM_REMAP_SHIFT; |
| 167 | + |
| 168 | + dev_dbg(sdev->dev, "adsp->pa_dram %pa, offset %#x\n", &adsp->pa_dram, offset); |
| 169 | + |
| 170 | + snd_sof_dsp_write(sdev, DSP_BUSREG_BAR, DSP_C0_EMI_MAP_ADDR, offset); |
| 171 | + snd_sof_dsp_write(sdev, DSP_BUSREG_BAR, DSP_C0_DMAEMI_MAP_ADDR, offset); |
| 172 | + |
| 173 | + if (offset != snd_sof_dsp_read(sdev, DSP_BUSREG_BAR, DSP_C0_EMI_MAP_ADDR) || |
| 174 | + offset != snd_sof_dsp_read(sdev, DSP_BUSREG_BAR, DSP_C0_DMAEMI_MAP_ADDR)) { |
| 175 | + dev_err(sdev->dev, "emi remap fail\n"); |
| 176 | + return -EIO; |
| 177 | + } |
| 178 | + |
| 179 | + return 0; |
| 180 | +} |
| 181 | + |
| 182 | +static int adsp_shared_base_ioremap(struct platform_device *pdev, void *data) |
| 183 | +{ |
| 184 | + struct device *dev = &pdev->dev; |
| 185 | + struct mtk_adsp_chip_info *adsp = data; |
| 186 | + u32 shared_size; |
| 187 | + |
| 188 | + /* remap shared-dram base to be non-cachable */ |
| 189 | + shared_size = TOTAL_SIZE_SHARED_DRAM_FROM_TAIL; |
| 190 | + adsp->pa_shared_dram = adsp->pa_dram + adsp->dramsize - shared_size; |
| 191 | + if (adsp->va_dram) { |
| 192 | + adsp->shared_dram = adsp->va_dram + DSP_DRAM_SIZE - shared_size; |
| 193 | + } else { |
| 194 | + adsp->shared_dram = devm_ioremap(dev, adsp->pa_shared_dram, |
| 195 | + shared_size); |
| 196 | + if (!adsp->shared_dram) { |
| 197 | + dev_err(dev, "ioremap failed for shared DRAM\n"); |
| 198 | + return -ENOMEM; |
| 199 | + } |
| 200 | + } |
| 201 | + dev_dbg(dev, "shared-dram vbase=%p, phy addr :%pa, size=%#x\n", |
| 202 | + adsp->shared_dram, &adsp->pa_shared_dram, shared_size); |
| 203 | + |
| 204 | + return 0; |
| 205 | +} |
| 206 | + |
| 207 | +static int mt8186_dsp_probe(struct snd_sof_dev *sdev) |
| 208 | +{ |
| 209 | + struct platform_device *pdev = container_of(sdev->dev, struct platform_device, dev); |
| 210 | + struct adsp_priv *priv; |
| 211 | + int ret; |
| 212 | + |
| 213 | + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 214 | + if (!priv) |
| 215 | + return -ENOMEM; |
| 216 | + |
| 217 | + sdev->pdata->hw_pdata = priv; |
| 218 | + priv->dev = sdev->dev; |
| 219 | + priv->sdev = sdev; |
| 220 | + |
| 221 | + priv->adsp = devm_kzalloc(&pdev->dev, sizeof(struct mtk_adsp_chip_info), GFP_KERNEL); |
| 222 | + if (!priv->adsp) |
| 223 | + return -ENOMEM; |
| 224 | + |
| 225 | + ret = platform_parse_resource(pdev, priv->adsp); |
| 226 | + if (ret) |
| 227 | + return ret; |
| 228 | + |
| 229 | + sdev->bar[SOF_FW_BLK_TYPE_IRAM] = devm_ioremap(sdev->dev, |
| 230 | + priv->adsp->pa_sram, |
| 231 | + priv->adsp->sramsize); |
| 232 | + if (!sdev->bar[SOF_FW_BLK_TYPE_IRAM]) { |
| 233 | + dev_err(sdev->dev, "failed to ioremap base %pa size %#x\n", |
| 234 | + &priv->adsp->pa_sram, priv->adsp->sramsize); |
| 235 | + return -ENOMEM; |
| 236 | + } |
| 237 | + |
| 238 | + sdev->bar[SOF_FW_BLK_TYPE_SRAM] = devm_ioremap_wc(sdev->dev, |
| 239 | + priv->adsp->pa_dram, |
| 240 | + priv->adsp->dramsize); |
| 241 | + if (!sdev->bar[SOF_FW_BLK_TYPE_SRAM]) { |
| 242 | + dev_err(sdev->dev, "failed to ioremap base %pa size %#x\n", |
| 243 | + &priv->adsp->pa_dram, priv->adsp->dramsize); |
| 244 | + return -ENOMEM; |
| 245 | + } |
| 246 | + |
| 247 | + priv->adsp->va_dram = sdev->bar[SOF_FW_BLK_TYPE_SRAM]; |
| 248 | + |
| 249 | + ret = adsp_shared_base_ioremap(pdev, priv->adsp); |
| 250 | + if (ret) { |
| 251 | + dev_err(sdev->dev, "adsp_shared_base_ioremap fail!\n"); |
| 252 | + return ret; |
| 253 | + } |
| 254 | + |
| 255 | + sdev->bar[DSP_REG_BAR] = priv->adsp->va_cfgreg; |
| 256 | + sdev->bar[DSP_SECREG_BAR] = priv->adsp->va_secreg; |
| 257 | + sdev->bar[DSP_BUSREG_BAR] = priv->adsp->va_busreg; |
| 258 | + |
| 259 | + sdev->mmio_bar = SOF_FW_BLK_TYPE_SRAM; |
| 260 | + sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM; |
| 261 | + |
| 262 | + ret = adsp_memory_remap_init(sdev, priv->adsp); |
| 263 | + if (ret) { |
| 264 | + dev_err(sdev->dev, "adsp_memory_remap_init fail!\n"); |
| 265 | + return ret; |
| 266 | + } |
| 267 | + |
| 268 | + adsp_sram_power_on(sdev); |
| 269 | + |
| 270 | + return 0; |
| 271 | +} |
| 272 | + |
| 273 | +static int mt8186_dsp_remove(struct snd_sof_dev *sdev) |
| 274 | +{ |
| 275 | + adsp_sram_power_off(sdev); |
| 276 | + |
| 277 | + return 0; |
| 278 | +} |
| 279 | + |
| 280 | +/* on mt8186 there is 1 to 1 match between type and BAR idx */ |
| 281 | +static int mt8186_get_bar_index(struct snd_sof_dev *sdev, u32 type) |
| 282 | +{ |
| 283 | + return type; |
| 284 | +} |
| 285 | + |
| 286 | +/* mt8186 ops */ |
| 287 | +static struct snd_sof_dsp_ops sof_mt8186_ops = { |
| 288 | + /* probe and remove */ |
| 289 | + .probe = mt8186_dsp_probe, |
| 290 | + .remove = mt8186_dsp_remove, |
| 291 | + |
| 292 | + /* Block IO */ |
| 293 | + .block_read = sof_block_read, |
| 294 | + .block_write = sof_block_write, |
| 295 | + |
| 296 | + /* Register IO */ |
| 297 | + .write = sof_io_write, |
| 298 | + .read = sof_io_read, |
| 299 | + .write64 = sof_io_write64, |
| 300 | + .read64 = sof_io_read64, |
| 301 | + |
| 302 | + /* misc */ |
| 303 | + .get_bar_index = mt8186_get_bar_index, |
| 304 | + |
| 305 | + /* Firmware ops */ |
| 306 | + .dsp_arch_ops = &sof_xtensa_arch_ops, |
| 307 | + |
| 308 | + /* ALSA HW info flags */ |
| 309 | + .hw_info = SNDRV_PCM_INFO_MMAP | |
| 310 | + SNDRV_PCM_INFO_MMAP_VALID | |
| 311 | + SNDRV_PCM_INFO_INTERLEAVED | |
| 312 | + SNDRV_PCM_INFO_PAUSE | |
| 313 | + SNDRV_PCM_INFO_NO_PERIOD_WAKEUP, |
| 314 | +}; |
| 315 | + |
| 316 | +static const struct sof_dev_desc sof_of_mt8186_desc = { |
| 317 | + .ipc_supported_mask = BIT(SOF_IPC), |
| 318 | + .ipc_default = SOF_IPC, |
| 319 | + .default_fw_path = { |
| 320 | + [SOF_IPC] = "mediatek/sof", |
| 321 | + }, |
| 322 | + .default_tplg_path = { |
| 323 | + [SOF_IPC] = "mediatek/sof-tplg", |
| 324 | + }, |
| 325 | + .default_fw_filename = { |
| 326 | + [SOF_IPC] = "sof-mt8186.ri", |
| 327 | + }, |
| 328 | + .nocodec_tplg_filename = "sof-mt8186-nocodec.tplg", |
| 329 | + .ops = &sof_mt8186_ops, |
| 330 | +}; |
| 331 | + |
| 332 | +static const struct of_device_id sof_of_mt8186_ids[] = { |
| 333 | + { .compatible = "mediatek,mt8186-dsp", .data = &sof_of_mt8186_desc}, |
| 334 | + { } |
| 335 | +}; |
| 336 | +MODULE_DEVICE_TABLE(of, sof_of_mt8186_ids); |
| 337 | + |
| 338 | +/* DT driver definition */ |
| 339 | +static struct platform_driver snd_sof_of_mt8186_driver = { |
| 340 | + .probe = sof_of_probe, |
| 341 | + .remove = sof_of_remove, |
| 342 | + .driver = { |
| 343 | + .name = "sof-audio-of-mt8186", |
| 344 | + .pm = &sof_of_pm, |
| 345 | + .of_match_table = sof_of_mt8186_ids, |
| 346 | + }, |
| 347 | +}; |
| 348 | +module_platform_driver(snd_sof_of_mt8186_driver); |
| 349 | + |
| 350 | +MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA); |
| 351 | +MODULE_IMPORT_NS(SND_SOC_SOF_MTK_COMMON); |
| 352 | +MODULE_LICENSE("Dual BSD/GPL"); |
0 commit comments