2 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
3 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/slab.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
15 #include <linux/idr.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/of_dma.h>
20 #define TI_XBAR_DRA7 0
21 #define TI_XBAR_AM335X 1
23 static const struct of_device_id ti_dma_xbar_match[] = {
25 .compatible = "ti,dra7-dma-crossbar",
26 .data = (void *)TI_XBAR_DRA7,
29 .compatible = "ti,am335x-edma-crossbar",
30 .data = (void *)TI_XBAR_AM335X,
35 /* Crossbar on AM335x/AM437x family */
36 #define TI_AM335X_XBAR_LINES 64
38 struct ti_am335x_xbar_data {
41 struct dma_router dmarouter;
43 u32 xbar_events; /* maximum number of events to select in xbar */
44 u32 dma_requests; /* number of DMA requests on eDMA */
47 struct ti_am335x_xbar_map {
52 static inline void ti_am335x_xbar_write(void __iomem *iomem, int event, u8 val)
55 * TPCC_EVT_MUX_60_63 register layout is different than the
56 * rest, in the sense, that event 63 is mapped to lowest byte
57 * and event 60 is mapped to highest, handle it separately.
59 if (event >= 60 && event <= 63)
60 writeb_relaxed(val, iomem + (63 - event % 4));
62 writeb_relaxed(val, iomem + event);
65 static void ti_am335x_xbar_free(struct device *dev, void *route_data)
67 struct ti_am335x_xbar_data *xbar = dev_get_drvdata(dev);
68 struct ti_am335x_xbar_map *map = route_data;
70 dev_dbg(dev, "Unmapping XBAR event %u on channel %u\n",
71 map->mux_val, map->dma_line);
73 ti_am335x_xbar_write(xbar->iomem, map->dma_line, 0);
77 static void *ti_am335x_xbar_route_allocate(struct of_phandle_args *dma_spec,
80 struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
81 struct ti_am335x_xbar_data *xbar = platform_get_drvdata(pdev);
82 struct ti_am335x_xbar_map *map;
84 if (dma_spec->args_count != 3)
85 return ERR_PTR(-EINVAL);
87 if (dma_spec->args[2] >= xbar->xbar_events) {
88 dev_err(&pdev->dev, "Invalid XBAR event number: %d\n",
90 return ERR_PTR(-EINVAL);
93 if (dma_spec->args[0] >= xbar->dma_requests) {
94 dev_err(&pdev->dev, "Invalid DMA request line number: %d\n",
96 return ERR_PTR(-EINVAL);
99 /* The of_node_put() will be done in the core for the node */
100 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
102 dev_err(&pdev->dev, "Can't get DMA master\n");
103 return ERR_PTR(-EINVAL);
106 map = kzalloc(sizeof(*map), GFP_KERNEL);
108 of_node_put(dma_spec->np);
109 return ERR_PTR(-ENOMEM);
112 map->dma_line = (u16)dma_spec->args[0];
113 map->mux_val = (u8)dma_spec->args[2];
115 dma_spec->args[2] = 0;
116 dma_spec->args_count = 2;
118 dev_dbg(&pdev->dev, "Mapping XBAR event%u to DMA%u\n",
119 map->mux_val, map->dma_line);
121 ti_am335x_xbar_write(xbar->iomem, map->dma_line, map->mux_val);
126 static const struct of_device_id ti_am335x_master_match[] = {
127 { .compatible = "ti,edma3-tpcc", },
131 static int ti_am335x_xbar_probe(struct platform_device *pdev)
133 struct device_node *node = pdev->dev.of_node;
134 const struct of_device_id *match;
135 struct device_node *dma_node;
136 struct ti_am335x_xbar_data *xbar;
137 struct resource *res;
144 xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
148 dma_node = of_parse_phandle(node, "dma-masters", 0);
150 dev_err(&pdev->dev, "Can't get DMA master node\n");
154 match = of_match_node(ti_am335x_master_match, dma_node);
156 dev_err(&pdev->dev, "DMA master is not supported\n");
157 of_node_put(dma_node);
161 if (of_property_read_u32(dma_node, "dma-requests",
162 &xbar->dma_requests)) {
164 "Missing XBAR output information, using %u.\n",
165 TI_AM335X_XBAR_LINES);
166 xbar->dma_requests = TI_AM335X_XBAR_LINES;
168 of_node_put(dma_node);
170 if (of_property_read_u32(node, "dma-requests", &xbar->xbar_events)) {
172 "Missing XBAR input information, using %u.\n",
173 TI_AM335X_XBAR_LINES);
174 xbar->xbar_events = TI_AM335X_XBAR_LINES;
177 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
178 iomem = devm_ioremap_resource(&pdev->dev, res);
180 return PTR_ERR(iomem);
184 xbar->dmarouter.dev = &pdev->dev;
185 xbar->dmarouter.route_free = ti_am335x_xbar_free;
187 platform_set_drvdata(pdev, xbar);
189 /* Reset the crossbar */
190 for (i = 0; i < xbar->dma_requests; i++)
191 ti_am335x_xbar_write(xbar->iomem, i, 0);
193 ret = of_dma_router_register(node, ti_am335x_xbar_route_allocate,
199 /* Crossbar on DRA7xx family */
200 #define TI_DRA7_XBAR_OUTPUTS 127
201 #define TI_DRA7_XBAR_INPUTS 256
203 #define TI_XBAR_EDMA_OFFSET 0
204 #define TI_XBAR_SDMA_OFFSET 1
206 struct ti_dra7_xbar_data {
209 struct dma_router dmarouter;
212 u16 safe_val; /* Value to rest the crossbar lines */
213 u32 xbar_requests; /* number of DMA requests connected to XBAR */
214 u32 dma_requests; /* number of DMA requests forwarded to DMA */
218 struct ti_dra7_xbar_map {
223 static inline void ti_dra7_xbar_write(void __iomem *iomem, int xbar, u16 val)
225 writew_relaxed(val, iomem + (xbar * 2));
228 static void ti_dra7_xbar_free(struct device *dev, void *route_data)
230 struct ti_dra7_xbar_data *xbar = dev_get_drvdata(dev);
231 struct ti_dra7_xbar_map *map = route_data;
233 dev_dbg(dev, "Unmapping XBAR%u (was routed to %d)\n",
234 map->xbar_in, map->xbar_out);
236 ti_dra7_xbar_write(xbar->iomem, map->xbar_out, xbar->safe_val);
237 idr_remove(&xbar->map_idr, map->xbar_out);
241 static void *ti_dra7_xbar_route_allocate(struct of_phandle_args *dma_spec,
242 struct of_dma *ofdma)
244 struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
245 struct ti_dra7_xbar_data *xbar = platform_get_drvdata(pdev);
246 struct ti_dra7_xbar_map *map;
248 if (dma_spec->args[0] >= xbar->xbar_requests) {
249 dev_err(&pdev->dev, "Invalid XBAR request number: %d\n",
251 return ERR_PTR(-EINVAL);
254 /* The of_node_put() will be done in the core for the node */
255 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
257 dev_err(&pdev->dev, "Can't get DMA master\n");
258 return ERR_PTR(-EINVAL);
261 map = kzalloc(sizeof(*map), GFP_KERNEL);
263 of_node_put(dma_spec->np);
264 return ERR_PTR(-ENOMEM);
267 map->xbar_out = idr_alloc(&xbar->map_idr, NULL, 0, xbar->dma_requests,
269 map->xbar_in = (u16)dma_spec->args[0];
271 dma_spec->args[0] = map->xbar_out + xbar->dma_offset;
273 dev_dbg(&pdev->dev, "Mapping XBAR%u to DMA%d\n",
274 map->xbar_in, map->xbar_out);
276 ti_dra7_xbar_write(xbar->iomem, map->xbar_out, map->xbar_in);
281 static const struct of_device_id ti_dra7_master_match[] = {
283 .compatible = "ti,omap4430-sdma",
284 .data = (void *)TI_XBAR_SDMA_OFFSET,
287 .compatible = "ti,edma3",
288 .data = (void *)TI_XBAR_EDMA_OFFSET,
293 static int ti_dra7_xbar_probe(struct platform_device *pdev)
295 struct device_node *node = pdev->dev.of_node;
296 const struct of_device_id *match;
297 struct device_node *dma_node;
298 struct ti_dra7_xbar_data *xbar;
299 struct resource *res;
307 xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
311 idr_init(&xbar->map_idr);
313 dma_node = of_parse_phandle(node, "dma-masters", 0);
315 dev_err(&pdev->dev, "Can't get DMA master node\n");
319 match = of_match_node(ti_dra7_master_match, dma_node);
321 dev_err(&pdev->dev, "DMA master is not supported\n");
322 of_node_put(dma_node);
326 if (of_property_read_u32(dma_node, "dma-requests",
327 &xbar->dma_requests)) {
329 "Missing XBAR output information, using %u.\n",
330 TI_DRA7_XBAR_OUTPUTS);
331 xbar->dma_requests = TI_DRA7_XBAR_OUTPUTS;
333 of_node_put(dma_node);
335 if (of_property_read_u32(node, "dma-requests", &xbar->xbar_requests)) {
337 "Missing XBAR input information, using %u.\n",
338 TI_DRA7_XBAR_INPUTS);
339 xbar->xbar_requests = TI_DRA7_XBAR_INPUTS;
342 if (!of_property_read_u32(node, "ti,dma-safe-map", &safe_val))
343 xbar->safe_val = (u16)safe_val;
345 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
346 iomem = devm_ioremap_resource(&pdev->dev, res);
348 return PTR_ERR(iomem);
352 xbar->dmarouter.dev = &pdev->dev;
353 xbar->dmarouter.route_free = ti_dra7_xbar_free;
354 xbar->dma_offset = (u32)match->data;
356 platform_set_drvdata(pdev, xbar);
358 /* Reset the crossbar */
359 for (i = 0; i < xbar->dma_requests; i++)
360 ti_dra7_xbar_write(xbar->iomem, i, xbar->safe_val);
362 ret = of_dma_router_register(node, ti_dra7_xbar_route_allocate,
365 /* Restore the defaults for the crossbar */
366 for (i = 0; i < xbar->dma_requests; i++)
367 ti_dra7_xbar_write(xbar->iomem, i, i);
373 static int ti_dma_xbar_probe(struct platform_device *pdev)
375 const struct of_device_id *match;
378 match = of_match_node(ti_dma_xbar_match, pdev->dev.of_node);
379 if (unlikely(!match))
382 switch ((u32)match->data) {
384 ret = ti_dra7_xbar_probe(pdev);
387 ret = ti_am335x_xbar_probe(pdev);
390 dev_err(&pdev->dev, "Unsupported crossbar\n");
398 static struct platform_driver ti_dma_xbar_driver = {
400 .name = "ti-dma-crossbar",
401 .of_match_table = of_match_ptr(ti_dma_xbar_match),
403 .probe = ti_dma_xbar_probe,
406 int omap_dmaxbar_init(void)
408 return platform_driver_register(&ti_dma_xbar_driver);
410 arch_initcall(omap_dmaxbar_init);