GNU Linux-libre 4.9.328-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/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/of_dma.h>
18
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,
24 };
25
26 static const struct of_device_id ti_dma_xbar_match[] = {
27         {
28                 .compatible = "ti,dra7-dma-crossbar",
29                 .data = &ti_xbar_type[TI_XBAR_DRA7],
30         },
31         {
32                 .compatible = "ti,am335x-edma-crossbar",
33                 .data = &ti_xbar_type[TI_XBAR_AM335X],
34         },
35         {},
36 };
37
38 /* Crossbar on AM335x/AM437x family */
39 #define TI_AM335X_XBAR_LINES    64
40
41 struct ti_am335x_xbar_data {
42         void __iomem *iomem;
43
44         struct dma_router dmarouter;
45
46         u32 xbar_events; /* maximum number of events to select in xbar */
47         u32 dma_requests; /* number of DMA requests on eDMA */
48 };
49
50 struct ti_am335x_xbar_map {
51         u16 dma_line;
52         u8 mux_val;
53 };
54
55 static inline void ti_am335x_xbar_write(void __iomem *iomem, int event, u8 val)
56 {
57         /*
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.
61          */
62         if (event >= 60 && event <= 63)
63                 writeb_relaxed(val, iomem + (63 - event % 4));
64         else
65                 writeb_relaxed(val, iomem + event);
66 }
67
68 static void ti_am335x_xbar_free(struct device *dev, void *route_data)
69 {
70         struct ti_am335x_xbar_data *xbar = dev_get_drvdata(dev);
71         struct ti_am335x_xbar_map *map = route_data;
72
73         dev_dbg(dev, "Unmapping XBAR event %u on channel %u\n",
74                 map->mux_val, map->dma_line);
75
76         ti_am335x_xbar_write(xbar->iomem, map->dma_line, 0);
77         kfree(map);
78 }
79
80 static void *ti_am335x_xbar_route_allocate(struct of_phandle_args *dma_spec,
81                                            struct of_dma *ofdma)
82 {
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;
86
87         if (dma_spec->args_count != 3)
88                 return ERR_PTR(-EINVAL);
89
90         if (dma_spec->args[2] >= xbar->xbar_events) {
91                 dev_err(&pdev->dev, "Invalid XBAR event number: %d\n",
92                         dma_spec->args[2]);
93                 return ERR_PTR(-EINVAL);
94         }
95
96         if (dma_spec->args[0] >= xbar->dma_requests) {
97                 dev_err(&pdev->dev, "Invalid DMA request line number: %d\n",
98                         dma_spec->args[0]);
99                 return ERR_PTR(-EINVAL);
100         }
101
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);
104         if (!dma_spec->np) {
105                 dev_err(&pdev->dev, "Can't get DMA master\n");
106                 return ERR_PTR(-EINVAL);
107         }
108
109         map = kzalloc(sizeof(*map), GFP_KERNEL);
110         if (!map) {
111                 of_node_put(dma_spec->np);
112                 return ERR_PTR(-ENOMEM);
113         }
114
115         map->dma_line = (u16)dma_spec->args[0];
116         map->mux_val = (u8)dma_spec->args[2];
117
118         dma_spec->args[2] = 0;
119         dma_spec->args_count = 2;
120
121         dev_dbg(&pdev->dev, "Mapping XBAR event%u to DMA%u\n",
122                 map->mux_val, map->dma_line);
123
124         ti_am335x_xbar_write(xbar->iomem, map->dma_line, map->mux_val);
125
126         return map;
127 }
128
129 static const struct of_device_id ti_am335x_master_match[] = {
130         { .compatible = "ti,edma3-tpcc", },
131         {},
132 };
133
134 static int ti_am335x_xbar_probe(struct platform_device *pdev)
135 {
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;
141         void __iomem *iomem;
142         int i, ret;
143
144         if (!node)
145                 return -ENODEV;
146
147         xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
148         if (!xbar)
149                 return -ENOMEM;
150
151         dma_node = of_parse_phandle(node, "dma-masters", 0);
152         if (!dma_node) {
153                 dev_err(&pdev->dev, "Can't get DMA master node\n");
154                 return -ENODEV;
155         }
156
157         match = of_match_node(ti_am335x_master_match, dma_node);
158         if (!match) {
159                 dev_err(&pdev->dev, "DMA master is not supported\n");
160                 of_node_put(dma_node);
161                 return -EINVAL;
162         }
163
164         if (of_property_read_u32(dma_node, "dma-requests",
165                                  &xbar->dma_requests)) {
166                 dev_info(&pdev->dev,
167                          "Missing XBAR output information, using %u.\n",
168                          TI_AM335X_XBAR_LINES);
169                 xbar->dma_requests = TI_AM335X_XBAR_LINES;
170         }
171         of_node_put(dma_node);
172
173         if (of_property_read_u32(node, "dma-requests", &xbar->xbar_events)) {
174                 dev_info(&pdev->dev,
175                          "Missing XBAR input information, using %u.\n",
176                          TI_AM335X_XBAR_LINES);
177                 xbar->xbar_events = TI_AM335X_XBAR_LINES;
178         }
179
180         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
181         iomem = devm_ioremap_resource(&pdev->dev, res);
182         if (IS_ERR(iomem))
183                 return PTR_ERR(iomem);
184
185         xbar->iomem = iomem;
186
187         xbar->dmarouter.dev = &pdev->dev;
188         xbar->dmarouter.route_free = ti_am335x_xbar_free;
189
190         platform_set_drvdata(pdev, xbar);
191
192         /* Reset the crossbar */
193         for (i = 0; i < xbar->dma_requests; i++)
194                 ti_am335x_xbar_write(xbar->iomem, i, 0);
195
196         ret = of_dma_router_register(node, ti_am335x_xbar_route_allocate,
197                                      &xbar->dmarouter);
198
199         return ret;
200 }
201
202 /* Crossbar on DRA7xx family */
203 #define TI_DRA7_XBAR_OUTPUTS    127
204 #define TI_DRA7_XBAR_INPUTS     256
205
206 struct ti_dra7_xbar_data {
207         void __iomem *iomem;
208
209         struct dma_router dmarouter;
210         struct mutex mutex;
211         unsigned long *dma_inuse;
212
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 */
216         u32 dma_offset;
217 };
218
219 struct ti_dra7_xbar_map {
220         u16 xbar_in;
221         int xbar_out;
222 };
223
224 static inline void ti_dra7_xbar_write(void __iomem *iomem, int xbar, u16 val)
225 {
226         writew_relaxed(val, iomem + (xbar * 2));
227 }
228
229 static void ti_dra7_xbar_free(struct device *dev, void *route_data)
230 {
231         struct ti_dra7_xbar_data *xbar = dev_get_drvdata(dev);
232         struct ti_dra7_xbar_map *map = route_data;
233
234         dev_dbg(dev, "Unmapping XBAR%u (was routed to %d)\n",
235                 map->xbar_in, map->xbar_out);
236
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);
241         kfree(map);
242 }
243
244 static void *ti_dra7_xbar_route_allocate(struct of_phandle_args *dma_spec,
245                                          struct of_dma *ofdma)
246 {
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;
250
251         if (dma_spec->args[0] >= xbar->xbar_requests) {
252                 dev_err(&pdev->dev, "Invalid XBAR request number: %d\n",
253                         dma_spec->args[0]);
254                 put_device(&pdev->dev);
255                 return ERR_PTR(-EINVAL);
256         }
257
258         /* The of_node_put() will be done in the core for the node */
259         dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
260         if (!dma_spec->np) {
261                 dev_err(&pdev->dev, "Can't get DMA master\n");
262                 put_device(&pdev->dev);
263                 return ERR_PTR(-EINVAL);
264         }
265
266         map = kzalloc(sizeof(*map), GFP_KERNEL);
267         if (!map) {
268                 of_node_put(dma_spec->np);
269                 put_device(&pdev->dev);
270                 return ERR_PTR(-ENOMEM);
271         }
272
273         mutex_lock(&xbar->mutex);
274         map->xbar_out = find_first_zero_bit(xbar->dma_inuse,
275                                             xbar->dma_requests);
276         if (map->xbar_out == xbar->dma_requests) {
277                 mutex_unlock(&xbar->mutex);
278                 dev_err(&pdev->dev, "Run out of free DMA requests\n");
279                 kfree(map);
280                 of_node_put(dma_spec->np);
281                 put_device(&pdev->dev);
282                 return ERR_PTR(-ENOMEM);
283         }
284         set_bit(map->xbar_out, xbar->dma_inuse);
285         mutex_unlock(&xbar->mutex);
286
287         map->xbar_in = (u16)dma_spec->args[0];
288
289         dma_spec->args[0] = map->xbar_out + xbar->dma_offset;
290
291         dev_dbg(&pdev->dev, "Mapping XBAR%u to DMA%d\n",
292                 map->xbar_in, map->xbar_out);
293
294         ti_dra7_xbar_write(xbar->iomem, map->xbar_out, map->xbar_in);
295
296         return map;
297 }
298
299 #define TI_XBAR_EDMA_OFFSET     0
300 #define TI_XBAR_SDMA_OFFSET     1
301 static const u32 ti_dma_offset[] = {
302         [TI_XBAR_EDMA_OFFSET] = 0,
303         [TI_XBAR_SDMA_OFFSET] = 1,
304 };
305
306 static const struct of_device_id ti_dra7_master_match[] = {
307         {
308                 .compatible = "ti,omap4430-sdma",
309                 .data = &ti_dma_offset[TI_XBAR_SDMA_OFFSET],
310         },
311         {
312                 .compatible = "ti,edma3",
313                 .data = &ti_dma_offset[TI_XBAR_EDMA_OFFSET],
314         },
315         {
316                 .compatible = "ti,edma3-tpcc",
317                 .data = &ti_dma_offset[TI_XBAR_EDMA_OFFSET],
318         },
319         {},
320 };
321
322 static inline void ti_dra7_xbar_reserve(int offset, int len, unsigned long *p)
323 {
324         for (; len > 0; len--)
325                 clear_bit(offset + (len - 1), p);
326 }
327
328 static int ti_dra7_xbar_probe(struct platform_device *pdev)
329 {
330         struct device_node *node = pdev->dev.of_node;
331         const struct of_device_id *match;
332         struct device_node *dma_node;
333         struct ti_dra7_xbar_data *xbar;
334         struct property *prop;
335         struct resource *res;
336         u32 safe_val;
337         int sz;
338         void __iomem *iomem;
339         int i, ret;
340
341         if (!node)
342                 return -ENODEV;
343
344         xbar = devm_kzalloc(&pdev->dev, sizeof(*xbar), GFP_KERNEL);
345         if (!xbar)
346                 return -ENOMEM;
347
348         dma_node = of_parse_phandle(node, "dma-masters", 0);
349         if (!dma_node) {
350                 dev_err(&pdev->dev, "Can't get DMA master node\n");
351                 return -ENODEV;
352         }
353
354         match = of_match_node(ti_dra7_master_match, dma_node);
355         if (!match) {
356                 dev_err(&pdev->dev, "DMA master is not supported\n");
357                 of_node_put(dma_node);
358                 return -EINVAL;
359         }
360
361         if (of_property_read_u32(dma_node, "dma-requests",
362                                  &xbar->dma_requests)) {
363                 dev_info(&pdev->dev,
364                          "Missing XBAR output information, using %u.\n",
365                          TI_DRA7_XBAR_OUTPUTS);
366                 xbar->dma_requests = TI_DRA7_XBAR_OUTPUTS;
367         }
368         of_node_put(dma_node);
369
370         xbar->dma_inuse = devm_kcalloc(&pdev->dev,
371                                        BITS_TO_LONGS(xbar->dma_requests),
372                                        sizeof(unsigned long), GFP_KERNEL);
373         if (!xbar->dma_inuse)
374                 return -ENOMEM;
375
376         if (of_property_read_u32(node, "dma-requests", &xbar->xbar_requests)) {
377                 dev_info(&pdev->dev,
378                          "Missing XBAR input information, using %u.\n",
379                          TI_DRA7_XBAR_INPUTS);
380                 xbar->xbar_requests = TI_DRA7_XBAR_INPUTS;
381         }
382
383         if (!of_property_read_u32(node, "ti,dma-safe-map", &safe_val))
384                 xbar->safe_val = (u16)safe_val;
385
386
387         prop = of_find_property(node, "ti,reserved-dma-request-ranges", &sz);
388         if (prop) {
389                 const char pname[] = "ti,reserved-dma-request-ranges";
390                 u32 (*rsv_events)[2];
391                 size_t nelm = sz / sizeof(*rsv_events);
392                 int i;
393
394                 if (!nelm)
395                         return -EINVAL;
396
397                 rsv_events = kcalloc(nelm, sizeof(*rsv_events), GFP_KERNEL);
398                 if (!rsv_events)
399                         return -ENOMEM;
400
401                 ret = of_property_read_u32_array(node, pname, (u32 *)rsv_events,
402                                                  nelm * 2);
403                 if (ret) {
404                         kfree(rsv_events);
405                         return ret;
406                 }
407
408                 for (i = 0; i < nelm; i++) {
409                         ti_dra7_xbar_reserve(rsv_events[i][0], rsv_events[i][1],
410                                              xbar->dma_inuse);
411                 }
412                 kfree(rsv_events);
413         }
414
415         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
416         iomem = devm_ioremap_resource(&pdev->dev, res);
417         if (IS_ERR(iomem))
418                 return PTR_ERR(iomem);
419
420         xbar->iomem = iomem;
421
422         xbar->dmarouter.dev = &pdev->dev;
423         xbar->dmarouter.route_free = ti_dra7_xbar_free;
424         xbar->dma_offset = *(u32 *)match->data;
425
426         mutex_init(&xbar->mutex);
427         platform_set_drvdata(pdev, xbar);
428
429         /* Reset the crossbar */
430         for (i = 0; i < xbar->dma_requests; i++) {
431                 if (!test_bit(i, xbar->dma_inuse))
432                         ti_dra7_xbar_write(xbar->iomem, i, xbar->safe_val);
433         }
434
435         ret = of_dma_router_register(node, ti_dra7_xbar_route_allocate,
436                                      &xbar->dmarouter);
437         if (ret) {
438                 /* Restore the defaults for the crossbar */
439                 for (i = 0; i < xbar->dma_requests; i++) {
440                         if (!test_bit(i, xbar->dma_inuse))
441                                 ti_dra7_xbar_write(xbar->iomem, i, i);
442                 }
443         }
444
445         return ret;
446 }
447
448 static int ti_dma_xbar_probe(struct platform_device *pdev)
449 {
450         const struct of_device_id *match;
451         int ret;
452
453         match = of_match_node(ti_dma_xbar_match, pdev->dev.of_node);
454         if (unlikely(!match))
455                 return -EINVAL;
456
457         switch (*(u32 *)match->data) {
458         case TI_XBAR_DRA7:
459                 ret = ti_dra7_xbar_probe(pdev);
460                 break;
461         case TI_XBAR_AM335X:
462                 ret = ti_am335x_xbar_probe(pdev);
463                 break;
464         default:
465                 dev_err(&pdev->dev, "Unsupported crossbar\n");
466                 ret = -ENODEV;
467                 break;
468         }
469
470         return ret;
471 }
472
473 static struct platform_driver ti_dma_xbar_driver = {
474         .driver = {
475                 .name = "ti-dma-crossbar",
476                 .of_match_table = of_match_ptr(ti_dma_xbar_match),
477         },
478         .probe  = ti_dma_xbar_probe,
479 };
480
481 static int omap_dmaxbar_init(void)
482 {
483         return platform_driver_register(&ti_dma_xbar_driver);
484 }
485 arch_initcall(omap_dmaxbar_init);