blob: 5cce8c9d00265bd41e556e991ad3d374431c9f0b [file] [log] [blame]
Peter Ujfalusia074ae32015-04-09 12:35:49 +03001/*
2 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
3 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
4 *
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.
8 *
9 */
10#include <linux/slab.h>
11#include <linux/err.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/io.h>
15#include <linux/idr.h>
16#include <linux/of_address.h>
17#include <linux/of_device.h>
18#include <linux/of_dma.h>
19
20#define TI_XBAR_OUTPUTS 127
21#define TI_XBAR_INPUTS 256
22
Peter Ujfalusi1eb995b2015-07-22 11:48:10 +030023#define TI_XBAR_EDMA_OFFSET 0
24#define TI_XBAR_SDMA_OFFSET 1
25
Peter Ujfalusia074ae32015-04-09 12:35:49 +030026struct ti_dma_xbar_data {
27 void __iomem *iomem;
28
29 struct dma_router dmarouter;
Misael Lopez Cruzbe559da2015-07-22 11:48:09 +030030 struct idr map_idr;
Peter Ujfalusia074ae32015-04-09 12:35:49 +030031
32 u16 safe_val; /* Value to rest the crossbar lines */
33 u32 xbar_requests; /* number of DMA requests connected to XBAR */
34 u32 dma_requests; /* number of DMA requests forwarded to DMA */
Peter Ujfalusi1eb995b2015-07-22 11:48:10 +030035 u32 dma_offset;
Peter Ujfalusia074ae32015-04-09 12:35:49 +030036};
37
38struct ti_dma_xbar_map {
39 u16 xbar_in;
40 int xbar_out;
41};
42
43static inline void ti_dma_xbar_write(void __iomem *iomem, int xbar, u16 val)
44{
45 writew_relaxed(val, iomem + (xbar * 2));
46}
47
48static void ti_dma_xbar_free(struct device *dev, void *route_data)
49{
50 struct ti_dma_xbar_data *xbar = dev_get_drvdata(dev);
51 struct ti_dma_xbar_map *map = route_data;
52
53 dev_dbg(dev, "Unmapping XBAR%u (was routed to %d)\n",
54 map->xbar_in, map->xbar_out);
55
56 ti_dma_xbar_write(xbar->iomem, map->xbar_out, xbar->safe_val);
Misael Lopez Cruzbe559da2015-07-22 11:48:09 +030057 idr_remove(&xbar->map_idr, map->xbar_out);
Peter Ujfalusia074ae32015-04-09 12:35:49 +030058 kfree(map);
59}
60
61static void *ti_dma_xbar_route_allocate(struct of_phandle_args *dma_spec,
62 struct of_dma *ofdma)
63{
64 struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
65 struct ti_dma_xbar_data *xbar = platform_get_drvdata(pdev);
66 struct ti_dma_xbar_map *map;
67
68 if (dma_spec->args[0] >= xbar->xbar_requests) {
69 dev_err(&pdev->dev, "Invalid XBAR request number: %d\n",
70 dma_spec->args[0]);
71 return ERR_PTR(-EINVAL);
72 }
73
74 /* The of_node_put() will be done in the core for the node */
75 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
76 if (!dma_spec->np) {
77 dev_err(&pdev->dev, "Can't get DMA master\n");
78 return ERR_PTR(-EINVAL);
79 }
80
81 map = kzalloc(sizeof(*map), GFP_KERNEL);
82 if (!map) {
83 of_node_put(dma_spec->np);
84 return ERR_PTR(-ENOMEM);
85 }
86
Misael Lopez Cruzbe559da2015-07-22 11:48:09 +030087 map->xbar_out = idr_alloc(&xbar->map_idr, NULL, 0, xbar->dma_requests,
Peter Ujfalusia074ae32015-04-09 12:35:49 +030088 GFP_KERNEL);
89 map->xbar_in = (u16)dma_spec->args[0];
90
Peter Ujfalusi1eb995b2015-07-22 11:48:10 +030091 dma_spec->args[0] = map->xbar_out + xbar->dma_offset;
Peter Ujfalusia074ae32015-04-09 12:35:49 +030092
93 dev_dbg(&pdev->dev, "Mapping XBAR%u to DMA%d\n",
94 map->xbar_in, map->xbar_out);
95
96 ti_dma_xbar_write(xbar->iomem, map->xbar_out, map->xbar_in);
97
98 return map;
99}
100
Peter Ujfalusi1eb995b2015-07-22 11:48:10 +0300101static const struct of_device_id ti_dma_master_match[] = {
102 {
103 .compatible = "ti,omap4430-sdma",
104 .data = (void *)TI_XBAR_SDMA_OFFSET,
105 },
106 {
107 .compatible = "ti,edma3",
108 .data = (void *)TI_XBAR_EDMA_OFFSET,
109 },
110 {},
111};
112
Peter Ujfalusia074ae32015-04-09 12:35:49 +0300113static int ti_dma_xbar_probe(struct platform_device *pdev)
114{
115 struct device_node *node = pdev->dev.of_node;
Peter Ujfalusi1eb995b2015-07-22 11:48:10 +0300116 const struct of_device_id *match;
Peter Ujfalusia074ae32015-04-09 12:35:49 +0300117 struct device_node *dma_node;
118 struct ti_dma_xbar_data *xbar;
119 struct resource *res;
120 u32 safe_val;
121 void __iomem *iomem;
122 int i, ret;
123
124 if (!node)
125 return -ENODEV;
126
127 xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
128 if (!xbar)
129 return -ENOMEM;
130
Misael Lopez Cruzbe559da2015-07-22 11:48:09 +0300131 idr_init(&xbar->map_idr);
132
Peter Ujfalusia074ae32015-04-09 12:35:49 +0300133 dma_node = of_parse_phandle(node, "dma-masters", 0);
134 if (!dma_node) {
135 dev_err(&pdev->dev, "Can't get DMA master node\n");
136 return -ENODEV;
137 }
138
Peter Ujfalusi1eb995b2015-07-22 11:48:10 +0300139 match = of_match_node(ti_dma_master_match, dma_node);
140 if (!match) {
141 dev_err(&pdev->dev, "DMA master is not supported\n");
142 return -EINVAL;
143 }
144
Peter Ujfalusia074ae32015-04-09 12:35:49 +0300145 if (of_property_read_u32(dma_node, "dma-requests",
146 &xbar->dma_requests)) {
147 dev_info(&pdev->dev,
148 "Missing XBAR output information, using %u.\n",
149 TI_XBAR_OUTPUTS);
150 xbar->dma_requests = TI_XBAR_OUTPUTS;
151 }
152 of_node_put(dma_node);
153
154 if (of_property_read_u32(node, "dma-requests", &xbar->xbar_requests)) {
155 dev_info(&pdev->dev,
156 "Missing XBAR input information, using %u.\n",
157 TI_XBAR_INPUTS);
158 xbar->xbar_requests = TI_XBAR_INPUTS;
159 }
160
161 if (!of_property_read_u32(node, "ti,dma-safe-map", &safe_val))
162 xbar->safe_val = (u16)safe_val;
163
164 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Peter Ujfalusia074ae32015-04-09 12:35:49 +0300165 iomem = devm_ioremap_resource(&pdev->dev, res);
Axel Lin28eb2322015-07-10 22:13:19 +0800166 if (IS_ERR(iomem))
167 return PTR_ERR(iomem);
Peter Ujfalusia074ae32015-04-09 12:35:49 +0300168
169 xbar->iomem = iomem;
170
171 xbar->dmarouter.dev = &pdev->dev;
172 xbar->dmarouter.route_free = ti_dma_xbar_free;
Peter Ujfalusi1eb995b2015-07-22 11:48:10 +0300173 xbar->dma_offset = (u32)match->data;
Peter Ujfalusia074ae32015-04-09 12:35:49 +0300174
175 platform_set_drvdata(pdev, xbar);
176
177 /* Reset the crossbar */
178 for (i = 0; i < xbar->dma_requests; i++)
179 ti_dma_xbar_write(xbar->iomem, i, xbar->safe_val);
180
181 ret = of_dma_router_register(node, ti_dma_xbar_route_allocate,
182 &xbar->dmarouter);
183 if (ret) {
184 /* Restore the defaults for the crossbar */
185 for (i = 0; i < xbar->dma_requests; i++)
186 ti_dma_xbar_write(xbar->iomem, i, i);
187 }
188
189 return ret;
190}
191
192static const struct of_device_id ti_dma_xbar_match[] = {
193 { .compatible = "ti,dra7-dma-crossbar" },
194 {},
195};
196
197static struct platform_driver ti_dma_xbar_driver = {
198 .driver = {
199 .name = "ti-dma-crossbar",
200 .of_match_table = of_match_ptr(ti_dma_xbar_match),
201 },
202 .probe = ti_dma_xbar_probe,
203};
204
205int omap_dmaxbar_init(void)
206{
207 return platform_driver_register(&ti_dma_xbar_driver);
208}
209arch_initcall(omap_dmaxbar_init);