xref: /linux/drivers/dma/dw/platform.c (revision 9cade1a46c77dfc96d57a3ea6354e95b2a7fcf61)
1*9cade1a4SAndy Shevchenko /*
2*9cade1a4SAndy Shevchenko  * Platform driver for the Synopsys DesignWare DMA Controller
3*9cade1a4SAndy Shevchenko  *
4*9cade1a4SAndy Shevchenko  * Copyright (C) 2007-2008 Atmel Corporation
5*9cade1a4SAndy Shevchenko  * Copyright (C) 2010-2011 ST Microelectronics
6*9cade1a4SAndy Shevchenko  * Copyright (C) 2013 Intel Corporation
7*9cade1a4SAndy Shevchenko  *
8*9cade1a4SAndy Shevchenko  * Some parts of this driver are derived from the original dw_dmac.
9*9cade1a4SAndy Shevchenko  *
10*9cade1a4SAndy Shevchenko  * This program is free software; you can redistribute it and/or modify
11*9cade1a4SAndy Shevchenko  * it under the terms of the GNU General Public License version 2 as
12*9cade1a4SAndy Shevchenko  * published by the Free Software Foundation.
13*9cade1a4SAndy Shevchenko  */
14*9cade1a4SAndy Shevchenko 
15*9cade1a4SAndy Shevchenko #include <linux/module.h>
16*9cade1a4SAndy Shevchenko #include <linux/device.h>
17*9cade1a4SAndy Shevchenko #include <linux/clk.h>
18*9cade1a4SAndy Shevchenko #include <linux/platform_device.h>
19*9cade1a4SAndy Shevchenko #include <linux/dmaengine.h>
20*9cade1a4SAndy Shevchenko #include <linux/dma-mapping.h>
21*9cade1a4SAndy Shevchenko #include <linux/of.h>
22*9cade1a4SAndy Shevchenko #include <linux/of_dma.h>
23*9cade1a4SAndy Shevchenko #include <linux/acpi.h>
24*9cade1a4SAndy Shevchenko #include <linux/acpi_dma.h>
25*9cade1a4SAndy Shevchenko 
26*9cade1a4SAndy Shevchenko #include "internal.h"
27*9cade1a4SAndy Shevchenko 
28*9cade1a4SAndy Shevchenko struct dw_dma_of_filter_args {
29*9cade1a4SAndy Shevchenko 	struct dw_dma *dw;
30*9cade1a4SAndy Shevchenko 	unsigned int req;
31*9cade1a4SAndy Shevchenko 	unsigned int src;
32*9cade1a4SAndy Shevchenko 	unsigned int dst;
33*9cade1a4SAndy Shevchenko };
34*9cade1a4SAndy Shevchenko 
35*9cade1a4SAndy Shevchenko static bool dw_dma_of_filter(struct dma_chan *chan, void *param)
36*9cade1a4SAndy Shevchenko {
37*9cade1a4SAndy Shevchenko 	struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
38*9cade1a4SAndy Shevchenko 	struct dw_dma_of_filter_args *fargs = param;
39*9cade1a4SAndy Shevchenko 
40*9cade1a4SAndy Shevchenko 	/* Ensure the device matches our channel */
41*9cade1a4SAndy Shevchenko 	if (chan->device != &fargs->dw->dma)
42*9cade1a4SAndy Shevchenko 		return false;
43*9cade1a4SAndy Shevchenko 
44*9cade1a4SAndy Shevchenko 	dwc->request_line = fargs->req;
45*9cade1a4SAndy Shevchenko 	dwc->src_master	= fargs->src;
46*9cade1a4SAndy Shevchenko 	dwc->dst_master	= fargs->dst;
47*9cade1a4SAndy Shevchenko 
48*9cade1a4SAndy Shevchenko 	return true;
49*9cade1a4SAndy Shevchenko }
50*9cade1a4SAndy Shevchenko 
51*9cade1a4SAndy Shevchenko static struct dma_chan *dw_dma_of_xlate(struct of_phandle_args *dma_spec,
52*9cade1a4SAndy Shevchenko 					struct of_dma *ofdma)
53*9cade1a4SAndy Shevchenko {
54*9cade1a4SAndy Shevchenko 	struct dw_dma *dw = ofdma->of_dma_data;
55*9cade1a4SAndy Shevchenko 	struct dw_dma_of_filter_args fargs = {
56*9cade1a4SAndy Shevchenko 		.dw = dw,
57*9cade1a4SAndy Shevchenko 	};
58*9cade1a4SAndy Shevchenko 	dma_cap_mask_t cap;
59*9cade1a4SAndy Shevchenko 
60*9cade1a4SAndy Shevchenko 	if (dma_spec->args_count != 3)
61*9cade1a4SAndy Shevchenko 		return NULL;
62*9cade1a4SAndy Shevchenko 
63*9cade1a4SAndy Shevchenko 	fargs.req = dma_spec->args[0];
64*9cade1a4SAndy Shevchenko 	fargs.src = dma_spec->args[1];
65*9cade1a4SAndy Shevchenko 	fargs.dst = dma_spec->args[2];
66*9cade1a4SAndy Shevchenko 
67*9cade1a4SAndy Shevchenko 	if (WARN_ON(fargs.req >= DW_DMA_MAX_NR_REQUESTS ||
68*9cade1a4SAndy Shevchenko 		    fargs.src >= dw->nr_masters ||
69*9cade1a4SAndy Shevchenko 		    fargs.dst >= dw->nr_masters))
70*9cade1a4SAndy Shevchenko 		return NULL;
71*9cade1a4SAndy Shevchenko 
72*9cade1a4SAndy Shevchenko 	dma_cap_zero(cap);
73*9cade1a4SAndy Shevchenko 	dma_cap_set(DMA_SLAVE, cap);
74*9cade1a4SAndy Shevchenko 
75*9cade1a4SAndy Shevchenko 	/* TODO: there should be a simpler way to do this */
76*9cade1a4SAndy Shevchenko 	return dma_request_channel(cap, dw_dma_of_filter, &fargs);
77*9cade1a4SAndy Shevchenko }
78*9cade1a4SAndy Shevchenko 
79*9cade1a4SAndy Shevchenko #ifdef CONFIG_ACPI
80*9cade1a4SAndy Shevchenko static bool dw_dma_acpi_filter(struct dma_chan *chan, void *param)
81*9cade1a4SAndy Shevchenko {
82*9cade1a4SAndy Shevchenko 	struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
83*9cade1a4SAndy Shevchenko 	struct acpi_dma_spec *dma_spec = param;
84*9cade1a4SAndy Shevchenko 
85*9cade1a4SAndy Shevchenko 	if (chan->device->dev != dma_spec->dev ||
86*9cade1a4SAndy Shevchenko 	    chan->chan_id != dma_spec->chan_id)
87*9cade1a4SAndy Shevchenko 		return false;
88*9cade1a4SAndy Shevchenko 
89*9cade1a4SAndy Shevchenko 	dwc->request_line = dma_spec->slave_id;
90*9cade1a4SAndy Shevchenko 	dwc->src_master = dwc_get_sms(NULL);
91*9cade1a4SAndy Shevchenko 	dwc->dst_master = dwc_get_dms(NULL);
92*9cade1a4SAndy Shevchenko 
93*9cade1a4SAndy Shevchenko 	return true;
94*9cade1a4SAndy Shevchenko }
95*9cade1a4SAndy Shevchenko 
96*9cade1a4SAndy Shevchenko static void dw_dma_acpi_controller_register(struct dw_dma *dw)
97*9cade1a4SAndy Shevchenko {
98*9cade1a4SAndy Shevchenko 	struct device *dev = dw->dma.dev;
99*9cade1a4SAndy Shevchenko 	struct acpi_dma_filter_info *info;
100*9cade1a4SAndy Shevchenko 	int ret;
101*9cade1a4SAndy Shevchenko 
102*9cade1a4SAndy Shevchenko 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
103*9cade1a4SAndy Shevchenko 	if (!info)
104*9cade1a4SAndy Shevchenko 		return;
105*9cade1a4SAndy Shevchenko 
106*9cade1a4SAndy Shevchenko 	dma_cap_zero(info->dma_cap);
107*9cade1a4SAndy Shevchenko 	dma_cap_set(DMA_SLAVE, info->dma_cap);
108*9cade1a4SAndy Shevchenko 	info->filter_fn = dw_dma_acpi_filter;
109*9cade1a4SAndy Shevchenko 
110*9cade1a4SAndy Shevchenko 	ret = devm_acpi_dma_controller_register(dev, acpi_dma_simple_xlate,
111*9cade1a4SAndy Shevchenko 						info);
112*9cade1a4SAndy Shevchenko 	if (ret)
113*9cade1a4SAndy Shevchenko 		dev_err(dev, "could not register acpi_dma_controller\n");
114*9cade1a4SAndy Shevchenko }
115*9cade1a4SAndy Shevchenko #else /* !CONFIG_ACPI */
116*9cade1a4SAndy Shevchenko static inline void dw_dma_acpi_controller_register(struct dw_dma *dw) {}
117*9cade1a4SAndy Shevchenko #endif /* !CONFIG_ACPI */
118*9cade1a4SAndy Shevchenko 
119*9cade1a4SAndy Shevchenko #ifdef CONFIG_OF
120*9cade1a4SAndy Shevchenko static struct dw_dma_platform_data *
121*9cade1a4SAndy Shevchenko dw_dma_parse_dt(struct platform_device *pdev)
122*9cade1a4SAndy Shevchenko {
123*9cade1a4SAndy Shevchenko 	struct device_node *np = pdev->dev.of_node;
124*9cade1a4SAndy Shevchenko 	struct dw_dma_platform_data *pdata;
125*9cade1a4SAndy Shevchenko 	u32 tmp, arr[4];
126*9cade1a4SAndy Shevchenko 
127*9cade1a4SAndy Shevchenko 	if (!np) {
128*9cade1a4SAndy Shevchenko 		dev_err(&pdev->dev, "Missing DT data\n");
129*9cade1a4SAndy Shevchenko 		return NULL;
130*9cade1a4SAndy Shevchenko 	}
131*9cade1a4SAndy Shevchenko 
132*9cade1a4SAndy Shevchenko 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
133*9cade1a4SAndy Shevchenko 	if (!pdata)
134*9cade1a4SAndy Shevchenko 		return NULL;
135*9cade1a4SAndy Shevchenko 
136*9cade1a4SAndy Shevchenko 	if (of_property_read_u32(np, "dma-channels", &pdata->nr_channels))
137*9cade1a4SAndy Shevchenko 		return NULL;
138*9cade1a4SAndy Shevchenko 
139*9cade1a4SAndy Shevchenko 	if (of_property_read_bool(np, "is_private"))
140*9cade1a4SAndy Shevchenko 		pdata->is_private = true;
141*9cade1a4SAndy Shevchenko 
142*9cade1a4SAndy Shevchenko 	if (!of_property_read_u32(np, "chan_allocation_order", &tmp))
143*9cade1a4SAndy Shevchenko 		pdata->chan_allocation_order = (unsigned char)tmp;
144*9cade1a4SAndy Shevchenko 
145*9cade1a4SAndy Shevchenko 	if (!of_property_read_u32(np, "chan_priority", &tmp))
146*9cade1a4SAndy Shevchenko 		pdata->chan_priority = tmp;
147*9cade1a4SAndy Shevchenko 
148*9cade1a4SAndy Shevchenko 	if (!of_property_read_u32(np, "block_size", &tmp))
149*9cade1a4SAndy Shevchenko 		pdata->block_size = tmp;
150*9cade1a4SAndy Shevchenko 
151*9cade1a4SAndy Shevchenko 	if (!of_property_read_u32(np, "dma-masters", &tmp)) {
152*9cade1a4SAndy Shevchenko 		if (tmp > 4)
153*9cade1a4SAndy Shevchenko 			return NULL;
154*9cade1a4SAndy Shevchenko 
155*9cade1a4SAndy Shevchenko 		pdata->nr_masters = tmp;
156*9cade1a4SAndy Shevchenko 	}
157*9cade1a4SAndy Shevchenko 
158*9cade1a4SAndy Shevchenko 	if (!of_property_read_u32_array(np, "data_width", arr,
159*9cade1a4SAndy Shevchenko 				pdata->nr_masters))
160*9cade1a4SAndy Shevchenko 		for (tmp = 0; tmp < pdata->nr_masters; tmp++)
161*9cade1a4SAndy Shevchenko 			pdata->data_width[tmp] = arr[tmp];
162*9cade1a4SAndy Shevchenko 
163*9cade1a4SAndy Shevchenko 	return pdata;
164*9cade1a4SAndy Shevchenko }
165*9cade1a4SAndy Shevchenko #else
166*9cade1a4SAndy Shevchenko static inline struct dw_dma_platform_data *
167*9cade1a4SAndy Shevchenko dw_dma_parse_dt(struct platform_device *pdev)
168*9cade1a4SAndy Shevchenko {
169*9cade1a4SAndy Shevchenko 	return NULL;
170*9cade1a4SAndy Shevchenko }
171*9cade1a4SAndy Shevchenko #endif
172*9cade1a4SAndy Shevchenko 
173*9cade1a4SAndy Shevchenko static int dw_probe(struct platform_device *pdev)
174*9cade1a4SAndy Shevchenko {
175*9cade1a4SAndy Shevchenko 	struct dw_dma_chip *chip;
176*9cade1a4SAndy Shevchenko 	struct device *dev = &pdev->dev;
177*9cade1a4SAndy Shevchenko 	struct resource *mem;
178*9cade1a4SAndy Shevchenko 	struct dw_dma_platform_data *pdata;
179*9cade1a4SAndy Shevchenko 	int err;
180*9cade1a4SAndy Shevchenko 
181*9cade1a4SAndy Shevchenko 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
182*9cade1a4SAndy Shevchenko 	if (!chip)
183*9cade1a4SAndy Shevchenko 		return -ENOMEM;
184*9cade1a4SAndy Shevchenko 
185*9cade1a4SAndy Shevchenko 	chip->irq = platform_get_irq(pdev, 0);
186*9cade1a4SAndy Shevchenko 	if (chip->irq < 0)
187*9cade1a4SAndy Shevchenko 		return chip->irq;
188*9cade1a4SAndy Shevchenko 
189*9cade1a4SAndy Shevchenko 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
190*9cade1a4SAndy Shevchenko 	chip->regs = devm_ioremap_resource(dev, mem);
191*9cade1a4SAndy Shevchenko 	if (IS_ERR(chip->regs))
192*9cade1a4SAndy Shevchenko 		return PTR_ERR(chip->regs);
193*9cade1a4SAndy Shevchenko 
194*9cade1a4SAndy Shevchenko 	/* Apply default dma_mask if needed */
195*9cade1a4SAndy Shevchenko 	if (!dev->dma_mask) {
196*9cade1a4SAndy Shevchenko 		dev->dma_mask = &dev->coherent_dma_mask;
197*9cade1a4SAndy Shevchenko 		dev->coherent_dma_mask = DMA_BIT_MASK(32);
198*9cade1a4SAndy Shevchenko 	}
199*9cade1a4SAndy Shevchenko 
200*9cade1a4SAndy Shevchenko 	pdata = dev_get_platdata(dev);
201*9cade1a4SAndy Shevchenko 	if (!pdata)
202*9cade1a4SAndy Shevchenko 		pdata = dw_dma_parse_dt(pdev);
203*9cade1a4SAndy Shevchenko 
204*9cade1a4SAndy Shevchenko 	chip->dev = dev;
205*9cade1a4SAndy Shevchenko 
206*9cade1a4SAndy Shevchenko 	err = dw_dma_probe(chip, pdata);
207*9cade1a4SAndy Shevchenko 	if (err)
208*9cade1a4SAndy Shevchenko 		return err;
209*9cade1a4SAndy Shevchenko 
210*9cade1a4SAndy Shevchenko 	platform_set_drvdata(pdev, chip);
211*9cade1a4SAndy Shevchenko 
212*9cade1a4SAndy Shevchenko 	if (pdev->dev.of_node) {
213*9cade1a4SAndy Shevchenko 		err = of_dma_controller_register(pdev->dev.of_node,
214*9cade1a4SAndy Shevchenko 						 dw_dma_of_xlate, chip->dw);
215*9cade1a4SAndy Shevchenko 		if (err)
216*9cade1a4SAndy Shevchenko 			dev_err(&pdev->dev,
217*9cade1a4SAndy Shevchenko 				"could not register of_dma_controller\n");
218*9cade1a4SAndy Shevchenko 	}
219*9cade1a4SAndy Shevchenko 
220*9cade1a4SAndy Shevchenko 	if (ACPI_HANDLE(&pdev->dev))
221*9cade1a4SAndy Shevchenko 		dw_dma_acpi_controller_register(chip->dw);
222*9cade1a4SAndy Shevchenko 
223*9cade1a4SAndy Shevchenko 	return 0;
224*9cade1a4SAndy Shevchenko }
225*9cade1a4SAndy Shevchenko 
226*9cade1a4SAndy Shevchenko static int dw_remove(struct platform_device *pdev)
227*9cade1a4SAndy Shevchenko {
228*9cade1a4SAndy Shevchenko 	struct dw_dma_chip *chip = platform_get_drvdata(pdev);
229*9cade1a4SAndy Shevchenko 
230*9cade1a4SAndy Shevchenko 	if (pdev->dev.of_node)
231*9cade1a4SAndy Shevchenko 		of_dma_controller_free(pdev->dev.of_node);
232*9cade1a4SAndy Shevchenko 
233*9cade1a4SAndy Shevchenko 	return dw_dma_remove(chip);
234*9cade1a4SAndy Shevchenko }
235*9cade1a4SAndy Shevchenko 
236*9cade1a4SAndy Shevchenko static void dw_shutdown(struct platform_device *pdev)
237*9cade1a4SAndy Shevchenko {
238*9cade1a4SAndy Shevchenko 	struct dw_dma_chip *chip = platform_get_drvdata(pdev);
239*9cade1a4SAndy Shevchenko 
240*9cade1a4SAndy Shevchenko 	dw_dma_shutdown(chip);
241*9cade1a4SAndy Shevchenko }
242*9cade1a4SAndy Shevchenko 
243*9cade1a4SAndy Shevchenko #ifdef CONFIG_OF
244*9cade1a4SAndy Shevchenko static const struct of_device_id dw_dma_of_id_table[] = {
245*9cade1a4SAndy Shevchenko 	{ .compatible = "snps,dma-spear1340" },
246*9cade1a4SAndy Shevchenko 	{}
247*9cade1a4SAndy Shevchenko };
248*9cade1a4SAndy Shevchenko MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
249*9cade1a4SAndy Shevchenko #endif
250*9cade1a4SAndy Shevchenko 
251*9cade1a4SAndy Shevchenko #ifdef CONFIG_ACPI
252*9cade1a4SAndy Shevchenko static const struct acpi_device_id dw_dma_acpi_id_table[] = {
253*9cade1a4SAndy Shevchenko 	{ "INTL9C60", 0 },
254*9cade1a4SAndy Shevchenko 	{ }
255*9cade1a4SAndy Shevchenko };
256*9cade1a4SAndy Shevchenko #endif
257*9cade1a4SAndy Shevchenko 
258*9cade1a4SAndy Shevchenko #ifdef CONFIG_PM_SLEEP
259*9cade1a4SAndy Shevchenko 
260*9cade1a4SAndy Shevchenko static int dw_suspend_noirq(struct device *dev)
261*9cade1a4SAndy Shevchenko {
262*9cade1a4SAndy Shevchenko 	struct platform_device *pdev = to_platform_device(dev);
263*9cade1a4SAndy Shevchenko 	struct dw_dma_chip *chip = platform_get_drvdata(pdev);
264*9cade1a4SAndy Shevchenko 
265*9cade1a4SAndy Shevchenko 	return dw_dma_suspend(chip);
266*9cade1a4SAndy Shevchenko }
267*9cade1a4SAndy Shevchenko 
268*9cade1a4SAndy Shevchenko static int dw_resume_noirq(struct device *dev)
269*9cade1a4SAndy Shevchenko {
270*9cade1a4SAndy Shevchenko 	struct platform_device *pdev = to_platform_device(dev);
271*9cade1a4SAndy Shevchenko 	struct dw_dma_chip *chip = platform_get_drvdata(pdev);
272*9cade1a4SAndy Shevchenko 
273*9cade1a4SAndy Shevchenko 	return dw_dma_resume(chip);
274*9cade1a4SAndy Shevchenko }
275*9cade1a4SAndy Shevchenko 
276*9cade1a4SAndy Shevchenko #else /* !CONFIG_PM_SLEEP */
277*9cade1a4SAndy Shevchenko 
278*9cade1a4SAndy Shevchenko #define dw_suspend_noirq	NULL
279*9cade1a4SAndy Shevchenko #define dw_resume_noirq		NULL
280*9cade1a4SAndy Shevchenko 
281*9cade1a4SAndy Shevchenko #endif /* !CONFIG_PM_SLEEP */
282*9cade1a4SAndy Shevchenko 
283*9cade1a4SAndy Shevchenko static const struct dev_pm_ops dw_dev_pm_ops = {
284*9cade1a4SAndy Shevchenko 	.suspend_noirq = dw_suspend_noirq,
285*9cade1a4SAndy Shevchenko 	.resume_noirq = dw_resume_noirq,
286*9cade1a4SAndy Shevchenko 	.freeze_noirq = dw_suspend_noirq,
287*9cade1a4SAndy Shevchenko 	.thaw_noirq = dw_resume_noirq,
288*9cade1a4SAndy Shevchenko 	.restore_noirq = dw_resume_noirq,
289*9cade1a4SAndy Shevchenko 	.poweroff_noirq = dw_suspend_noirq,
290*9cade1a4SAndy Shevchenko };
291*9cade1a4SAndy Shevchenko 
292*9cade1a4SAndy Shevchenko static struct platform_driver dw_driver = {
293*9cade1a4SAndy Shevchenko 	.probe		= dw_probe,
294*9cade1a4SAndy Shevchenko 	.remove		= dw_remove,
295*9cade1a4SAndy Shevchenko 	.shutdown	= dw_shutdown,
296*9cade1a4SAndy Shevchenko 	.driver = {
297*9cade1a4SAndy Shevchenko 		.name	= "dw_dmac",
298*9cade1a4SAndy Shevchenko 		.pm	= &dw_dev_pm_ops,
299*9cade1a4SAndy Shevchenko 		.of_match_table = of_match_ptr(dw_dma_of_id_table),
300*9cade1a4SAndy Shevchenko 		.acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
301*9cade1a4SAndy Shevchenko 	},
302*9cade1a4SAndy Shevchenko };
303*9cade1a4SAndy Shevchenko 
304*9cade1a4SAndy Shevchenko static int __init dw_init(void)
305*9cade1a4SAndy Shevchenko {
306*9cade1a4SAndy Shevchenko 	return platform_driver_register(&dw_driver);
307*9cade1a4SAndy Shevchenko }
308*9cade1a4SAndy Shevchenko subsys_initcall(dw_init);
309*9cade1a4SAndy Shevchenko 
310*9cade1a4SAndy Shevchenko static void __exit dw_exit(void)
311*9cade1a4SAndy Shevchenko {
312*9cade1a4SAndy Shevchenko 	platform_driver_unregister(&dw_driver);
313*9cade1a4SAndy Shevchenko }
314*9cade1a4SAndy Shevchenko module_exit(dw_exit);
315*9cade1a4SAndy Shevchenko 
316*9cade1a4SAndy Shevchenko MODULE_LICENSE("GPL v2");
317*9cade1a4SAndy Shevchenko MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver");
318