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 static DEFINE_IDR(map_idr
);
25 struct ti_dma_xbar_data
{
28 struct dma_router dmarouter
;
30 u16 safe_val
; /* Value to rest the crossbar lines */
31 u32 xbar_requests
; /* number of DMA requests connected to XBAR */
32 u32 dma_requests
; /* number of DMA requests forwarded to DMA */
35 struct ti_dma_xbar_map
{
40 static inline void ti_dma_xbar_write(void __iomem
*iomem
, int xbar
, u16 val
)
42 writew_relaxed(val
, iomem
+ (xbar
* 2));
45 static void ti_dma_xbar_free(struct device
*dev
, void *route_data
)
47 struct ti_dma_xbar_data
*xbar
= dev_get_drvdata(dev
);
48 struct ti_dma_xbar_map
*map
= route_data
;
50 dev_dbg(dev
, "Unmapping XBAR%u (was routed to %d)\n",
51 map
->xbar_in
, map
->xbar_out
);
53 ti_dma_xbar_write(xbar
->iomem
, map
->xbar_out
, xbar
->safe_val
);
54 idr_remove(&map_idr
, map
->xbar_out
);
58 static void *ti_dma_xbar_route_allocate(struct of_phandle_args
*dma_spec
,
61 struct platform_device
*pdev
= of_find_device_by_node(ofdma
->of_node
);
62 struct ti_dma_xbar_data
*xbar
= platform_get_drvdata(pdev
);
63 struct ti_dma_xbar_map
*map
;
65 if (dma_spec
->args
[0] >= xbar
->xbar_requests
) {
66 dev_err(&pdev
->dev
, "Invalid XBAR request number: %d\n",
68 return ERR_PTR(-EINVAL
);
71 /* The of_node_put() will be done in the core for the node */
72 dma_spec
->np
= of_parse_phandle(ofdma
->of_node
, "dma-masters", 0);
74 dev_err(&pdev
->dev
, "Can't get DMA master\n");
75 return ERR_PTR(-EINVAL
);
78 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
80 of_node_put(dma_spec
->np
);
81 return ERR_PTR(-ENOMEM
);
84 map
->xbar_out
= idr_alloc(&map_idr
, NULL
, 0, xbar
->dma_requests
,
86 map
->xbar_in
= (u16
)dma_spec
->args
[0];
88 /* The DMA request is 1 based in sDMA */
89 dma_spec
->args
[0] = map
->xbar_out
+ 1;
91 dev_dbg(&pdev
->dev
, "Mapping XBAR%u to DMA%d\n",
92 map
->xbar_in
, map
->xbar_out
);
94 ti_dma_xbar_write(xbar
->iomem
, map
->xbar_out
, map
->xbar_in
);
99 static int ti_dma_xbar_probe(struct platform_device
*pdev
)
101 struct device_node
*node
= pdev
->dev
.of_node
;
102 struct device_node
*dma_node
;
103 struct ti_dma_xbar_data
*xbar
;
104 struct resource
*res
;
112 xbar
= devm_kzalloc(&pdev
->dev
, sizeof(*xbar
), GFP_KERNEL
);
116 dma_node
= of_parse_phandle(node
, "dma-masters", 0);
118 dev_err(&pdev
->dev
, "Can't get DMA master node\n");
122 if (of_property_read_u32(dma_node
, "dma-requests",
123 &xbar
->dma_requests
)) {
125 "Missing XBAR output information, using %u.\n",
127 xbar
->dma_requests
= TI_XBAR_OUTPUTS
;
129 of_node_put(dma_node
);
131 if (of_property_read_u32(node
, "dma-requests", &xbar
->xbar_requests
)) {
133 "Missing XBAR input information, using %u.\n",
135 xbar
->xbar_requests
= TI_XBAR_INPUTS
;
138 if (!of_property_read_u32(node
, "ti,dma-safe-map", &safe_val
))
139 xbar
->safe_val
= (u16
)safe_val
;
141 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
145 iomem
= devm_ioremap_resource(&pdev
->dev
, res
);
151 xbar
->dmarouter
.dev
= &pdev
->dev
;
152 xbar
->dmarouter
.route_free
= ti_dma_xbar_free
;
154 platform_set_drvdata(pdev
, xbar
);
156 /* Reset the crossbar */
157 for (i
= 0; i
< xbar
->dma_requests
; i
++)
158 ti_dma_xbar_write(xbar
->iomem
, i
, xbar
->safe_val
);
160 ret
= of_dma_router_register(node
, ti_dma_xbar_route_allocate
,
163 /* Restore the defaults for the crossbar */
164 for (i
= 0; i
< xbar
->dma_requests
; i
++)
165 ti_dma_xbar_write(xbar
->iomem
, i
, i
);
171 static const struct of_device_id ti_dma_xbar_match
[] = {
172 { .compatible
= "ti,dra7-dma-crossbar" },
176 static struct platform_driver ti_dma_xbar_driver
= {
178 .name
= "ti-dma-crossbar",
179 .of_match_table
= of_match_ptr(ti_dma_xbar_match
),
181 .probe
= ti_dma_xbar_probe
,
184 int omap_dmaxbar_init(void)
186 return platform_driver_register(&ti_dma_xbar_driver
);
188 arch_initcall(omap_dmaxbar_init
);