1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Platform driver for the Synopsys DesignWare DMA Controller 4 * 5 * Copyright (C) 2007-2008 Atmel Corporation 6 * Copyright (C) 2010-2011 ST Microelectronics 7 * Copyright (C) 2013 Intel Corporation 8 * 9 * Some parts of this driver are derived from the original dw_dmac. 10 */ 11 12 #include <linux/module.h> 13 #include <linux/device.h> 14 #include <linux/clk.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/platform_device.h> 17 #include <linux/dmaengine.h> 18 #include <linux/dma-mapping.h> 19 #include <linux/of.h> 20 #include <linux/of_dma.h> 21 #include <linux/acpi.h> 22 #include <linux/acpi_dma.h> 23 24 #include "internal.h" 25 26 #define DRV_NAME "dw_dmac" 27 28 static struct dma_chan *dw_dma_of_xlate(struct of_phandle_args *dma_spec, 29 struct of_dma *ofdma) 30 { 31 struct dw_dma *dw = ofdma->of_dma_data; 32 struct dw_dma_slave slave = { 33 .dma_dev = dw->dma.dev, 34 }; 35 dma_cap_mask_t cap; 36 37 if (dma_spec->args_count != 3) 38 return NULL; 39 40 slave.src_id = dma_spec->args[0]; 41 slave.dst_id = dma_spec->args[0]; 42 slave.m_master = dma_spec->args[1]; 43 slave.p_master = dma_spec->args[2]; 44 45 if (WARN_ON(slave.src_id >= DW_DMA_MAX_NR_REQUESTS || 46 slave.dst_id >= DW_DMA_MAX_NR_REQUESTS || 47 slave.m_master >= dw->pdata->nr_masters || 48 slave.p_master >= dw->pdata->nr_masters)) 49 return NULL; 50 51 dma_cap_zero(cap); 52 dma_cap_set(DMA_SLAVE, cap); 53 54 /* TODO: there should be a simpler way to do this */ 55 return dma_request_channel(cap, dw_dma_filter, &slave); 56 } 57 58 #ifdef CONFIG_ACPI 59 static bool dw_dma_acpi_filter(struct dma_chan *chan, void *param) 60 { 61 struct acpi_dma_spec *dma_spec = param; 62 struct dw_dma_slave slave = { 63 .dma_dev = dma_spec->dev, 64 .src_id = dma_spec->slave_id, 65 .dst_id = dma_spec->slave_id, 66 .m_master = 0, 67 .p_master = 1, 68 }; 69 70 return dw_dma_filter(chan, &slave); 71 } 72 73 static void dw_dma_acpi_controller_register(struct dw_dma *dw) 74 { 75 struct device *dev = dw->dma.dev; 76 struct acpi_dma_filter_info *info; 77 int ret; 78 79 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 80 if (!info) 81 return; 82 83 dma_cap_zero(info->dma_cap); 84 dma_cap_set(DMA_SLAVE, info->dma_cap); 85 info->filter_fn = dw_dma_acpi_filter; 86 87 ret = devm_acpi_dma_controller_register(dev, acpi_dma_simple_xlate, 88 info); 89 if (ret) 90 dev_err(dev, "could not register acpi_dma_controller\n"); 91 } 92 #else /* !CONFIG_ACPI */ 93 static inline void dw_dma_acpi_controller_register(struct dw_dma *dw) {} 94 #endif /* !CONFIG_ACPI */ 95 96 #ifdef CONFIG_OF 97 static struct dw_dma_platform_data * 98 dw_dma_parse_dt(struct platform_device *pdev) 99 { 100 struct device_node *np = pdev->dev.of_node; 101 struct dw_dma_platform_data *pdata; 102 u32 tmp, arr[DW_DMA_MAX_NR_MASTERS], mb[DW_DMA_MAX_NR_CHANNELS]; 103 u32 nr_masters; 104 u32 nr_channels; 105 106 if (!np) { 107 dev_err(&pdev->dev, "Missing DT data\n"); 108 return NULL; 109 } 110 111 if (of_property_read_u32(np, "dma-masters", &nr_masters)) 112 return NULL; 113 if (nr_masters < 1 || nr_masters > DW_DMA_MAX_NR_MASTERS) 114 return NULL; 115 116 if (of_property_read_u32(np, "dma-channels", &nr_channels)) 117 return NULL; 118 if (nr_channels > DW_DMA_MAX_NR_CHANNELS) 119 return NULL; 120 121 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 122 if (!pdata) 123 return NULL; 124 125 pdata->nr_masters = nr_masters; 126 pdata->nr_channels = nr_channels; 127 128 if (!of_property_read_u32(np, "chan_allocation_order", &tmp)) 129 pdata->chan_allocation_order = (unsigned char)tmp; 130 131 if (!of_property_read_u32(np, "chan_priority", &tmp)) 132 pdata->chan_priority = tmp; 133 134 if (!of_property_read_u32(np, "block_size", &tmp)) 135 pdata->block_size = tmp; 136 137 if (!of_property_read_u32_array(np, "data-width", arr, nr_masters)) { 138 for (tmp = 0; tmp < nr_masters; tmp++) 139 pdata->data_width[tmp] = arr[tmp]; 140 } else if (!of_property_read_u32_array(np, "data_width", arr, nr_masters)) { 141 for (tmp = 0; tmp < nr_masters; tmp++) 142 pdata->data_width[tmp] = BIT(arr[tmp] & 0x07); 143 } 144 145 if (!of_property_read_u32_array(np, "multi-block", mb, nr_channels)) { 146 for (tmp = 0; tmp < nr_channels; tmp++) 147 pdata->multi_block[tmp] = mb[tmp]; 148 } else { 149 for (tmp = 0; tmp < nr_channels; tmp++) 150 pdata->multi_block[tmp] = 1; 151 } 152 153 if (!of_property_read_u32(np, "snps,dma-protection-control", &tmp)) { 154 if (tmp > CHAN_PROTCTL_MASK) 155 return NULL; 156 pdata->protctl = tmp; 157 } 158 159 return pdata; 160 } 161 #else 162 static inline struct dw_dma_platform_data * 163 dw_dma_parse_dt(struct platform_device *pdev) 164 { 165 return NULL; 166 } 167 #endif 168 169 static int dw_probe(struct platform_device *pdev) 170 { 171 const struct dw_dma_chip_pdata *match; 172 struct dw_dma_chip_pdata *data; 173 struct dw_dma_chip *chip; 174 struct device *dev = &pdev->dev; 175 struct resource *mem; 176 int err; 177 178 match = device_get_match_data(dev); 179 if (!match) 180 return -ENODEV; 181 182 data = devm_kmemdup(&pdev->dev, match, sizeof(*match), GFP_KERNEL); 183 if (!data) 184 return -ENOMEM; 185 186 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 187 if (!chip) 188 return -ENOMEM; 189 190 chip->irq = platform_get_irq(pdev, 0); 191 if (chip->irq < 0) 192 return chip->irq; 193 194 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 195 chip->regs = devm_ioremap_resource(dev, mem); 196 if (IS_ERR(chip->regs)) 197 return PTR_ERR(chip->regs); 198 199 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 200 if (err) 201 return err; 202 203 if (!data->pdata) 204 data->pdata = dev_get_platdata(dev); 205 if (!data->pdata) 206 data->pdata = dw_dma_parse_dt(pdev); 207 208 chip->dev = dev; 209 chip->id = pdev->id; 210 chip->pdata = data->pdata; 211 212 data->chip = chip; 213 214 chip->clk = devm_clk_get(chip->dev, "hclk"); 215 if (IS_ERR(chip->clk)) 216 return PTR_ERR(chip->clk); 217 err = clk_prepare_enable(chip->clk); 218 if (err) 219 return err; 220 221 pm_runtime_enable(&pdev->dev); 222 223 err = data->probe(chip); 224 if (err) 225 goto err_dw_dma_probe; 226 227 platform_set_drvdata(pdev, data); 228 229 if (pdev->dev.of_node) { 230 err = of_dma_controller_register(pdev->dev.of_node, 231 dw_dma_of_xlate, chip->dw); 232 if (err) 233 dev_err(&pdev->dev, 234 "could not register of_dma_controller\n"); 235 } 236 237 if (ACPI_HANDLE(&pdev->dev)) 238 dw_dma_acpi_controller_register(chip->dw); 239 240 return 0; 241 242 err_dw_dma_probe: 243 pm_runtime_disable(&pdev->dev); 244 clk_disable_unprepare(chip->clk); 245 return err; 246 } 247 248 static int dw_remove(struct platform_device *pdev) 249 { 250 struct dw_dma_chip_pdata *data = platform_get_drvdata(pdev); 251 struct dw_dma_chip *chip = data->chip; 252 int ret; 253 254 if (pdev->dev.of_node) 255 of_dma_controller_free(pdev->dev.of_node); 256 257 ret = data->remove(chip); 258 if (ret) 259 dev_warn(chip->dev, "can't remove device properly: %d\n", ret); 260 261 pm_runtime_disable(&pdev->dev); 262 clk_disable_unprepare(chip->clk); 263 264 return 0; 265 } 266 267 static void dw_shutdown(struct platform_device *pdev) 268 { 269 struct dw_dma_chip_pdata *data = platform_get_drvdata(pdev); 270 struct dw_dma_chip *chip = data->chip; 271 272 /* 273 * We have to call do_dw_dma_disable() to stop any ongoing transfer. On 274 * some platforms we can't do that since DMA device is powered off. 275 * Moreover we have no possibility to check if the platform is affected 276 * or not. That's why we call pm_runtime_get_sync() / pm_runtime_put() 277 * unconditionally. On the other hand we can't use 278 * pm_runtime_suspended() because runtime PM framework is not fully 279 * used by the driver. 280 */ 281 pm_runtime_get_sync(chip->dev); 282 do_dw_dma_disable(chip); 283 pm_runtime_put_sync_suspend(chip->dev); 284 285 clk_disable_unprepare(chip->clk); 286 } 287 288 #ifdef CONFIG_OF 289 static const struct of_device_id dw_dma_of_id_table[] = { 290 { .compatible = "snps,dma-spear1340", .data = &dw_dma_chip_pdata }, 291 {} 292 }; 293 MODULE_DEVICE_TABLE(of, dw_dma_of_id_table); 294 #endif 295 296 #ifdef CONFIG_ACPI 297 static const struct acpi_device_id dw_dma_acpi_id_table[] = { 298 { "INTL9C60", (kernel_ulong_t)&dw_dma_chip_pdata }, 299 { "80862286", (kernel_ulong_t)&dw_dma_chip_pdata }, 300 { "808622C0", (kernel_ulong_t)&dw_dma_chip_pdata }, 301 302 /* Elkhart Lake iDMA 32-bit (PSE DMA) */ 303 { "80864BB4", (kernel_ulong_t)&idma32_chip_pdata }, 304 { "80864BB5", (kernel_ulong_t)&idma32_chip_pdata }, 305 { "80864BB6", (kernel_ulong_t)&idma32_chip_pdata }, 306 307 { } 308 }; 309 MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table); 310 #endif 311 312 #ifdef CONFIG_PM_SLEEP 313 314 static int dw_suspend_late(struct device *dev) 315 { 316 struct dw_dma_chip_pdata *data = dev_get_drvdata(dev); 317 struct dw_dma_chip *chip = data->chip; 318 319 do_dw_dma_disable(chip); 320 clk_disable_unprepare(chip->clk); 321 322 return 0; 323 } 324 325 static int dw_resume_early(struct device *dev) 326 { 327 struct dw_dma_chip_pdata *data = dev_get_drvdata(dev); 328 struct dw_dma_chip *chip = data->chip; 329 int ret; 330 331 ret = clk_prepare_enable(chip->clk); 332 if (ret) 333 return ret; 334 335 return do_dw_dma_enable(chip); 336 } 337 338 #endif /* CONFIG_PM_SLEEP */ 339 340 static const struct dev_pm_ops dw_dev_pm_ops = { 341 SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early) 342 }; 343 344 static struct platform_driver dw_driver = { 345 .probe = dw_probe, 346 .remove = dw_remove, 347 .shutdown = dw_shutdown, 348 .driver = { 349 .name = DRV_NAME, 350 .pm = &dw_dev_pm_ops, 351 .of_match_table = of_match_ptr(dw_dma_of_id_table), 352 .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table), 353 }, 354 }; 355 356 static int __init dw_init(void) 357 { 358 return platform_driver_register(&dw_driver); 359 } 360 subsys_initcall(dw_init); 361 362 static void __exit dw_exit(void) 363 { 364 platform_driver_unregister(&dw_driver); 365 } 366 module_exit(dw_exit); 367 368 MODULE_LICENSE("GPL v2"); 369 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver"); 370 MODULE_ALIAS("platform:" DRV_NAME); 371