blob: 1fd3fb73d6e8fc54602088200d3eccb56a4183a7 [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 Ujfalusia074ae32015-04-09 12:35:49 +030023struct ti_dma_xbar_data {
24 void __iomem *iomem;
25
26 struct dma_router dmarouter;
Misael Lopez Cruzbe559da2015-07-22 11:48:09 +030027 struct idr map_idr;
Peter Ujfalusia074ae32015-04-09 12:35:49 +030028
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 */
32};
33
34struct ti_dma_xbar_map {
35 u16 xbar_in;
36 int xbar_out;
37};
38
39static inline void ti_dma_xbar_write(void __iomem *iomem, int xbar, u16 val)
40{
41 writew_relaxed(val, iomem + (xbar * 2));
42}
43
44static void ti_dma_xbar_free(struct device *dev, void *route_data)
45{
46 struct ti_dma_xbar_data *xbar = dev_get_drvdata(dev);
47 struct ti_dma_xbar_map *map = route_data;
48
49 dev_dbg(dev, "Unmapping XBAR%u (was routed to %d)\n",
50 map->xbar_in, map->xbar_out);
51
52 ti_dma_xbar_write(xbar->iomem, map->xbar_out, xbar->safe_val);
Misael Lopez Cruzbe559da2015-07-22 11:48:09 +030053 idr_remove(&xbar->map_idr, map->xbar_out);
Peter Ujfalusia074ae32015-04-09 12:35:49 +030054 kfree(map);
55}
56
57static void *ti_dma_xbar_route_allocate(struct of_phandle_args *dma_spec,
58 struct of_dma *ofdma)
59{
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;
63
64 if (dma_spec->args[0] >= xbar->xbar_requests) {
65 dev_err(&pdev->dev, "Invalid XBAR request number: %d\n",
66 dma_spec->args[0]);
67 return ERR_PTR(-EINVAL);
68 }
69
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);
72 if (!dma_spec->np) {
73 dev_err(&pdev->dev, "Can't get DMA master\n");
74 return ERR_PTR(-EINVAL);
75 }
76
77 map = kzalloc(sizeof(*map), GFP_KERNEL);
78 if (!map) {
79 of_node_put(dma_spec->np);
80 return ERR_PTR(-ENOMEM);
81 }
82
Misael Lopez Cruzbe559da2015-07-22 11:48:09 +030083 map->xbar_out = idr_alloc(&xbar->map_idr, NULL, 0, xbar->dma_requests,
Peter Ujfalusia074ae32015-04-09 12:35:49 +030084 GFP_KERNEL);
85 map->xbar_in = (u16)dma_spec->args[0];
86
87 /* The DMA request is 1 based in sDMA */
88 dma_spec->args[0] = map->xbar_out + 1;
89
90 dev_dbg(&pdev->dev, "Mapping XBAR%u to DMA%d\n",
91 map->xbar_in, map->xbar_out);
92
93 ti_dma_xbar_write(xbar->iomem, map->xbar_out, map->xbar_in);
94
95 return map;
96}
97
98static int ti_dma_xbar_probe(struct platform_device *pdev)
99{
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;
104 u32 safe_val;
105 void __iomem *iomem;
106 int i, ret;
107
108 if (!node)
109 return -ENODEV;
110
111 xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
112 if (!xbar)
113 return -ENOMEM;
114
Misael Lopez Cruzbe559da2015-07-22 11:48:09 +0300115 idr_init(&xbar->map_idr);
116
Peter Ujfalusia074ae32015-04-09 12:35:49 +0300117 dma_node = of_parse_phandle(node, "dma-masters", 0);
118 if (!dma_node) {
119 dev_err(&pdev->dev, "Can't get DMA master node\n");
120 return -ENODEV;
121 }
122
123 if (of_property_read_u32(dma_node, "dma-requests",
124 &xbar->dma_requests)) {
125 dev_info(&pdev->dev,
126 "Missing XBAR output information, using %u.\n",
127 TI_XBAR_OUTPUTS);
128 xbar->dma_requests = TI_XBAR_OUTPUTS;
129 }
130 of_node_put(dma_node);
131
132 if (of_property_read_u32(node, "dma-requests", &xbar->xbar_requests)) {
133 dev_info(&pdev->dev,
134 "Missing XBAR input information, using %u.\n",
135 TI_XBAR_INPUTS);
136 xbar->xbar_requests = TI_XBAR_INPUTS;
137 }
138
139 if (!of_property_read_u32(node, "ti,dma-safe-map", &safe_val))
140 xbar->safe_val = (u16)safe_val;
141
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 if (!res)
144 return -ENODEV;
145
146 iomem = devm_ioremap_resource(&pdev->dev, res);
147 if (!iomem)
148 return -ENOMEM;
149
150 xbar->iomem = iomem;
151
152 xbar->dmarouter.dev = &pdev->dev;
153 xbar->dmarouter.route_free = ti_dma_xbar_free;
154
155 platform_set_drvdata(pdev, xbar);
156
157 /* Reset the crossbar */
158 for (i = 0; i < xbar->dma_requests; i++)
159 ti_dma_xbar_write(xbar->iomem, i, xbar->safe_val);
160
161 ret = of_dma_router_register(node, ti_dma_xbar_route_allocate,
162 &xbar->dmarouter);
163 if (ret) {
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);
167 }
168
169 return ret;
170}
171
172static const struct of_device_id ti_dma_xbar_match[] = {
173 { .compatible = "ti,dra7-dma-crossbar" },
174 {},
175};
176
177static struct platform_driver ti_dma_xbar_driver = {
178 .driver = {
179 .name = "ti-dma-crossbar",
180 .of_match_table = of_match_ptr(ti_dma_xbar_match),
181 },
182 .probe = ti_dma_xbar_probe,
183};
184
185int omap_dmaxbar_init(void)
186{
187 return platform_driver_register(&ti_dma_xbar_driver);
188}
189arch_initcall(omap_dmaxbar_init);