1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
4 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
6 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/list.h>
11 #include <linux/of_address.h>
12 #include <linux/of_device.h>
13 #include <linux/of_dma.h>
15 #define TI_XBAR_DRA7 0
16 #define TI_XBAR_AM335X 1
17 static const u32 ti_xbar_type[] = {
18 [TI_XBAR_DRA7] = TI_XBAR_DRA7,
19 [TI_XBAR_AM335X] = TI_XBAR_AM335X,
22 static const struct of_device_id ti_dma_xbar_match[] = {
24 .compatible = "ti,dra7-dma-crossbar",
25 .data = &ti_xbar_type[TI_XBAR_DRA7],
28 .compatible = "ti,am335x-edma-crossbar",
29 .data = &ti_xbar_type[TI_XBAR_AM335X],
34 /* Crossbar on AM335x/AM437x family */
35 #define TI_AM335X_XBAR_LINES 64
37 struct ti_am335x_xbar_data {
40 struct dma_router dmarouter;
42 u32 xbar_events; /* maximum number of events to select in xbar */
43 u32 dma_requests; /* number of DMA requests on eDMA */
46 struct ti_am335x_xbar_map {
51 static inline void ti_am335x_xbar_write(void __iomem *iomem, int event, u8 val)
54 * TPCC_EVT_MUX_60_63 register layout is different than the
55 * rest, in the sense, that event 63 is mapped to lowest byte
56 * and event 60 is mapped to highest, handle it separately.
58 if (event >= 60 && event <= 63)
59 writeb_relaxed(val, iomem + (63 - event % 4));
61 writeb_relaxed(val, iomem + event);
64 static void ti_am335x_xbar_free(struct device *dev, void *route_data)
66 struct ti_am335x_xbar_data *xbar = dev_get_drvdata(dev);
67 struct ti_am335x_xbar_map *map = route_data;
69 dev_dbg(dev, "Unmapping XBAR event %u on channel %u\n",
70 map->mux_val, map->dma_line);
72 ti_am335x_xbar_write(xbar->iomem, map->dma_line, 0);
76 static void *ti_am335x_xbar_route_allocate(struct of_phandle_args *dma_spec,
79 struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
80 struct ti_am335x_xbar_data *xbar = platform_get_drvdata(pdev);
81 struct ti_am335x_xbar_map *map;
83 if (dma_spec->args_count != 3)
84 return ERR_PTR(-EINVAL);
86 if (dma_spec->args[2] >= xbar->xbar_events) {
87 dev_err(&pdev->dev, "Invalid XBAR event number: %d\n",
89 return ERR_PTR(-EINVAL);
92 if (dma_spec->args[0] >= xbar->dma_requests) {
93 dev_err(&pdev->dev, "Invalid DMA request line number: %d\n",
95 return ERR_PTR(-EINVAL);
98 /* The of_node_put() will be done in the core for the node */
99 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
101 dev_err(&pdev->dev, "Can't get DMA master\n");
102 return ERR_PTR(-EINVAL);
105 map = kzalloc(sizeof(*map), GFP_KERNEL);
107 of_node_put(dma_spec->np);
108 return ERR_PTR(-ENOMEM);
111 map->dma_line = (u16)dma_spec->args[0];
112 map->mux_val = (u8)dma_spec->args[2];
114 dma_spec->args[2] = 0;
115 dma_spec->args_count = 2;
117 dev_dbg(&pdev->dev, "Mapping XBAR event%u to DMA%u\n",
118 map->mux_val, map->dma_line);
120 ti_am335x_xbar_write(xbar->iomem, map->dma_line, map->mux_val);
125 static const struct of_device_id ti_am335x_master_match[] = {
126 { .compatible = "ti,edma3-tpcc", },
130 static int ti_am335x_xbar_probe(struct platform_device *pdev)
132 struct device_node *node = pdev->dev.of_node;
133 const struct of_device_id *match;
134 struct device_node *dma_node;
135 struct ti_am335x_xbar_data *xbar;
136 struct resource *res;
143 xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
147 dma_node = of_parse_phandle(node, "dma-masters", 0);
149 dev_err(&pdev->dev, "Can't get DMA master node\n");
153 match = of_match_node(ti_am335x_master_match, dma_node);
155 dev_err(&pdev->dev, "DMA master is not supported\n");
156 of_node_put(dma_node);
160 if (of_property_read_u32(dma_node, "dma-requests",
161 &xbar->dma_requests)) {
163 "Missing XBAR output information, using %u.\n",
164 TI_AM335X_XBAR_LINES);
165 xbar->dma_requests = TI_AM335X_XBAR_LINES;
167 of_node_put(dma_node);
169 if (of_property_read_u32(node, "dma-requests", &xbar->xbar_events)) {
171 "Missing XBAR input information, using %u.\n",
172 TI_AM335X_XBAR_LINES);
173 xbar->xbar_events = TI_AM335X_XBAR_LINES;
176 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
177 iomem = devm_ioremap_resource(&pdev->dev, res);
179 return PTR_ERR(iomem);
183 xbar->dmarouter.dev = &pdev->dev;
184 xbar->dmarouter.route_free = ti_am335x_xbar_free;
186 platform_set_drvdata(pdev, xbar);
188 /* Reset the crossbar */
189 for (i = 0; i < xbar->dma_requests; i++)
190 ti_am335x_xbar_write(xbar->iomem, i, 0);
192 ret = of_dma_router_register(node, ti_am335x_xbar_route_allocate,
198 /* Crossbar on DRA7xx family */
199 #define TI_DRA7_XBAR_OUTPUTS 127
200 #define TI_DRA7_XBAR_INPUTS 256
202 struct ti_dra7_xbar_data {
205 struct dma_router dmarouter;
207 unsigned long *dma_inuse;
209 u16 safe_val; /* Value to rest the crossbar lines */
210 u32 xbar_requests; /* number of DMA requests connected to XBAR */
211 u32 dma_requests; /* number of DMA requests forwarded to DMA */
215 struct ti_dra7_xbar_map {
220 static inline void ti_dra7_xbar_write(void __iomem *iomem, int xbar, u16 val)
222 writew_relaxed(val, iomem + (xbar * 2));
225 static void ti_dra7_xbar_free(struct device *dev, void *route_data)
227 struct ti_dra7_xbar_data *xbar = dev_get_drvdata(dev);
228 struct ti_dra7_xbar_map *map = route_data;
230 dev_dbg(dev, "Unmapping XBAR%u (was routed to %d)\n",
231 map->xbar_in, map->xbar_out);
233 ti_dra7_xbar_write(xbar->iomem, map->xbar_out, xbar->safe_val);
234 mutex_lock(&xbar->mutex);
235 clear_bit(map->xbar_out, xbar->dma_inuse);
236 mutex_unlock(&xbar->mutex);
240 static void *ti_dra7_xbar_route_allocate(struct of_phandle_args *dma_spec,
241 struct of_dma *ofdma)
243 struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
244 struct ti_dra7_xbar_data *xbar = platform_get_drvdata(pdev);
245 struct ti_dra7_xbar_map *map;
247 if (dma_spec->args[0] >= xbar->xbar_requests) {
248 dev_err(&pdev->dev, "Invalid XBAR request number: %d\n",
250 return ERR_PTR(-EINVAL);
253 /* The of_node_put() will be done in the core for the node */
254 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
256 dev_err(&pdev->dev, "Can't get DMA master\n");
257 return ERR_PTR(-EINVAL);
260 map = kzalloc(sizeof(*map), GFP_KERNEL);
262 of_node_put(dma_spec->np);
263 return ERR_PTR(-ENOMEM);
266 mutex_lock(&xbar->mutex);
267 map->xbar_out = find_first_zero_bit(xbar->dma_inuse,
269 if (map->xbar_out == xbar->dma_requests) {
270 mutex_unlock(&xbar->mutex);
271 dev_err(&pdev->dev, "Run out of free DMA requests\n");
273 return ERR_PTR(-ENOMEM);
275 set_bit(map->xbar_out, xbar->dma_inuse);
276 mutex_unlock(&xbar->mutex);
278 map->xbar_in = (u16)dma_spec->args[0];
280 dma_spec->args[0] = map->xbar_out + xbar->dma_offset;
282 dev_dbg(&pdev->dev, "Mapping XBAR%u to DMA%d\n",
283 map->xbar_in, map->xbar_out);
285 ti_dra7_xbar_write(xbar->iomem, map->xbar_out, map->xbar_in);
290 #define TI_XBAR_EDMA_OFFSET 0
291 #define TI_XBAR_SDMA_OFFSET 1
292 static const u32 ti_dma_offset[] = {
293 [TI_XBAR_EDMA_OFFSET] = 0,
294 [TI_XBAR_SDMA_OFFSET] = 1,
297 static const struct of_device_id ti_dra7_master_match[] = {
299 .compatible = "ti,omap4430-sdma",
300 .data = &ti_dma_offset[TI_XBAR_SDMA_OFFSET],
303 .compatible = "ti,edma3",
304 .data = &ti_dma_offset[TI_XBAR_EDMA_OFFSET],
307 .compatible = "ti,edma3-tpcc",
308 .data = &ti_dma_offset[TI_XBAR_EDMA_OFFSET],
313 static inline void ti_dra7_xbar_reserve(int offset, int len, unsigned long *p)
315 for (; len > 0; len--)
316 set_bit(offset + (len - 1), p);
319 static int ti_dra7_xbar_probe(struct platform_device *pdev)
321 struct device_node *node = pdev->dev.of_node;
322 const struct of_device_id *match;
323 struct device_node *dma_node;
324 struct ti_dra7_xbar_data *xbar;
325 struct property *prop;
326 struct resource *res;
335 xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
339 dma_node = of_parse_phandle(node, "dma-masters", 0);
341 dev_err(&pdev->dev, "Can't get DMA master node\n");
345 match = of_match_node(ti_dra7_master_match, dma_node);
347 dev_err(&pdev->dev, "DMA master is not supported\n");
348 of_node_put(dma_node);
352 if (of_property_read_u32(dma_node, "dma-requests",
353 &xbar->dma_requests)) {
355 "Missing XBAR output information, using %u.\n",
356 TI_DRA7_XBAR_OUTPUTS);
357 xbar->dma_requests = TI_DRA7_XBAR_OUTPUTS;
359 of_node_put(dma_node);
361 xbar->dma_inuse = devm_kcalloc(&pdev->dev,
362 BITS_TO_LONGS(xbar->dma_requests),
363 sizeof(unsigned long), GFP_KERNEL);
364 if (!xbar->dma_inuse)
367 if (of_property_read_u32(node, "dma-requests", &xbar->xbar_requests)) {
369 "Missing XBAR input information, using %u.\n",
370 TI_DRA7_XBAR_INPUTS);
371 xbar->xbar_requests = TI_DRA7_XBAR_INPUTS;
374 if (!of_property_read_u32(node, "ti,dma-safe-map", &safe_val))
375 xbar->safe_val = (u16)safe_val;
378 prop = of_find_property(node, "ti,reserved-dma-request-ranges", &sz);
380 const char pname[] = "ti,reserved-dma-request-ranges";
381 u32 (*rsv_events)[2];
382 size_t nelm = sz / sizeof(*rsv_events);
388 rsv_events = kcalloc(nelm, sizeof(*rsv_events), GFP_KERNEL);
392 ret = of_property_read_u32_array(node, pname, (u32 *)rsv_events,
397 for (i = 0; i < nelm; i++) {
398 ti_dra7_xbar_reserve(rsv_events[i][0], rsv_events[i][1],
404 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
405 iomem = devm_ioremap_resource(&pdev->dev, res);
407 return PTR_ERR(iomem);
411 xbar->dmarouter.dev = &pdev->dev;
412 xbar->dmarouter.route_free = ti_dra7_xbar_free;
413 xbar->dma_offset = *(u32 *)match->data;
415 mutex_init(&xbar->mutex);
416 platform_set_drvdata(pdev, xbar);
418 /* Reset the crossbar */
419 for (i = 0; i < xbar->dma_requests; i++) {
420 if (!test_bit(i, xbar->dma_inuse))
421 ti_dra7_xbar_write(xbar->iomem, i, xbar->safe_val);
424 ret = of_dma_router_register(node, ti_dra7_xbar_route_allocate,
427 /* Restore the defaults for the crossbar */
428 for (i = 0; i < xbar->dma_requests; i++) {
429 if (!test_bit(i, xbar->dma_inuse))
430 ti_dra7_xbar_write(xbar->iomem, i, i);
437 static int ti_dma_xbar_probe(struct platform_device *pdev)
439 const struct of_device_id *match;
442 match = of_match_node(ti_dma_xbar_match, pdev->dev.of_node);
443 if (unlikely(!match))
446 switch (*(u32 *)match->data) {
448 ret = ti_dra7_xbar_probe(pdev);
451 ret = ti_am335x_xbar_probe(pdev);
454 dev_err(&pdev->dev, "Unsupported crossbar\n");
462 static struct platform_driver ti_dma_xbar_driver = {
464 .name = "ti-dma-crossbar",
465 .of_match_table = of_match_ptr(ti_dma_xbar_match),
467 .probe = ti_dma_xbar_probe,
470 static int omap_dmaxbar_init(void)
472 return platform_driver_register(&ti_dma_xbar_driver);
474 arch_initcall(omap_dmaxbar_init);