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_OUTPUTS 127
21 #define TI_XBAR_INPUTS 256
23 struct ti_dma_xbar_data {
26 struct dma_router dmarouter;
29 u16 safe_val; /* Value to rest the crossbar lines */
30 u32 xbar_requests; /* number of DMA requests connected to XBAR */
31 u32 dma_requests; /* number of DMA requests forwarded to DMA */
34 struct ti_dma_xbar_map {
39 static inline void ti_dma_xbar_write(void __iomem *iomem, int xbar, u16 val)
41 writew_relaxed(val, iomem + (xbar * 2));
44 static void ti_dma_xbar_free(struct device *dev, void *route_data)
46 struct ti_dma_xbar_data *xbar = dev_get_drvdata(dev);
47 struct ti_dma_xbar_map *map = route_data;
49 dev_dbg(dev, "Unmapping XBAR%u (was routed to %d)\n",
50 map->xbar_in, map->xbar_out);
52 ti_dma_xbar_write(xbar->iomem, map->xbar_out, xbar->safe_val);
53 idr_remove(&xbar->map_idr, map->xbar_out);
57 static void *ti_dma_xbar_route_allocate(struct of_phandle_args *dma_spec,
60 struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
61 struct ti_dma_xbar_data *xbar = platform_get_drvdata(pdev);
62 struct ti_dma_xbar_map *map;
64 if (dma_spec->args[0] >= xbar->xbar_requests) {
65 dev_err(&pdev->dev, "Invalid XBAR request number: %d\n",
67 return ERR_PTR(-EINVAL);
70 /* The of_node_put() will be done in the core for the node */
71 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
73 dev_err(&pdev->dev, "Can't get DMA master\n");
74 return ERR_PTR(-EINVAL);
77 map = kzalloc(sizeof(*map), GFP_KERNEL);
79 of_node_put(dma_spec->np);
80 return ERR_PTR(-ENOMEM);
83 map->xbar_out = idr_alloc(&xbar->map_idr, NULL, 0, xbar->dma_requests,
85 map->xbar_in = (u16)dma_spec->args[0];
87 /* The DMA request is 1 based in sDMA */
88 dma_spec->args[0] = map->xbar_out + 1;
90 dev_dbg(&pdev->dev, "Mapping XBAR%u to DMA%d\n",
91 map->xbar_in, map->xbar_out);
93 ti_dma_xbar_write(xbar->iomem, map->xbar_out, map->xbar_in);
98 static int ti_dma_xbar_probe(struct platform_device *pdev)
100 struct device_node *node = pdev->dev.of_node;
101 struct device_node *dma_node;
102 struct ti_dma_xbar_data *xbar;
103 struct resource *res;
111 xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
115 idr_init(&xbar->map_idr);
117 dma_node = of_parse_phandle(node, "dma-masters", 0);
119 dev_err(&pdev->dev, "Can't get DMA master node\n");
123 if (of_property_read_u32(dma_node, "dma-requests",
124 &xbar->dma_requests)) {
126 "Missing XBAR output information, using %u.\n",
128 xbar->dma_requests = TI_XBAR_OUTPUTS;
130 of_node_put(dma_node);
132 if (of_property_read_u32(node, "dma-requests", &xbar->xbar_requests)) {
134 "Missing XBAR input information, using %u.\n",
136 xbar->xbar_requests = TI_XBAR_INPUTS;
139 if (!of_property_read_u32(node, "ti,dma-safe-map", &safe_val))
140 xbar->safe_val = (u16)safe_val;
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
146 iomem = devm_ioremap_resource(&pdev->dev, res);
152 xbar->dmarouter.dev = &pdev->dev;
153 xbar->dmarouter.route_free = ti_dma_xbar_free;
155 platform_set_drvdata(pdev, xbar);
157 /* Reset the crossbar */
158 for (i = 0; i < xbar->dma_requests; i++)
159 ti_dma_xbar_write(xbar->iomem, i, xbar->safe_val);
161 ret = of_dma_router_register(node, ti_dma_xbar_route_allocate,
164 /* Restore the defaults for the crossbar */
165 for (i = 0; i < xbar->dma_requests; i++)
166 ti_dma_xbar_write(xbar->iomem, i, i);
172 static const struct of_device_id ti_dma_xbar_match[] = {
173 { .compatible = "ti,dra7-dma-crossbar" },
177 static struct platform_driver ti_dma_xbar_driver = {
179 .name = "ti-dma-crossbar",
180 .of_match_table = of_match_ptr(ti_dma_xbar_match),
182 .probe = ti_dma_xbar_probe,
185 int omap_dmaxbar_init(void)
187 return platform_driver_register(&ti_dma_xbar_driver);
189 arch_initcall(omap_dmaxbar_init);