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/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/of_dma.h>
19 #define TI_XBAR_DRA7 0
20 #define TI_XBAR_AM335X 1
21 static const u32 ti_xbar_type
[] = {
22 [TI_XBAR_DRA7
] = TI_XBAR_DRA7
,
23 [TI_XBAR_AM335X
] = TI_XBAR_AM335X
,
26 static const struct of_device_id ti_dma_xbar_match
[] = {
28 .compatible
= "ti,dra7-dma-crossbar",
29 .data
= &ti_xbar_type
[TI_XBAR_DRA7
],
32 .compatible
= "ti,am335x-edma-crossbar",
33 .data
= &ti_xbar_type
[TI_XBAR_AM335X
],
38 /* Crossbar on AM335x/AM437x family */
39 #define TI_AM335X_XBAR_LINES 64
41 struct ti_am335x_xbar_data
{
44 struct dma_router dmarouter
;
46 u32 xbar_events
; /* maximum number of events to select in xbar */
47 u32 dma_requests
; /* number of DMA requests on eDMA */
50 struct ti_am335x_xbar_map
{
55 static inline void ti_am335x_xbar_write(void __iomem
*iomem
, int event
, u8 val
)
58 * TPCC_EVT_MUX_60_63 register layout is different than the
59 * rest, in the sense, that event 63 is mapped to lowest byte
60 * and event 60 is mapped to highest, handle it separately.
62 if (event
>= 60 && event
<= 63)
63 writeb_relaxed(val
, iomem
+ (63 - event
% 4));
65 writeb_relaxed(val
, iomem
+ event
);
68 static void ti_am335x_xbar_free(struct device
*dev
, void *route_data
)
70 struct ti_am335x_xbar_data
*xbar
= dev_get_drvdata(dev
);
71 struct ti_am335x_xbar_map
*map
= route_data
;
73 dev_dbg(dev
, "Unmapping XBAR event %u on channel %u\n",
74 map
->mux_val
, map
->dma_line
);
76 ti_am335x_xbar_write(xbar
->iomem
, map
->dma_line
, 0);
80 static void *ti_am335x_xbar_route_allocate(struct of_phandle_args
*dma_spec
,
83 struct platform_device
*pdev
= of_find_device_by_node(ofdma
->of_node
);
84 struct ti_am335x_xbar_data
*xbar
= platform_get_drvdata(pdev
);
85 struct ti_am335x_xbar_map
*map
;
87 if (dma_spec
->args_count
!= 3)
88 return ERR_PTR(-EINVAL
);
90 if (dma_spec
->args
[2] >= xbar
->xbar_events
) {
91 dev_err(&pdev
->dev
, "Invalid XBAR event number: %d\n",
93 return ERR_PTR(-EINVAL
);
96 if (dma_spec
->args
[0] >= xbar
->dma_requests
) {
97 dev_err(&pdev
->dev
, "Invalid DMA request line number: %d\n",
99 return ERR_PTR(-EINVAL
);
102 /* The of_node_put() will be done in the core for the node */
103 dma_spec
->np
= of_parse_phandle(ofdma
->of_node
, "dma-masters", 0);
105 dev_err(&pdev
->dev
, "Can't get DMA master\n");
106 return ERR_PTR(-EINVAL
);
109 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
111 of_node_put(dma_spec
->np
);
112 return ERR_PTR(-ENOMEM
);
115 map
->dma_line
= (u16
)dma_spec
->args
[0];
116 map
->mux_val
= (u8
)dma_spec
->args
[2];
118 dma_spec
->args
[2] = 0;
119 dma_spec
->args_count
= 2;
121 dev_dbg(&pdev
->dev
, "Mapping XBAR event%u to DMA%u\n",
122 map
->mux_val
, map
->dma_line
);
124 ti_am335x_xbar_write(xbar
->iomem
, map
->dma_line
, map
->mux_val
);
129 static const struct of_device_id ti_am335x_master_match
[] = {
130 { .compatible
= "ti,edma3-tpcc", },
134 static int ti_am335x_xbar_probe(struct platform_device
*pdev
)
136 struct device_node
*node
= pdev
->dev
.of_node
;
137 const struct of_device_id
*match
;
138 struct device_node
*dma_node
;
139 struct ti_am335x_xbar_data
*xbar
;
140 struct resource
*res
;
147 xbar
= devm_kzalloc(&pdev
->dev
, sizeof(*xbar
), GFP_KERNEL
);
151 dma_node
= of_parse_phandle(node
, "dma-masters", 0);
153 dev_err(&pdev
->dev
, "Can't get DMA master node\n");
157 match
= of_match_node(ti_am335x_master_match
, dma_node
);
159 dev_err(&pdev
->dev
, "DMA master is not supported\n");
160 of_node_put(dma_node
);
164 if (of_property_read_u32(dma_node
, "dma-requests",
165 &xbar
->dma_requests
)) {
167 "Missing XBAR output information, using %u.\n",
168 TI_AM335X_XBAR_LINES
);
169 xbar
->dma_requests
= TI_AM335X_XBAR_LINES
;
171 of_node_put(dma_node
);
173 if (of_property_read_u32(node
, "dma-requests", &xbar
->xbar_events
)) {
175 "Missing XBAR input information, using %u.\n",
176 TI_AM335X_XBAR_LINES
);
177 xbar
->xbar_events
= TI_AM335X_XBAR_LINES
;
180 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
181 iomem
= devm_ioremap_resource(&pdev
->dev
, res
);
183 return PTR_ERR(iomem
);
187 xbar
->dmarouter
.dev
= &pdev
->dev
;
188 xbar
->dmarouter
.route_free
= ti_am335x_xbar_free
;
190 platform_set_drvdata(pdev
, xbar
);
192 /* Reset the crossbar */
193 for (i
= 0; i
< xbar
->dma_requests
; i
++)
194 ti_am335x_xbar_write(xbar
->iomem
, i
, 0);
196 ret
= of_dma_router_register(node
, ti_am335x_xbar_route_allocate
,
202 /* Crossbar on DRA7xx family */
203 #define TI_DRA7_XBAR_OUTPUTS 127
204 #define TI_DRA7_XBAR_INPUTS 256
206 struct ti_dra7_xbar_data
{
209 struct dma_router dmarouter
;
211 unsigned long *dma_inuse
;
213 u16 safe_val
; /* Value to rest the crossbar lines */
214 u32 xbar_requests
; /* number of DMA requests connected to XBAR */
215 u32 dma_requests
; /* number of DMA requests forwarded to DMA */
219 struct ti_dra7_xbar_map
{
224 static inline void ti_dra7_xbar_write(void __iomem
*iomem
, int xbar
, u16 val
)
226 writew_relaxed(val
, iomem
+ (xbar
* 2));
229 static void ti_dra7_xbar_free(struct device
*dev
, void *route_data
)
231 struct ti_dra7_xbar_data
*xbar
= dev_get_drvdata(dev
);
232 struct ti_dra7_xbar_map
*map
= route_data
;
234 dev_dbg(dev
, "Unmapping XBAR%u (was routed to %d)\n",
235 map
->xbar_in
, map
->xbar_out
);
237 ti_dra7_xbar_write(xbar
->iomem
, map
->xbar_out
, xbar
->safe_val
);
238 mutex_lock(&xbar
->mutex
);
239 clear_bit(map
->xbar_out
, xbar
->dma_inuse
);
240 mutex_unlock(&xbar
->mutex
);
244 static void *ti_dra7_xbar_route_allocate(struct of_phandle_args
*dma_spec
,
245 struct of_dma
*ofdma
)
247 struct platform_device
*pdev
= of_find_device_by_node(ofdma
->of_node
);
248 struct ti_dra7_xbar_data
*xbar
= platform_get_drvdata(pdev
);
249 struct ti_dra7_xbar_map
*map
;
251 if (dma_spec
->args
[0] >= xbar
->xbar_requests
) {
252 dev_err(&pdev
->dev
, "Invalid XBAR request number: %d\n",
254 return ERR_PTR(-EINVAL
);
257 /* The of_node_put() will be done in the core for the node */
258 dma_spec
->np
= of_parse_phandle(ofdma
->of_node
, "dma-masters", 0);
260 dev_err(&pdev
->dev
, "Can't get DMA master\n");
261 return ERR_PTR(-EINVAL
);
264 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
266 of_node_put(dma_spec
->np
);
267 return ERR_PTR(-ENOMEM
);
270 mutex_lock(&xbar
->mutex
);
271 map
->xbar_out
= find_first_zero_bit(xbar
->dma_inuse
,
273 if (map
->xbar_out
== xbar
->dma_requests
) {
274 mutex_unlock(&xbar
->mutex
);
275 dev_err(&pdev
->dev
, "Run out of free DMA requests\n");
277 return ERR_PTR(-ENOMEM
);
279 set_bit(map
->xbar_out
, xbar
->dma_inuse
);
280 mutex_unlock(&xbar
->mutex
);
282 map
->xbar_in
= (u16
)dma_spec
->args
[0];
284 dma_spec
->args
[0] = map
->xbar_out
+ xbar
->dma_offset
;
286 dev_dbg(&pdev
->dev
, "Mapping XBAR%u to DMA%d\n",
287 map
->xbar_in
, map
->xbar_out
);
289 ti_dra7_xbar_write(xbar
->iomem
, map
->xbar_out
, map
->xbar_in
);
294 #define TI_XBAR_EDMA_OFFSET 0
295 #define TI_XBAR_SDMA_OFFSET 1
296 static const u32 ti_dma_offset
[] = {
297 [TI_XBAR_EDMA_OFFSET
] = 0,
298 [TI_XBAR_SDMA_OFFSET
] = 1,
301 static const struct of_device_id ti_dra7_master_match
[] = {
303 .compatible
= "ti,omap4430-sdma",
304 .data
= &ti_dma_offset
[TI_XBAR_SDMA_OFFSET
],
307 .compatible
= "ti,edma3",
308 .data
= &ti_dma_offset
[TI_XBAR_EDMA_OFFSET
],
311 .compatible
= "ti,edma3-tpcc",
312 .data
= &ti_dma_offset
[TI_XBAR_EDMA_OFFSET
],
317 static inline void ti_dra7_xbar_reserve(int offset
, int len
, unsigned long *p
)
319 for (; len
> 0; len
--)
320 set_bit(offset
+ (len
- 1), p
);
323 static int ti_dra7_xbar_probe(struct platform_device
*pdev
)
325 struct device_node
*node
= pdev
->dev
.of_node
;
326 const struct of_device_id
*match
;
327 struct device_node
*dma_node
;
328 struct ti_dra7_xbar_data
*xbar
;
329 struct property
*prop
;
330 struct resource
*res
;
339 xbar
= devm_kzalloc(&pdev
->dev
, sizeof(*xbar
), GFP_KERNEL
);
343 dma_node
= of_parse_phandle(node
, "dma-masters", 0);
345 dev_err(&pdev
->dev
, "Can't get DMA master node\n");
349 match
= of_match_node(ti_dra7_master_match
, dma_node
);
351 dev_err(&pdev
->dev
, "DMA master is not supported\n");
352 of_node_put(dma_node
);
356 if (of_property_read_u32(dma_node
, "dma-requests",
357 &xbar
->dma_requests
)) {
359 "Missing XBAR output information, using %u.\n",
360 TI_DRA7_XBAR_OUTPUTS
);
361 xbar
->dma_requests
= TI_DRA7_XBAR_OUTPUTS
;
363 of_node_put(dma_node
);
365 xbar
->dma_inuse
= devm_kcalloc(&pdev
->dev
,
366 BITS_TO_LONGS(xbar
->dma_requests
),
367 sizeof(unsigned long), GFP_KERNEL
);
368 if (!xbar
->dma_inuse
)
371 if (of_property_read_u32(node
, "dma-requests", &xbar
->xbar_requests
)) {
373 "Missing XBAR input information, using %u.\n",
374 TI_DRA7_XBAR_INPUTS
);
375 xbar
->xbar_requests
= TI_DRA7_XBAR_INPUTS
;
378 if (!of_property_read_u32(node
, "ti,dma-safe-map", &safe_val
))
379 xbar
->safe_val
= (u16
)safe_val
;
382 prop
= of_find_property(node
, "ti,reserved-dma-request-ranges", &sz
);
384 const char pname
[] = "ti,reserved-dma-request-ranges";
385 u32 (*rsv_events
)[2];
386 size_t nelm
= sz
/ sizeof(*rsv_events
);
392 rsv_events
= kcalloc(nelm
, sizeof(*rsv_events
), GFP_KERNEL
);
396 ret
= of_property_read_u32_array(node
, pname
, (u32
*)rsv_events
,
401 for (i
= 0; i
< nelm
; i
++) {
402 ti_dra7_xbar_reserve(rsv_events
[i
][0], rsv_events
[i
][1],
408 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
409 iomem
= devm_ioremap_resource(&pdev
->dev
, res
);
411 return PTR_ERR(iomem
);
415 xbar
->dmarouter
.dev
= &pdev
->dev
;
416 xbar
->dmarouter
.route_free
= ti_dra7_xbar_free
;
417 xbar
->dma_offset
= *(u32
*)match
->data
;
419 mutex_init(&xbar
->mutex
);
420 platform_set_drvdata(pdev
, xbar
);
422 /* Reset the crossbar */
423 for (i
= 0; i
< xbar
->dma_requests
; i
++) {
424 if (!test_bit(i
, xbar
->dma_inuse
))
425 ti_dra7_xbar_write(xbar
->iomem
, i
, xbar
->safe_val
);
428 ret
= of_dma_router_register(node
, ti_dra7_xbar_route_allocate
,
431 /* Restore the defaults for the crossbar */
432 for (i
= 0; i
< xbar
->dma_requests
; i
++) {
433 if (!test_bit(i
, xbar
->dma_inuse
))
434 ti_dra7_xbar_write(xbar
->iomem
, i
, i
);
441 static int ti_dma_xbar_probe(struct platform_device
*pdev
)
443 const struct of_device_id
*match
;
446 match
= of_match_node(ti_dma_xbar_match
, pdev
->dev
.of_node
);
447 if (unlikely(!match
))
450 switch (*(u32
*)match
->data
) {
452 ret
= ti_dra7_xbar_probe(pdev
);
455 ret
= ti_am335x_xbar_probe(pdev
);
458 dev_err(&pdev
->dev
, "Unsupported crossbar\n");
466 static struct platform_driver ti_dma_xbar_driver
= {
468 .name
= "ti-dma-crossbar",
469 .of_match_table
= of_match_ptr(ti_dma_xbar_match
),
471 .probe
= ti_dma_xbar_probe
,
474 static int omap_dmaxbar_init(void)
476 return platform_driver_register(&ti_dma_xbar_driver
);
478 arch_initcall(omap_dmaxbar_init
);