xref: /linux/drivers/dma/dw/platform.c (revision a9c56721d6ae99b22e983d0722e6b1b53a11dd59)
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 	int err;
176 
177 	match = device_get_match_data(dev);
178 	if (!match)
179 		return -ENODEV;
180 
181 	data = devm_kmemdup(&pdev->dev, match, sizeof(*match), GFP_KERNEL);
182 	if (!data)
183 		return -ENOMEM;
184 
185 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
186 	if (!chip)
187 		return -ENOMEM;
188 
189 	chip->irq = platform_get_irq(pdev, 0);
190 	if (chip->irq < 0)
191 		return chip->irq;
192 
193 	chip->regs = devm_platform_ioremap_resource(pdev, 0);
194 	if (IS_ERR(chip->regs))
195 		return PTR_ERR(chip->regs);
196 
197 	err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
198 	if (err)
199 		return err;
200 
201 	if (!data->pdata)
202 		data->pdata = dev_get_platdata(dev);
203 	if (!data->pdata)
204 		data->pdata = dw_dma_parse_dt(pdev);
205 
206 	chip->dev = dev;
207 	chip->id = pdev->id;
208 	chip->pdata = data->pdata;
209 
210 	data->chip = chip;
211 
212 	chip->clk = devm_clk_get(chip->dev, "hclk");
213 	if (IS_ERR(chip->clk))
214 		return PTR_ERR(chip->clk);
215 	err = clk_prepare_enable(chip->clk);
216 	if (err)
217 		return err;
218 
219 	pm_runtime_enable(&pdev->dev);
220 
221 	err = data->probe(chip);
222 	if (err)
223 		goto err_dw_dma_probe;
224 
225 	platform_set_drvdata(pdev, data);
226 
227 	if (pdev->dev.of_node) {
228 		err = of_dma_controller_register(pdev->dev.of_node,
229 						 dw_dma_of_xlate, chip->dw);
230 		if (err)
231 			dev_err(&pdev->dev,
232 				"could not register of_dma_controller\n");
233 	}
234 
235 	if (ACPI_HANDLE(&pdev->dev))
236 		dw_dma_acpi_controller_register(chip->dw);
237 
238 	return 0;
239 
240 err_dw_dma_probe:
241 	pm_runtime_disable(&pdev->dev);
242 	clk_disable_unprepare(chip->clk);
243 	return err;
244 }
245 
246 static int dw_remove(struct platform_device *pdev)
247 {
248 	struct dw_dma_chip_pdata *data = platform_get_drvdata(pdev);
249 	struct dw_dma_chip *chip = data->chip;
250 	int ret;
251 
252 	if (pdev->dev.of_node)
253 		of_dma_controller_free(pdev->dev.of_node);
254 
255 	ret = data->remove(chip);
256 	if (ret)
257 		dev_warn(chip->dev, "can't remove device properly: %d\n", ret);
258 
259 	pm_runtime_disable(&pdev->dev);
260 	clk_disable_unprepare(chip->clk);
261 
262 	return 0;
263 }
264 
265 static void dw_shutdown(struct platform_device *pdev)
266 {
267 	struct dw_dma_chip_pdata *data = platform_get_drvdata(pdev);
268 	struct dw_dma_chip *chip = data->chip;
269 
270 	/*
271 	 * We have to call do_dw_dma_disable() to stop any ongoing transfer. On
272 	 * some platforms we can't do that since DMA device is powered off.
273 	 * Moreover we have no possibility to check if the platform is affected
274 	 * or not. That's why we call pm_runtime_get_sync() / pm_runtime_put()
275 	 * unconditionally. On the other hand we can't use
276 	 * pm_runtime_suspended() because runtime PM framework is not fully
277 	 * used by the driver.
278 	 */
279 	pm_runtime_get_sync(chip->dev);
280 	do_dw_dma_disable(chip);
281 	pm_runtime_put_sync_suspend(chip->dev);
282 
283 	clk_disable_unprepare(chip->clk);
284 }
285 
286 #ifdef CONFIG_OF
287 static const struct of_device_id dw_dma_of_id_table[] = {
288 	{ .compatible = "snps,dma-spear1340", .data = &dw_dma_chip_pdata },
289 	{}
290 };
291 MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
292 #endif
293 
294 #ifdef CONFIG_ACPI
295 static const struct acpi_device_id dw_dma_acpi_id_table[] = {
296 	{ "INTL9C60", (kernel_ulong_t)&dw_dma_chip_pdata },
297 	{ "80862286", (kernel_ulong_t)&dw_dma_chip_pdata },
298 	{ "808622C0", (kernel_ulong_t)&dw_dma_chip_pdata },
299 
300 	/* Elkhart Lake iDMA 32-bit (PSE DMA) */
301 	{ "80864BB4", (kernel_ulong_t)&idma32_chip_pdata },
302 	{ "80864BB5", (kernel_ulong_t)&idma32_chip_pdata },
303 	{ "80864BB6", (kernel_ulong_t)&idma32_chip_pdata },
304 
305 	{ }
306 };
307 MODULE_DEVICE_TABLE(acpi, dw_dma_acpi_id_table);
308 #endif
309 
310 #ifdef CONFIG_PM_SLEEP
311 
312 static int dw_suspend_late(struct device *dev)
313 {
314 	struct dw_dma_chip_pdata *data = dev_get_drvdata(dev);
315 	struct dw_dma_chip *chip = data->chip;
316 
317 	do_dw_dma_disable(chip);
318 	clk_disable_unprepare(chip->clk);
319 
320 	return 0;
321 }
322 
323 static int dw_resume_early(struct device *dev)
324 {
325 	struct dw_dma_chip_pdata *data = dev_get_drvdata(dev);
326 	struct dw_dma_chip *chip = data->chip;
327 	int ret;
328 
329 	ret = clk_prepare_enable(chip->clk);
330 	if (ret)
331 		return ret;
332 
333 	return do_dw_dma_enable(chip);
334 }
335 
336 #endif /* CONFIG_PM_SLEEP */
337 
338 static const struct dev_pm_ops dw_dev_pm_ops = {
339 	SET_LATE_SYSTEM_SLEEP_PM_OPS(dw_suspend_late, dw_resume_early)
340 };
341 
342 static struct platform_driver dw_driver = {
343 	.probe		= dw_probe,
344 	.remove		= dw_remove,
345 	.shutdown       = dw_shutdown,
346 	.driver = {
347 		.name	= DRV_NAME,
348 		.pm	= &dw_dev_pm_ops,
349 		.of_match_table = of_match_ptr(dw_dma_of_id_table),
350 		.acpi_match_table = ACPI_PTR(dw_dma_acpi_id_table),
351 	},
352 };
353 
354 static int __init dw_init(void)
355 {
356 	return platform_driver_register(&dw_driver);
357 }
358 subsys_initcall(dw_init);
359 
360 static void __exit dw_exit(void)
361 {
362 	platform_driver_unregister(&dw_driver);
363 }
364 module_exit(dw_exit);
365 
366 MODULE_LICENSE("GPL v2");
367 MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller platform driver");
368 MODULE_ALIAS("platform:" DRV_NAME);
369