ARM: dts: UniPhier: add UART and USB pinmux nodes
[linux-2.6/btrfs-unstable.git] / drivers / dma / ti-dma-crossbar.c
blob24f5ca2356bfcaf3b370b7b8ddee4f0992a2ee57
1 /*
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.
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>
20 #define TI_XBAR_OUTPUTS 127
21 #define TI_XBAR_INPUTS 256
23 static DEFINE_IDR(map_idr);
25 struct ti_dma_xbar_data {
26 void __iomem *iomem;
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 {
36 u16 xbar_in;
37 int xbar_out;
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);
55 kfree(map);
58 static void *ti_dma_xbar_route_allocate(struct of_phandle_args *dma_spec,
59 struct of_dma *ofdma)
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",
67 dma_spec->args[0]);
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);
73 if (!dma_spec->np) {
74 dev_err(&pdev->dev, "Can't get DMA master\n");
75 return ERR_PTR(-EINVAL);
78 map = kzalloc(sizeof(*map), GFP_KERNEL);
79 if (!map) {
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,
85 GFP_KERNEL);
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);
96 return map;
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;
105 u32 safe_val;
106 void __iomem *iomem;
107 int i, ret;
109 if (!node)
110 return -ENODEV;
112 xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
113 if (!xbar)
114 return -ENOMEM;
116 dma_node = of_parse_phandle(node, "dma-masters", 0);
117 if (!dma_node) {
118 dev_err(&pdev->dev, "Can't get DMA master node\n");
119 return -ENODEV;
122 if (of_property_read_u32(dma_node, "dma-requests",
123 &xbar->dma_requests)) {
124 dev_info(&pdev->dev,
125 "Missing XBAR output information, using %u.\n",
126 TI_XBAR_OUTPUTS);
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)) {
132 dev_info(&pdev->dev,
133 "Missing XBAR input information, using %u.\n",
134 TI_XBAR_INPUTS);
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);
142 if (!res)
143 return -ENODEV;
145 iomem = devm_ioremap_resource(&pdev->dev, res);
146 if (!iomem)
147 return -ENOMEM;
149 xbar->iomem = iomem;
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,
161 &xbar->dmarouter);
162 if (ret) {
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);
168 return ret;
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 = {
177 .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);