GNU Linux-libre 4.4.283-gnu1
[releases.git] / drivers / dma / ti-dma-crossbar.c
1 /*
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_DRA7            0
21 #define TI_XBAR_AM335X          1
22
23 static const struct of_device_id ti_dma_xbar_match[] = {
24         {
25                 .compatible = "ti,dra7-dma-crossbar",
26                 .data = (void *)TI_XBAR_DRA7,
27         },
28         {
29                 .compatible = "ti,am335x-edma-crossbar",
30                 .data = (void *)TI_XBAR_AM335X,
31         },
32         {},
33 };
34
35 /* Crossbar on AM335x/AM437x family */
36 #define TI_AM335X_XBAR_LINES    64
37
38 struct ti_am335x_xbar_data {
39         void __iomem *iomem;
40
41         struct dma_router dmarouter;
42
43         u32 xbar_events; /* maximum number of events to select in xbar */
44         u32 dma_requests; /* number of DMA requests on eDMA */
45 };
46
47 struct ti_am335x_xbar_map {
48         u16 dma_line;
49         u8 mux_val;
50 };
51
52 static inline void ti_am335x_xbar_write(void __iomem *iomem, int event, u8 val)
53 {
54         /*
55          * TPCC_EVT_MUX_60_63 register layout is different than the
56          * rest, in the sense, that event 63 is mapped to lowest byte
57          * and event 60 is mapped to highest, handle it separately.
58          */
59         if (event >= 60 && event <= 63)
60                 writeb_relaxed(val, iomem + (63 - event % 4));
61         else
62                 writeb_relaxed(val, iomem + event);
63 }
64
65 static void ti_am335x_xbar_free(struct device *dev, void *route_data)
66 {
67         struct ti_am335x_xbar_data *xbar = dev_get_drvdata(dev);
68         struct ti_am335x_xbar_map *map = route_data;
69
70         dev_dbg(dev, "Unmapping XBAR event %u on channel %u\n",
71                 map->mux_val, map->dma_line);
72
73         ti_am335x_xbar_write(xbar->iomem, map->dma_line, 0);
74         kfree(map);
75 }
76
77 static void *ti_am335x_xbar_route_allocate(struct of_phandle_args *dma_spec,
78                                            struct of_dma *ofdma)
79 {
80         struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
81         struct ti_am335x_xbar_data *xbar = platform_get_drvdata(pdev);
82         struct ti_am335x_xbar_map *map;
83
84         if (dma_spec->args_count != 3)
85                 return ERR_PTR(-EINVAL);
86
87         if (dma_spec->args[2] >= xbar->xbar_events) {
88                 dev_err(&pdev->dev, "Invalid XBAR event number: %d\n",
89                         dma_spec->args[2]);
90                 return ERR_PTR(-EINVAL);
91         }
92
93         if (dma_spec->args[0] >= xbar->dma_requests) {
94                 dev_err(&pdev->dev, "Invalid DMA request line number: %d\n",
95                         dma_spec->args[0]);
96                 return ERR_PTR(-EINVAL);
97         }
98
99         /* The of_node_put() will be done in the core for the node */
100         dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
101         if (!dma_spec->np) {
102                 dev_err(&pdev->dev, "Can't get DMA master\n");
103                 return ERR_PTR(-EINVAL);
104         }
105
106         map = kzalloc(sizeof(*map), GFP_KERNEL);
107         if (!map) {
108                 of_node_put(dma_spec->np);
109                 return ERR_PTR(-ENOMEM);
110         }
111
112         map->dma_line = (u16)dma_spec->args[0];
113         map->mux_val = (u8)dma_spec->args[2];
114
115         dma_spec->args[2] = 0;
116         dma_spec->args_count = 2;
117
118         dev_dbg(&pdev->dev, "Mapping XBAR event%u to DMA%u\n",
119                 map->mux_val, map->dma_line);
120
121         ti_am335x_xbar_write(xbar->iomem, map->dma_line, map->mux_val);
122
123         return map;
124 }
125
126 static const struct of_device_id ti_am335x_master_match[] = {
127         { .compatible = "ti,edma3-tpcc", },
128         {},
129 };
130
131 static int ti_am335x_xbar_probe(struct platform_device *pdev)
132 {
133         struct device_node *node = pdev->dev.of_node;
134         const struct of_device_id *match;
135         struct device_node *dma_node;
136         struct ti_am335x_xbar_data *xbar;
137         struct resource *res;
138         void __iomem *iomem;
139         int i, ret;
140
141         if (!node)
142                 return -ENODEV;
143
144         xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
145         if (!xbar)
146                 return -ENOMEM;
147
148         dma_node = of_parse_phandle(node, "dma-masters", 0);
149         if (!dma_node) {
150                 dev_err(&pdev->dev, "Can't get DMA master node\n");
151                 return -ENODEV;
152         }
153
154         match = of_match_node(ti_am335x_master_match, dma_node);
155         if (!match) {
156                 dev_err(&pdev->dev, "DMA master is not supported\n");
157                 of_node_put(dma_node);
158                 return -EINVAL;
159         }
160
161         if (of_property_read_u32(dma_node, "dma-requests",
162                                  &xbar->dma_requests)) {
163                 dev_info(&pdev->dev,
164                          "Missing XBAR output information, using %u.\n",
165                          TI_AM335X_XBAR_LINES);
166                 xbar->dma_requests = TI_AM335X_XBAR_LINES;
167         }
168         of_node_put(dma_node);
169
170         if (of_property_read_u32(node, "dma-requests", &xbar->xbar_events)) {
171                 dev_info(&pdev->dev,
172                          "Missing XBAR input information, using %u.\n",
173                          TI_AM335X_XBAR_LINES);
174                 xbar->xbar_events = TI_AM335X_XBAR_LINES;
175         }
176
177         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
178         iomem = devm_ioremap_resource(&pdev->dev, res);
179         if (IS_ERR(iomem))
180                 return PTR_ERR(iomem);
181
182         xbar->iomem = iomem;
183
184         xbar->dmarouter.dev = &pdev->dev;
185         xbar->dmarouter.route_free = ti_am335x_xbar_free;
186
187         platform_set_drvdata(pdev, xbar);
188
189         /* Reset the crossbar */
190         for (i = 0; i < xbar->dma_requests; i++)
191                 ti_am335x_xbar_write(xbar->iomem, i, 0);
192
193         ret = of_dma_router_register(node, ti_am335x_xbar_route_allocate,
194                                      &xbar->dmarouter);
195
196         return ret;
197 }
198
199 /* Crossbar on DRA7xx family */
200 #define TI_DRA7_XBAR_OUTPUTS    127
201 #define TI_DRA7_XBAR_INPUTS     256
202
203 #define TI_XBAR_EDMA_OFFSET     0
204 #define TI_XBAR_SDMA_OFFSET     1
205
206 struct ti_dra7_xbar_data {
207         void __iomem *iomem;
208
209         struct dma_router dmarouter;
210         struct idr map_idr;
211
212         u16 safe_val; /* Value to rest the crossbar lines */
213         u32 xbar_requests; /* number of DMA requests connected to XBAR */
214         u32 dma_requests; /* number of DMA requests forwarded to DMA */
215         u32 dma_offset;
216 };
217
218 struct ti_dra7_xbar_map {
219         u16 xbar_in;
220         int xbar_out;
221 };
222
223 static inline void ti_dra7_xbar_write(void __iomem *iomem, int xbar, u16 val)
224 {
225         writew_relaxed(val, iomem + (xbar * 2));
226 }
227
228 static void ti_dra7_xbar_free(struct device *dev, void *route_data)
229 {
230         struct ti_dra7_xbar_data *xbar = dev_get_drvdata(dev);
231         struct ti_dra7_xbar_map *map = route_data;
232
233         dev_dbg(dev, "Unmapping XBAR%u (was routed to %d)\n",
234                 map->xbar_in, map->xbar_out);
235
236         ti_dra7_xbar_write(xbar->iomem, map->xbar_out, xbar->safe_val);
237         idr_remove(&xbar->map_idr, map->xbar_out);
238         kfree(map);
239 }
240
241 static void *ti_dra7_xbar_route_allocate(struct of_phandle_args *dma_spec,
242                                          struct of_dma *ofdma)
243 {
244         struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
245         struct ti_dra7_xbar_data *xbar = platform_get_drvdata(pdev);
246         struct ti_dra7_xbar_map *map;
247
248         if (dma_spec->args[0] >= xbar->xbar_requests) {
249                 dev_err(&pdev->dev, "Invalid XBAR request number: %d\n",
250                         dma_spec->args[0]);
251                 return ERR_PTR(-EINVAL);
252         }
253
254         /* The of_node_put() will be done in the core for the node */
255         dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
256         if (!dma_spec->np) {
257                 dev_err(&pdev->dev, "Can't get DMA master\n");
258                 return ERR_PTR(-EINVAL);
259         }
260
261         map = kzalloc(sizeof(*map), GFP_KERNEL);
262         if (!map) {
263                 of_node_put(dma_spec->np);
264                 return ERR_PTR(-ENOMEM);
265         }
266
267         map->xbar_out = idr_alloc(&xbar->map_idr, NULL, 0, xbar->dma_requests,
268                                   GFP_KERNEL);
269         map->xbar_in = (u16)dma_spec->args[0];
270
271         dma_spec->args[0] = map->xbar_out + xbar->dma_offset;
272
273         dev_dbg(&pdev->dev, "Mapping XBAR%u to DMA%d\n",
274                 map->xbar_in, map->xbar_out);
275
276         ti_dra7_xbar_write(xbar->iomem, map->xbar_out, map->xbar_in);
277
278         return map;
279 }
280
281 static const struct of_device_id ti_dra7_master_match[] = {
282         {
283                 .compatible = "ti,omap4430-sdma",
284                 .data = (void *)TI_XBAR_SDMA_OFFSET,
285         },
286         {
287                 .compatible = "ti,edma3",
288                 .data = (void *)TI_XBAR_EDMA_OFFSET,
289         },
290         {},
291 };
292
293 static int ti_dra7_xbar_probe(struct platform_device *pdev)
294 {
295         struct device_node *node = pdev->dev.of_node;
296         const struct of_device_id *match;
297         struct device_node *dma_node;
298         struct ti_dra7_xbar_data *xbar;
299         struct resource *res;
300         u32 safe_val;
301         void __iomem *iomem;
302         int i, ret;
303
304         if (!node)
305                 return -ENODEV;
306
307         xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
308         if (!xbar)
309                 return -ENOMEM;
310
311         idr_init(&xbar->map_idr);
312
313         dma_node = of_parse_phandle(node, "dma-masters", 0);
314         if (!dma_node) {
315                 dev_err(&pdev->dev, "Can't get DMA master node\n");
316                 return -ENODEV;
317         }
318
319         match = of_match_node(ti_dra7_master_match, dma_node);
320         if (!match) {
321                 dev_err(&pdev->dev, "DMA master is not supported\n");
322                 of_node_put(dma_node);
323                 return -EINVAL;
324         }
325
326         if (of_property_read_u32(dma_node, "dma-requests",
327                                  &xbar->dma_requests)) {
328                 dev_info(&pdev->dev,
329                          "Missing XBAR output information, using %u.\n",
330                          TI_DRA7_XBAR_OUTPUTS);
331                 xbar->dma_requests = TI_DRA7_XBAR_OUTPUTS;
332         }
333         of_node_put(dma_node);
334
335         if (of_property_read_u32(node, "dma-requests", &xbar->xbar_requests)) {
336                 dev_info(&pdev->dev,
337                          "Missing XBAR input information, using %u.\n",
338                          TI_DRA7_XBAR_INPUTS);
339                 xbar->xbar_requests = TI_DRA7_XBAR_INPUTS;
340         }
341
342         if (!of_property_read_u32(node, "ti,dma-safe-map", &safe_val))
343                 xbar->safe_val = (u16)safe_val;
344
345         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
346         iomem = devm_ioremap_resource(&pdev->dev, res);
347         if (IS_ERR(iomem))
348                 return PTR_ERR(iomem);
349
350         xbar->iomem = iomem;
351
352         xbar->dmarouter.dev = &pdev->dev;
353         xbar->dmarouter.route_free = ti_dra7_xbar_free;
354         xbar->dma_offset = (u32)match->data;
355
356         platform_set_drvdata(pdev, xbar);
357
358         /* Reset the crossbar */
359         for (i = 0; i < xbar->dma_requests; i++)
360                 ti_dra7_xbar_write(xbar->iomem, i, xbar->safe_val);
361
362         ret = of_dma_router_register(node, ti_dra7_xbar_route_allocate,
363                                      &xbar->dmarouter);
364         if (ret) {
365                 /* Restore the defaults for the crossbar */
366                 for (i = 0; i < xbar->dma_requests; i++)
367                         ti_dra7_xbar_write(xbar->iomem, i, i);
368         }
369
370         return ret;
371 }
372
373 static int ti_dma_xbar_probe(struct platform_device *pdev)
374 {
375         const struct of_device_id *match;
376         int ret;
377
378         match = of_match_node(ti_dma_xbar_match, pdev->dev.of_node);
379         if (unlikely(!match))
380                 return -EINVAL;
381
382         switch ((u32)match->data) {
383         case TI_XBAR_DRA7:
384                 ret = ti_dra7_xbar_probe(pdev);
385                 break;
386         case TI_XBAR_AM335X:
387                 ret = ti_am335x_xbar_probe(pdev);
388                 break;
389         default:
390                 dev_err(&pdev->dev, "Unsupported crossbar\n");
391                 ret = -ENODEV;
392                 break;
393         }
394
395         return ret;
396 }
397
398 static struct platform_driver ti_dma_xbar_driver = {
399         .driver = {
400                 .name = "ti-dma-crossbar",
401                 .of_match_table = of_match_ptr(ti_dma_xbar_match),
402         },
403         .probe  = ti_dma_xbar_probe,
404 };
405
406 int omap_dmaxbar_init(void)
407 {
408         return platform_driver_register(&ti_dma_xbar_driver);
409 }
410 arch_initcall(omap_dmaxbar_init);