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> 189cade1a4SAndy Shevchenko #include <linux/platform_device.h> 199cade1a4SAndy Shevchenko #include <linux/dmaengine.h> 209cade1a4SAndy Shevchenko #include <linux/dma-mapping.h> 219cade1a4SAndy Shevchenko #include <linux/of.h> 229cade1a4SAndy Shevchenko #include <linux/of_dma.h> 239cade1a4SAndy Shevchenko #include <linux/acpi.h> 249cade1a4SAndy Shevchenko #include <linux/acpi_dma.h> 259cade1a4SAndy Shevchenko 269cade1a4SAndy Shevchenko #include "internal.h" 279cade1a4SAndy Shevchenko 289cade1a4SAndy Shevchenko struct dw_dma_of_filter_args { 299cade1a4SAndy Shevchenko struct dw_dma *dw; 309cade1a4SAndy Shevchenko unsigned int req; 319cade1a4SAndy Shevchenko unsigned int src; 329cade1a4SAndy Shevchenko unsigned int dst; 339cade1a4SAndy Shevchenko }; 349cade1a4SAndy Shevchenko 359cade1a4SAndy Shevchenko static bool dw_dma_of_filter(struct dma_chan *chan, void *param) 369cade1a4SAndy Shevchenko { 379cade1a4SAndy Shevchenko struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 389cade1a4SAndy Shevchenko struct dw_dma_of_filter_args *fargs = param; 399cade1a4SAndy Shevchenko 409cade1a4SAndy Shevchenko /* Ensure the device matches our channel */ 419cade1a4SAndy Shevchenko if (chan->device != &fargs->dw->dma) 429cade1a4SAndy Shevchenko return false; 439cade1a4SAndy Shevchenko 449cade1a4SAndy Shevchenko dwc->request_line = fargs->req; 459cade1a4SAndy Shevchenko dwc->src_master = fargs->src; 469cade1a4SAndy Shevchenko dwc->dst_master = fargs->dst; 479cade1a4SAndy Shevchenko 489cade1a4SAndy Shevchenko return true; 499cade1a4SAndy Shevchenko } 509cade1a4SAndy Shevchenko 519cade1a4SAndy Shevchenko static struct dma_chan *dw_dma_of_xlate(struct of_phandle_args *dma_spec, 529cade1a4SAndy Shevchenko struct of_dma *ofdma) 539cade1a4SAndy Shevchenko { 549cade1a4SAndy Shevchenko struct dw_dma *dw = ofdma->of_dma_data; 559cade1a4SAndy Shevchenko struct dw_dma_of_filter_args fargs = { 569cade1a4SAndy Shevchenko .dw = dw, 579cade1a4SAndy Shevchenko }; 589cade1a4SAndy Shevchenko dma_cap_mask_t cap; 599cade1a4SAndy Shevchenko 609cade1a4SAndy Shevchenko if (dma_spec->args_count != 3) 619cade1a4SAndy Shevchenko return NULL; 629cade1a4SAndy Shevchenko 639cade1a4SAndy Shevchenko fargs.req = dma_spec->args[0]; 649cade1a4SAndy Shevchenko fargs.src = dma_spec->args[1]; 659cade1a4SAndy Shevchenko fargs.dst = dma_spec->args[2]; 669cade1a4SAndy Shevchenko 679cade1a4SAndy Shevchenko if (WARN_ON(fargs.req >= DW_DMA_MAX_NR_REQUESTS || 689cade1a4SAndy Shevchenko fargs.src >= dw->nr_masters || 699cade1a4SAndy Shevchenko fargs.dst >= dw->nr_masters)) 709cade1a4SAndy Shevchenko return NULL; 719cade1a4SAndy Shevchenko 729cade1a4SAndy Shevchenko dma_cap_zero(cap); 739cade1a4SAndy Shevchenko dma_cap_set(DMA_SLAVE, cap); 749cade1a4SAndy Shevchenko 759cade1a4SAndy Shevchenko /* TODO: there should be a simpler way to do this */ 769cade1a4SAndy Shevchenko return dma_request_channel(cap, dw_dma_of_filter, &fargs); 779cade1a4SAndy Shevchenko } 789cade1a4SAndy Shevchenko 799cade1a4SAndy Shevchenko #ifdef CONFIG_ACPI 809cade1a4SAndy Shevchenko static bool dw_dma_acpi_filter(struct dma_chan *chan, void *param) 819cade1a4SAndy Shevchenko { 829cade1a4SAndy Shevchenko struct dw_dma_chan *dwc = to_dw_dma_chan(chan); 839cade1a4SAndy Shevchenko struct acpi_dma_spec *dma_spec = param; 849cade1a4SAndy Shevchenko 859cade1a4SAndy Shevchenko if (chan->device->dev != dma_spec->dev || 869cade1a4SAndy Shevchenko chan->chan_id != dma_spec->chan_id) 879cade1a4SAndy Shevchenko return false; 889cade1a4SAndy Shevchenko 899cade1a4SAndy Shevchenko dwc->request_line = dma_spec->slave_id; 909cade1a4SAndy Shevchenko dwc->src_master = dwc_get_sms(NULL); 919cade1a4SAndy Shevchenko dwc->dst_master = dwc_get_dms(NULL); 929cade1a4SAndy Shevchenko 939cade1a4SAndy Shevchenko return true; 949cade1a4SAndy Shevchenko } 959cade1a4SAndy Shevchenko 969cade1a4SAndy Shevchenko static void dw_dma_acpi_controller_register(struct dw_dma *dw) 979cade1a4SAndy Shevchenko { 989cade1a4SAndy Shevchenko struct device *dev = dw->dma.dev; 999cade1a4SAndy Shevchenko struct acpi_dma_filter_info *info; 1009cade1a4SAndy Shevchenko int ret; 1019cade1a4SAndy Shevchenko 1029cade1a4SAndy Shevchenko info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 1039cade1a4SAndy Shevchenko if (!info) 1049cade1a4SAndy Shevchenko return; 1059cade1a4SAndy Shevchenko 1069cade1a4SAndy Shevchenko dma_cap_zero(info->dma_cap); 1079cade1a4SAndy Shevchenko dma_cap_set(DMA_SLAVE, info->dma_cap); 1089cade1a4SAndy Shevchenko info->filter_fn = dw_dma_acpi_filter; 1099cade1a4SAndy Shevchenko 1109cade1a4SAndy Shevchenko ret = devm_acpi_dma_controller_register(dev, acpi_dma_simple_xlate, 1119cade1a4SAndy Shevchenko info); 1129cade1a4SAndy Shevchenko if (ret) 1139cade1a4SAndy Shevchenko dev_err(dev, "could not register acpi_dma_controller\n"); 1149cade1a4SAndy Shevchenko } 1159cade1a4SAndy Shevchenko #else /* !CONFIG_ACPI */ 1169cade1a4SAndy Shevchenko static inline void dw_dma_acpi_controller_register(struct dw_dma *dw) {} 1179cade1a4SAndy Shevchenko #endif /* !CONFIG_ACPI */ 1189cade1a4SAndy Shevchenko 1199cade1a4SAndy Shevchenko #ifdef CONFIG_OF 1209cade1a4SAndy Shevchenko static struct dw_dma_platform_data * 1219cade1a4SAndy Shevchenko dw_dma_parse_dt(struct platform_device *pdev) 1229cade1a4SAndy Shevchenko { 1239cade1a4SAndy Shevchenko struct device_node *np = pdev->dev.of_node; 1249cade1a4SAndy Shevchenko struct dw_dma_platform_data *pdata; 1259cade1a4SAndy Shevchenko u32 tmp, arr[4]; 1269cade1a4SAndy Shevchenko 1279cade1a4SAndy Shevchenko if (!np) { 1289cade1a4SAndy Shevchenko dev_err(&pdev->dev, "Missing DT data\n"); 1299cade1a4SAndy Shevchenko return NULL; 1309cade1a4SAndy Shevchenko } 1319cade1a4SAndy Shevchenko 1329cade1a4SAndy Shevchenko pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 1339cade1a4SAndy Shevchenko if (!pdata) 1349cade1a4SAndy Shevchenko return NULL; 1359cade1a4SAndy Shevchenko 1369cade1a4SAndy Shevchenko if (of_property_read_u32(np, "dma-channels", &pdata->nr_channels)) 1379cade1a4SAndy Shevchenko return NULL; 1389cade1a4SAndy Shevchenko 1399cade1a4SAndy Shevchenko if (of_property_read_bool(np, "is_private")) 1409cade1a4SAndy Shevchenko pdata->is_private = true; 1419cade1a4SAndy Shevchenko 1429cade1a4SAndy Shevchenko if (!of_property_read_u32(np, "chan_allocation_order", &tmp)) 1439cade1a4SAndy Shevchenko pdata->chan_allocation_order = (unsigned char)tmp; 1449cade1a4SAndy Shevchenko 1459cade1a4SAndy Shevchenko if (!of_property_read_u32(np, "chan_priority", &tmp)) 1469cade1a4SAndy Shevchenko pdata->chan_priority = tmp; 1479cade1a4SAndy Shevchenko 1489cade1a4SAndy Shevchenko if (!of_property_read_u32(np, "block_size", &tmp)) 1499cade1a4SAndy Shevchenko pdata->block_size = tmp; 1509cade1a4SAndy Shevchenko 1519cade1a4SAndy Shevchenko if (!of_property_read_u32(np, "dma-masters", &tmp)) { 1529cade1a4SAndy Shevchenko if (tmp > 4) 1539cade1a4SAndy Shevchenko return NULL; 1549cade1a4SAndy Shevchenko 1559cade1a4SAndy Shevchenko pdata->nr_masters = tmp; 1569cade1a4SAndy Shevchenko } 1579cade1a4SAndy Shevchenko 1589cade1a4SAndy Shevchenko if (!of_property_read_u32_array(np, "data_width", arr, 1599cade1a4SAndy Shevchenko pdata->nr_masters)) 1609cade1a4SAndy Shevchenko for (tmp = 0; tmp < pdata->nr_masters; tmp++) 1619cade1a4SAndy Shevchenko pdata->data_width[tmp] = arr[tmp]; 1629cade1a4SAndy Shevchenko 1639cade1a4SAndy Shevchenko return pdata; 1649cade1a4SAndy Shevchenko } 1659cade1a4SAndy Shevchenko #else 1669cade1a4SAndy Shevchenko static inline struct dw_dma_platform_data * 1679cade1a4SAndy Shevchenko dw_dma_parse_dt(struct platform_device *pdev) 1689cade1a4SAndy Shevchenko { 1699cade1a4SAndy Shevchenko return NULL; 1709cade1a4SAndy Shevchenko } 1719cade1a4SAndy Shevchenko #endif 1729cade1a4SAndy Shevchenko 1739cade1a4SAndy Shevchenko static int dw_probe(struct platform_device *pdev) 1749cade1a4SAndy Shevchenko { 1759cade1a4SAndy Shevchenko struct dw_dma_chip *chip; 1769cade1a4SAndy Shevchenko struct device *dev = &pdev->dev; 1779cade1a4SAndy Shevchenko struct resource *mem; 1789cade1a4SAndy Shevchenko struct dw_dma_platform_data *pdata; 1799cade1a4SAndy Shevchenko int err; 1809cade1a4SAndy Shevchenko 1819cade1a4SAndy Shevchenko chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 1829cade1a4SAndy Shevchenko if (!chip) 1839cade1a4SAndy Shevchenko return -ENOMEM; 1849cade1a4SAndy Shevchenko 1859cade1a4SAndy Shevchenko chip->irq = platform_get_irq(pdev, 0); 1869cade1a4SAndy Shevchenko if (chip->irq < 0) 1879cade1a4SAndy Shevchenko return chip->irq; 1889cade1a4SAndy Shevchenko 1899cade1a4SAndy Shevchenko mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1909cade1a4SAndy Shevchenko chip->regs = devm_ioremap_resource(dev, mem); 1919cade1a4SAndy Shevchenko if (IS_ERR(chip->regs)) 1929cade1a4SAndy Shevchenko return PTR_ERR(chip->regs); 1939cade1a4SAndy Shevchenko 19424353b8bSRussell King err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 19524353b8bSRussell King if (err) 19624353b8bSRussell King return err; 1979cade1a4SAndy Shevchenko 1989cade1a4SAndy Shevchenko pdata = dev_get_platdata(dev); 1999cade1a4SAndy Shevchenko if (!pdata) 2009cade1a4SAndy Shevchenko pdata = dw_dma_parse_dt(pdev); 2019cade1a4SAndy Shevchenko 2029cade1a4SAndy Shevchenko chip->dev = dev; 2039cade1a4SAndy Shevchenko 2049cade1a4SAndy Shevchenko err = dw_dma_probe(chip, pdata); 2059cade1a4SAndy Shevchenko if (err) 2069cade1a4SAndy Shevchenko return err; 2079cade1a4SAndy Shevchenko 2089cade1a4SAndy Shevchenko platform_set_drvdata(pdev, chip); 2099cade1a4SAndy Shevchenko 2109cade1a4SAndy Shevchenko if (pdev->dev.of_node) { 2119cade1a4SAndy Shevchenko err = of_dma_controller_register(pdev->dev.of_node, 2129cade1a4SAndy Shevchenko dw_dma_of_xlate, chip->dw); 2139cade1a4SAndy Shevchenko if (err) 2149cade1a4SAndy Shevchenko dev_err(&pdev->dev, 2159cade1a4SAndy Shevchenko "could not register of_dma_controller\n"); 2169cade1a4SAndy Shevchenko } 2179cade1a4SAndy Shevchenko 2189cade1a4SAndy Shevchenko if (ACPI_HANDLE(&pdev->dev)) 2199cade1a4SAndy Shevchenko dw_dma_acpi_controller_register(chip->dw); 2209cade1a4SAndy Shevchenko 2219cade1a4SAndy Shevchenko return 0; 2229cade1a4SAndy Shevchenko } 2239cade1a4SAndy Shevchenko 2249cade1a4SAndy Shevchenko static int dw_remove(struct platform_device *pdev) 2259cade1a4SAndy Shevchenko { 2269cade1a4SAndy Shevchenko struct dw_dma_chip *chip = platform_get_drvdata(pdev); 2279cade1a4SAndy Shevchenko 2289cade1a4SAndy Shevchenko if (pdev->dev.of_node) 2299cade1a4SAndy Shevchenko of_dma_controller_free(pdev->dev.of_node); 2309cade1a4SAndy Shevchenko 2319cade1a4SAndy Shevchenko return dw_dma_remove(chip); 2329cade1a4SAndy Shevchenko } 2339cade1a4SAndy Shevchenko 2349cade1a4SAndy Shevchenko static void dw_shutdown(struct platform_device *pdev) 2359cade1a4SAndy Shevchenko { 2369cade1a4SAndy Shevchenko struct dw_dma_chip *chip = platform_get_drvdata(pdev); 2379cade1a4SAndy Shevchenko 2389cade1a4SAndy Shevchenko dw_dma_shutdown(chip); 2399cade1a4SAndy Shevchenko } 2409cade1a4SAndy Shevchenko 2419cade1a4SAndy Shevchenko #ifdef CONFIG_OF 2429cade1a4SAndy Shevchenko static const struct of_device_id dw_dma_of_id_table[] = { 2439cade1a4SAndy Shevchenko { .compatible = "snps,dma-spear1340" }, 2449cade1a4SAndy Shevchenko {} 2459cade1a4SAndy Shevchenko }; 2469cade1a4SAndy Shevchenko MODULE_DEVICE_TABLE(of, dw_dma_of_id_table); 2479cade1a4SAndy Shevchenko #endif 2489cade1a4SAndy Shevchenko 2499cade1a4SAndy Shevchenko #ifdef CONFIG_ACPI 2509cade1a4SAndy Shevchenko static const struct acpi_device_id dw_dma_acpi_id_table[] = { 2519cade1a4SAndy Shevchenko { "INTL9C60", 0 }, 2529cade1a4SAndy Shevchenko { } 2539cade1a4SAndy Shevchenko }; 254be480dcbSAndy Shevchenko MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table); 2559cade1a4SAndy Shevchenko #endif 2569cade1a4SAndy Shevchenko 2579cade1a4SAndy Shevchenko #ifdef CONFIG_PM_SLEEP 2589cade1a4SAndy Shevchenko 259*067bd4fdSAndy Shevchenko static int dw_suspend_late(struct device *dev) 2609cade1a4SAndy Shevchenko { 2619cade1a4SAndy Shevchenko struct platform_device *pdev = to_platform_device(dev); 2629cade1a4SAndy Shevchenko struct dw_dma_chip *chip = platform_get_drvdata(pdev); 2639cade1a4SAndy Shevchenko 2649cade1a4SAndy Shevchenko return dw_dma_suspend(chip); 2659cade1a4SAndy Shevchenko } 2669cade1a4SAndy Shevchenko 267*067bd4fdSAndy Shevchenko static int dw_resume_early(struct device *dev) 2689cade1a4SAndy Shevchenko { 2699cade1a4SAndy Shevchenko struct platform_device *pdev = to_platform_device(dev); 2709cade1a4SAndy Shevchenko struct dw_dma_chip *chip = platform_get_drvdata(pdev); 2719cade1a4SAndy Shevchenko 2729cade1a4SAndy Shevchenko return dw_dma_resume(chip); 2739cade1a4SAndy Shevchenko } 2749cade1a4SAndy Shevchenko 275*067bd4fdSAndy Shevchenko #endif /* CONFIG_PM_SLEEP */ 2769cade1a4SAndy Shevchenko 2779cade1a4SAndy Shevchenko static const struct dev_pm_ops dw_dev_pm_ops = { 278*067bd4fdSAndy Shevchenko SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early) 2799cade1a4SAndy Shevchenko }; 2809cade1a4SAndy Shevchenko 2819cade1a4SAndy Shevchenko static struct platform_driver dw_driver = { 2829cade1a4SAndy Shevchenko .probe = dw_probe, 2839cade1a4SAndy Shevchenko .remove = dw_remove, 2849cade1a4SAndy Shevchenko .shutdown = dw_shutdown, 2859cade1a4SAndy Shevchenko .driver = { 2869cade1a4SAndy Shevchenko .name = "dw_dmac", 2879cade1a4SAndy Shevchenko .pm = &dw_dev_pm_ops, 2889cade1a4SAndy Shevchenko .of_match_table = of_match_ptr(dw_dma_of_id_table), 2899cade1a4SAndy Shevchenko .acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table), 2909cade1a4SAndy Shevchenko }, 2919cade1a4SAndy Shevchenko }; 2929cade1a4SAndy Shevchenko 2939cade1a4SAndy Shevchenko static int __init dw_init(void) 2949cade1a4SAndy Shevchenko { 2959cade1a4SAndy Shevchenko return platform_driver_register(&dw_driver); 2969cade1a4SAndy Shevchenko } 2979cade1a4SAndy Shevchenko subsys_initcall(dw_init); 2989cade1a4SAndy Shevchenko 2999cade1a4SAndy Shevchenko static void __exit dw_exit(void) 3009cade1a4SAndy Shevchenko { 3019cade1a4SAndy Shevchenko platform_driver_unregister(&dw_driver); 3029cade1a4SAndy Shevchenko } 3039cade1a4SAndy Shevchenko module_exit(dw_exit); 3049cade1a4SAndy Shevchenko 3059cade1a4SAndy Shevchenko MODULE_LICENSE("GPL v2"); 3069cade1a4SAndy Shevchenko MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver"); 307