19cade1a4SAndy Shevchenko /* 29cade1a4SAndy Shevchenko * Platform driver for the Synopsys DesignWare DMA Controller 39cade1a4SAndy Shevchenko * 49cade1a4SAndy Shevchenko * Copyright (C) 2007-2008 Atmel Corporation 59cade1a4SAndy Shevchenko * Copyright (C) 2010-2011 ST Microelectronics 69cade1a4SAndy Shevchenko * Copyright (C) 2013 Intel Corporation 79cade1a4SAndy Shevchenko * 89cade1a4SAndy Shevchenko * Some parts of this driver are derived from the original dw_dmac. 99cade1a4SAndy Shevchenko * 109cade1a4SAndy Shevchenko * This program is free software; you can redistribute it and/or modify 119cade1a4SAndy Shevchenko * it under the terms of the GNU General Public License version 2 as 129cade1a4SAndy Shevchenko * published by the Free Software Foundation. 139cade1a4SAndy Shevchenko */ 149cade1a4SAndy Shevchenko 159cade1a4SAndy Shevchenko #include <linux/module.h> 169cade1a4SAndy Shevchenko #include <linux/device.h> 179cade1a4SAndy Shevchenko #include <linux/clk.h> 186acf3998SAndy Shevchenko #include <linux/pm_runtime.h> 199cade1a4SAndy Shevchenko #include <linux/platform_device.h> 209cade1a4SAndy Shevchenko #include <linux/dmaengine.h> 219cade1a4SAndy Shevchenko #include <linux/dma-mapping.h> 229cade1a4SAndy Shevchenko #include <linux/of.h> 239cade1a4SAndy Shevchenko #include <linux/of_dma.h> 249cade1a4SAndy Shevchenko #include <linux/acpi.h> 259cade1a4SAndy Shevchenko #include <linux/acpi_dma.h> 269cade1a4SAndy Shevchenko 279cade1a4SAndy Shevchenko #include "internal.h" 289cade1a4SAndy Shevchenko 29a104a45bSAndy Shevchenko #define DRV_NAME "dw_dmac" 30a104a45bSAndy Shevchenko 319cade1a4SAndy Shevchenko static struct dma_chan *dw_dma_of_xlate(struct of_phandle_args *dma_spec, 329cade1a4SAndy Shevchenko struct of_dma *ofdma) 339cade1a4SAndy Shevchenko { 349cade1a4SAndy Shevchenko struct dw_dma *dw = ofdma->of_dma_data; 354d130de2SAndy Shevchenko struct dw_dma_slave slave = { 364d130de2SAndy Shevchenko .dma_dev = dw->dma.dev, 379cade1a4SAndy Shevchenko }; 389cade1a4SAndy Shevchenko dma_cap_mask_t cap; 399cade1a4SAndy Shevchenko 409cade1a4SAndy Shevchenko if (dma_spec->args_count != 3) 419cade1a4SAndy Shevchenko return NULL; 429cade1a4SAndy Shevchenko 434d130de2SAndy Shevchenko slave.src_id = dma_spec->args[0]; 444d130de2SAndy Shevchenko slave.dst_id = dma_spec->args[0]; 45c422025cSAndy Shevchenko slave.m_master = dma_spec->args[1]; 46c422025cSAndy Shevchenko slave.p_master = dma_spec->args[2]; 479cade1a4SAndy Shevchenko 484d130de2SAndy Shevchenko if (WARN_ON(slave.src_id >= DW_DMA_MAX_NR_REQUESTS || 494d130de2SAndy Shevchenko slave.dst_id >= DW_DMA_MAX_NR_REQUESTS || 50161c3d04SAndy Shevchenko slave.m_master >= dw->pdata->nr_masters || 51161c3d04SAndy Shevchenko slave.p_master >= dw->pdata->nr_masters)) 529cade1a4SAndy Shevchenko return NULL; 539cade1a4SAndy Shevchenko 549cade1a4SAndy Shevchenko dma_cap_zero(cap); 559cade1a4SAndy Shevchenko dma_cap_set(DMA_SLAVE, cap); 569cade1a4SAndy Shevchenko 579cade1a4SAndy Shevchenko /* TODO: there should be a simpler way to do this */ 584d130de2SAndy Shevchenko return dma_request_channel(cap, dw_dma_filter, &slave); 599cade1a4SAndy Shevchenko } 609cade1a4SAndy Shevchenko 619cade1a4SAndy Shevchenko #ifdef CONFIG_ACPI 629cade1a4SAndy Shevchenko static bool dw_dma_acpi_filter(struct dma_chan *chan, void *param) 639cade1a4SAndy Shevchenko { 649cade1a4SAndy Shevchenko struct acpi_dma_spec *dma_spec = param; 654d130de2SAndy Shevchenko struct dw_dma_slave slave = { 664d130de2SAndy Shevchenko .dma_dev = dma_spec->dev, 674d130de2SAndy Shevchenko .src_id = dma_spec->slave_id, 684d130de2SAndy Shevchenko .dst_id = dma_spec->slave_id, 69c422025cSAndy Shevchenko .m_master = 0, 70c422025cSAndy Shevchenko .p_master = 1, 714d130de2SAndy Shevchenko }; 729cade1a4SAndy Shevchenko 734d130de2SAndy Shevchenko return dw_dma_filter(chan, &slave); 749cade1a4SAndy Shevchenko } 759cade1a4SAndy Shevchenko 769cade1a4SAndy Shevchenko static void dw_dma_acpi_controller_register(struct dw_dma *dw) 779cade1a4SAndy Shevchenko { 789cade1a4SAndy Shevchenko struct device *dev = dw->dma.dev; 799cade1a4SAndy Shevchenko struct acpi_dma_filter_info *info; 809cade1a4SAndy Shevchenko int ret; 819cade1a4SAndy Shevchenko 829cade1a4SAndy Shevchenko info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 839cade1a4SAndy Shevchenko if (!info) 849cade1a4SAndy Shevchenko return; 859cade1a4SAndy Shevchenko 869cade1a4SAndy Shevchenko dma_cap_zero(info->dma_cap); 879cade1a4SAndy Shevchenko dma_cap_set(DMA_SLAVE, info->dma_cap); 889cade1a4SAndy Shevchenko info->filter_fn = dw_dma_acpi_filter; 899cade1a4SAndy Shevchenko 909cade1a4SAndy Shevchenko ret = devm_acpi_dma_controller_register(dev, acpi_dma_simple_xlate, 919cade1a4SAndy Shevchenko info); 929cade1a4SAndy Shevchenko if (ret) 939cade1a4SAndy Shevchenko dev_err(dev, "could not register acpi_dma_controller\n"); 949cade1a4SAndy Shevchenko } 959cade1a4SAndy Shevchenko #else /* !CONFIG_ACPI */ 969cade1a4SAndy Shevchenko static inline void dw_dma_acpi_controller_register(struct dw_dma *dw) {} 979cade1a4SAndy Shevchenko #endif /* !CONFIG_ACPI */ 989cade1a4SAndy Shevchenko 999cade1a4SAndy Shevchenko #ifdef CONFIG_OF 1009cade1a4SAndy Shevchenko static struct dw_dma_platform_data * 1019cade1a4SAndy Shevchenko dw_dma_parse_dt(struct platform_device *pdev) 1029cade1a4SAndy Shevchenko { 1039cade1a4SAndy Shevchenko struct device_node *np = pdev->dev.of_node; 1049cade1a4SAndy Shevchenko struct dw_dma_platform_data *pdata; 105bd2c6636SEugeniy Paltsev u32 tmp, arr[DW_DMA_MAX_NR_MASTERS], mb[DW_DMA_MAX_NR_CHANNELS]; 106969f750fSAndy Shevchenko u32 nr_masters; 1072b574ba9SMans Rullgard u32 nr_channels; 1089cade1a4SAndy Shevchenko 1099cade1a4SAndy Shevchenko if (!np) { 1109cade1a4SAndy Shevchenko dev_err(&pdev->dev, "Missing DT data\n"); 1119cade1a4SAndy Shevchenko return NULL; 1129cade1a4SAndy Shevchenko } 1139cade1a4SAndy Shevchenko 114969f750fSAndy Shevchenko if (of_property_read_u32(np, "dma-masters", &nr_masters)) 115969f750fSAndy Shevchenko return NULL; 116969f750fSAndy Shevchenko if (nr_masters < 1 || nr_masters > DW_DMA_MAX_NR_MASTERS) 117969f750fSAndy Shevchenko return NULL; 118969f750fSAndy Shevchenko 1192b574ba9SMans Rullgard if (of_property_read_u32(np, "dma-channels", &nr_channels)) 1202b574ba9SMans Rullgard return NULL; 121bd2c6636SEugeniy Paltsev if (nr_channels > DW_DMA_MAX_NR_CHANNELS) 122bd2c6636SEugeniy Paltsev return NULL; 1232b574ba9SMans Rullgard 1249cade1a4SAndy Shevchenko pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 1259cade1a4SAndy Shevchenko if (!pdata) 1269cade1a4SAndy Shevchenko return NULL; 1279cade1a4SAndy Shevchenko 128969f750fSAndy Shevchenko pdata->nr_masters = nr_masters; 1292b574ba9SMans Rullgard pdata->nr_channels = nr_channels; 1309cade1a4SAndy Shevchenko 1319cade1a4SAndy Shevchenko if (!of_property_read_u32(np, "chan_allocation_order", &tmp)) 1329cade1a4SAndy Shevchenko pdata->chan_allocation_order = (unsigned char)tmp; 1339cade1a4SAndy Shevchenko 1349cade1a4SAndy Shevchenko if (!of_property_read_u32(np, "chan_priority", &tmp)) 1359cade1a4SAndy Shevchenko pdata->chan_priority = tmp; 1369cade1a4SAndy Shevchenko 1379cade1a4SAndy Shevchenko if (!of_property_read_u32(np, "block_size", &tmp)) 1389cade1a4SAndy Shevchenko pdata->block_size = tmp; 1399cade1a4SAndy Shevchenko 1402e65060eSAndy Shevchenko if (!of_property_read_u32_array(np, "data-width", arr, nr_masters)) { 141969f750fSAndy Shevchenko for (tmp = 0; tmp < nr_masters; tmp++) 1429cade1a4SAndy Shevchenko pdata->data_width[tmp] = arr[tmp]; 1432e65060eSAndy Shevchenko } else if (!of_property_read_u32_array(np, "data_width", arr, nr_masters)) { 1442e65060eSAndy Shevchenko for (tmp = 0; tmp < nr_masters; tmp++) 1452e65060eSAndy Shevchenko pdata->data_width[tmp] = BIT(arr[tmp] & 0x07); 146969f750fSAndy Shevchenko } 1479cade1a4SAndy Shevchenko 148bd2c6636SEugeniy Paltsev if (!of_property_read_u32_array(np, "multi-block", mb, nr_channels)) { 149bd2c6636SEugeniy Paltsev for (tmp = 0; tmp < nr_channels; tmp++) 150bd2c6636SEugeniy Paltsev pdata->multi_block[tmp] = mb[tmp]; 151bd2c6636SEugeniy Paltsev } else { 152bd2c6636SEugeniy Paltsev for (tmp = 0; tmp < nr_channels; tmp++) 153bd2c6636SEugeniy Paltsev pdata->multi_block[tmp] = 1; 154bd2c6636SEugeniy Paltsev } 155bd2c6636SEugeniy Paltsev 1567b0c03ecSChristian Lamparter if (!of_property_read_u32(np, "snps,dma-protection-control", &tmp)) { 1577b0c03ecSChristian Lamparter if (tmp > CHAN_PROTCTL_MASK) 1587b0c03ecSChristian Lamparter return NULL; 1597b0c03ecSChristian Lamparter pdata->protctl = tmp; 1607b0c03ecSChristian Lamparter } 1617b0c03ecSChristian Lamparter 1629cade1a4SAndy Shevchenko return pdata; 1639cade1a4SAndy Shevchenko } 1649cade1a4SAndy Shevchenko #else 1659cade1a4SAndy Shevchenko static inline struct dw_dma_platform_data * 1669cade1a4SAndy Shevchenko dw_dma_parse_dt(struct platform_device *pdev) 1679cade1a4SAndy Shevchenko { 1689cade1a4SAndy Shevchenko return NULL; 1699cade1a4SAndy Shevchenko } 1709cade1a4SAndy Shevchenko #endif 1719cade1a4SAndy Shevchenko 1729cade1a4SAndy Shevchenko static int dw_probe(struct platform_device *pdev) 1739cade1a4SAndy Shevchenko { 1749cade1a4SAndy Shevchenko struct dw_dma_chip *chip; 1759cade1a4SAndy Shevchenko struct device *dev = &pdev->dev; 1769cade1a4SAndy Shevchenko struct resource *mem; 1773a14c66dSAndy Shevchenko const struct dw_dma_platform_data *pdata; 1789cade1a4SAndy Shevchenko int err; 1799cade1a4SAndy Shevchenko 1809cade1a4SAndy Shevchenko chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 1819cade1a4SAndy Shevchenko if (!chip) 1829cade1a4SAndy Shevchenko return -ENOMEM; 1839cade1a4SAndy Shevchenko 1849cade1a4SAndy Shevchenko chip->irq = platform_get_irq(pdev, 0); 1859cade1a4SAndy Shevchenko if (chip->irq < 0) 1869cade1a4SAndy Shevchenko return chip->irq; 1879cade1a4SAndy Shevchenko 1889cade1a4SAndy Shevchenko mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1899cade1a4SAndy Shevchenko chip->regs = devm_ioremap_resource(dev, mem); 1909cade1a4SAndy Shevchenko if (IS_ERR(chip->regs)) 1919cade1a4SAndy Shevchenko return PTR_ERR(chip->regs); 1929cade1a4SAndy Shevchenko 19324353b8bSRussell King err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 19424353b8bSRussell King if (err) 19524353b8bSRussell King return err; 1969cade1a4SAndy Shevchenko 1979cade1a4SAndy Shevchenko pdata = dev_get_platdata(dev); 1989cade1a4SAndy Shevchenko if (!pdata) 1999cade1a4SAndy Shevchenko pdata = dw_dma_parse_dt(pdev); 2009cade1a4SAndy Shevchenko 2019cade1a4SAndy Shevchenko chip->dev = dev; 20208d62f58SAndy Shevchenko chip->id = pdev->id; 2033a14c66dSAndy Shevchenko chip->pdata = pdata; 2049cade1a4SAndy Shevchenko 205a15636e8SAndy Shevchenko chip->clk = devm_clk_get(chip->dev, "hclk"); 206a15636e8SAndy Shevchenko if (IS_ERR(chip->clk)) 207a15636e8SAndy Shevchenko return PTR_ERR(chip->clk); 208a15636e8SAndy Shevchenko err = clk_prepare_enable(chip->clk); 2099cade1a4SAndy Shevchenko if (err) 2109cade1a4SAndy Shevchenko return err; 2119cade1a4SAndy Shevchenko 2126acf3998SAndy Shevchenko pm_runtime_enable(&pdev->dev); 2136acf3998SAndy Shevchenko 2143a14c66dSAndy Shevchenko err = dw_dma_probe(chip); 215a15636e8SAndy Shevchenko if (err) 216a15636e8SAndy Shevchenko goto err_dw_dma_probe; 217a15636e8SAndy Shevchenko 2189cade1a4SAndy Shevchenko platform_set_drvdata(pdev, chip); 2199cade1a4SAndy Shevchenko 2209cade1a4SAndy Shevchenko if (pdev->dev.of_node) { 2219cade1a4SAndy Shevchenko err = of_dma_controller_register(pdev->dev.of_node, 2229cade1a4SAndy Shevchenko dw_dma_of_xlate, chip->dw); 2239cade1a4SAndy Shevchenko if (err) 2249cade1a4SAndy Shevchenko dev_err(&pdev->dev, 2259cade1a4SAndy Shevchenko "could not register of_dma_controller\n"); 2269cade1a4SAndy Shevchenko } 2279cade1a4SAndy Shevchenko 2289cade1a4SAndy Shevchenko if (ACPI_HANDLE(&pdev->dev)) 2299cade1a4SAndy Shevchenko dw_dma_acpi_controller_register(chip->dw); 2309cade1a4SAndy Shevchenko 2319cade1a4SAndy Shevchenko return 0; 232a15636e8SAndy Shevchenko 233a15636e8SAndy Shevchenko err_dw_dma_probe: 2346acf3998SAndy Shevchenko pm_runtime_disable(&pdev->dev); 235a15636e8SAndy Shevchenko clk_disable_unprepare(chip->clk); 236a15636e8SAndy Shevchenko return err; 2379cade1a4SAndy Shevchenko } 2389cade1a4SAndy Shevchenko 2399cade1a4SAndy Shevchenko static int dw_remove(struct platform_device *pdev) 2409cade1a4SAndy Shevchenko { 2419cade1a4SAndy Shevchenko struct dw_dma_chip *chip = platform_get_drvdata(pdev); 2429cade1a4SAndy Shevchenko 2439cade1a4SAndy Shevchenko if (pdev->dev.of_node) 2449cade1a4SAndy Shevchenko of_dma_controller_free(pdev->dev.of_node); 2459cade1a4SAndy Shevchenko 246a15636e8SAndy Shevchenko dw_dma_remove(chip); 2476acf3998SAndy Shevchenko pm_runtime_disable(&pdev->dev); 248a15636e8SAndy Shevchenko clk_disable_unprepare(chip->clk); 249a15636e8SAndy Shevchenko 250a15636e8SAndy Shevchenko return 0; 2519cade1a4SAndy Shevchenko } 2529cade1a4SAndy Shevchenko 2539cade1a4SAndy Shevchenko static void dw_shutdown(struct platform_device *pdev) 2549cade1a4SAndy Shevchenko { 2559cade1a4SAndy Shevchenko struct dw_dma_chip *chip = platform_get_drvdata(pdev); 2569cade1a4SAndy Shevchenko 25732146588SAndy Shevchenko /* 258*69da8be9SAndy Shevchenko * We have to call do_dw_dma_disable() to stop any ongoing transfer. On 25932146588SAndy Shevchenko * some platforms we can't do that since DMA device is powered off. 26032146588SAndy Shevchenko * Moreover we have no possibility to check if the platform is affected 26132146588SAndy Shevchenko * or not. That's why we call pm_runtime_get_sync() / pm_runtime_put() 26232146588SAndy Shevchenko * unconditionally. On the other hand we can't use 26332146588SAndy Shevchenko * pm_runtime_suspended() because runtime PM framework is not fully 26432146588SAndy Shevchenko * used by the driver. 26532146588SAndy Shevchenko */ 26632146588SAndy Shevchenko pm_runtime_get_sync(chip->dev); 267*69da8be9SAndy Shevchenko do_dw_dma_disable(chip); 26832146588SAndy Shevchenko pm_runtime_put_sync_suspend(chip->dev); 26932146588SAndy Shevchenko 270a15636e8SAndy Shevchenko clk_disable_unprepare(chip->clk); 2719cade1a4SAndy Shevchenko } 2729cade1a4SAndy Shevchenko 2739cade1a4SAndy Shevchenko #ifdef CONFIG_OF 2749cade1a4SAndy Shevchenko static const struct of_device_id dw_dma_of_id_table[] = { 2759cade1a4SAndy Shevchenko { .compatible = "snps,dma-spear1340" }, 2769cade1a4SAndy Shevchenko {} 2779cade1a4SAndy Shevchenko }; 2789cade1a4SAndy Shevchenko MODULE_DEVICE_TABLE(of, dw_dma_of_id_table); 2799cade1a4SAndy Shevchenko #endif 2809cade1a4SAndy Shevchenko 2819cade1a4SAndy Shevchenko #ifdef CONFIG_ACPI 2829cade1a4SAndy Shevchenko static const struct acpi_device_id dw_dma_acpi_id_table[] = { 283bc0bb1fdSAndy Shevchenko { "INTL9C60", 0 }, 2845658f4f9SHans de Goede { "80862286", 0 }, 2855658f4f9SHans de Goede { "808622C0", 0 }, 2869cade1a4SAndy Shevchenko { } 2879cade1a4SAndy Shevchenko }; 288be480dcbSAndy Shevchenko MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table); 2899cade1a4SAndy Shevchenko #endif 2909cade1a4SAndy Shevchenko 2919cade1a4SAndy Shevchenko #ifdef CONFIG_PM_SLEEP 2929cade1a4SAndy Shevchenko 293067bd4fdSAndy Shevchenko static int dw_suspend_late(struct device *dev) 2949cade1a4SAndy Shevchenko { 29583ff1323SWolfram Sang struct dw_dma_chip *chip = dev_get_drvdata(dev); 2969cade1a4SAndy Shevchenko 297*69da8be9SAndy Shevchenko do_dw_dma_disable(chip); 298a15636e8SAndy Shevchenko clk_disable_unprepare(chip->clk); 299a15636e8SAndy Shevchenko 300a15636e8SAndy Shevchenko return 0; 3019cade1a4SAndy Shevchenko } 3029cade1a4SAndy Shevchenko 303067bd4fdSAndy Shevchenko static int dw_resume_early(struct device *dev) 3049cade1a4SAndy Shevchenko { 30583ff1323SWolfram Sang struct dw_dma_chip *chip = dev_get_drvdata(dev); 306702fce05SArvind Yadav int ret; 3079cade1a4SAndy Shevchenko 308702fce05SArvind Yadav ret = clk_prepare_enable(chip->clk); 309702fce05SArvind Yadav if (ret) 310702fce05SArvind Yadav return ret; 311702fce05SArvind Yadav 312*69da8be9SAndy Shevchenko return do_dw_dma_enable(chip); 3139cade1a4SAndy Shevchenko } 3149cade1a4SAndy Shevchenko 315067bd4fdSAndy Shevchenko #endif /* CONFIG_PM_SLEEP */ 3169cade1a4SAndy Shevchenko 3179cade1a4SAndy Shevchenko static const struct dev_pm_ops dw_dev_pm_ops = { 318067bd4fdSAndy Shevchenko SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early) 3199cade1a4SAndy Shevchenko }; 3209cade1a4SAndy Shevchenko 3219cade1a4SAndy Shevchenko static struct platform_driver dw_driver = { 3229cade1a4SAndy Shevchenko .probe = dw_probe, 3239cade1a4SAndy Shevchenko .remove = dw_remove, 3249cade1a4SAndy Shevchenko .shutdown = dw_shutdown, 3259cade1a4SAndy Shevchenko .driver = { 326a104a45bSAndy Shevchenko .name = DRV_NAME, 3279cade1a4SAndy Shevchenko .pm = &dw_dev_pm_ops, 3289cade1a4SAndy Shevchenko .of_match_table = of_match_ptr(dw_dma_of_id_table), 3299cade1a4SAndy Shevchenko .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table), 3309cade1a4SAndy Shevchenko }, 3319cade1a4SAndy Shevchenko }; 3329cade1a4SAndy Shevchenko 3339cade1a4SAndy Shevchenko static int __init dw_init(void) 3349cade1a4SAndy Shevchenko { 3359cade1a4SAndy Shevchenko return platform_driver_register(&dw_driver); 3369cade1a4SAndy Shevchenko } 3379cade1a4SAndy Shevchenko subsys_initcall(dw_init); 3389cade1a4SAndy Shevchenko 3399cade1a4SAndy Shevchenko static void __exit dw_exit(void) 3409cade1a4SAndy Shevchenko { 3419cade1a4SAndy Shevchenko platform_driver_unregister(&dw_driver); 3429cade1a4SAndy Shevchenko } 3439cade1a4SAndy Shevchenko module_exit(dw_exit); 3449cade1a4SAndy Shevchenko 3459cade1a4SAndy Shevchenko MODULE_LICENSE("GPL v2"); 3469cade1a4SAndy Shevchenko MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver"); 347a104a45bSAndy Shevchenko MODULE_ALIAS("platform:" DRV_NAME); 348