blob: e550ccb7634e91d312cd9377ff499499d37e38ed [file] [log] [blame]
Hiroshi Doyu4e0ee782012-06-25 14:23:54 +03001/*
2 * OF helpers for IOMMU
3 *
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include <linux/export.h>
21#include <linux/limits.h>
22#include <linux/of.h>
Brian Norriscbff5632013-12-04 17:22:53 -080023#include <linux/of_iommu.h>
Hiroshi Doyu4e0ee782012-06-25 14:23:54 +030024
25/**
26 * of_get_dma_window - Parse *dma-window property and returns 0 if found.
27 *
28 * @dn: device node
29 * @prefix: prefix for property name if any
30 * @index: index to start to parse
31 * @busno: Returns busno if supported. Otherwise pass NULL
32 * @addr: Returns address that DMA starts
33 * @size: Returns the range that DMA can handle
34 *
35 * This supports different formats flexibly. "prefix" can be
36 * configured if any. "busno" and "index" are optionally
37 * specified. Set 0(or NULL) if not used.
38 */
39int of_get_dma_window(struct device_node *dn, const char *prefix, int index,
40 unsigned long *busno, dma_addr_t *addr, size_t *size)
41{
42 const __be32 *dma_window, *end;
43 int bytes, cur_index = 0;
44 char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX];
45
46 if (!dn || !addr || !size)
47 return -EINVAL;
48
49 if (!prefix)
50 prefix = "";
51
52 snprintf(propname, sizeof(propname), "%sdma-window", prefix);
53 snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix);
54 snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix);
55
56 dma_window = of_get_property(dn, propname, &bytes);
57 if (!dma_window)
58 return -ENODEV;
59 end = dma_window + bytes / sizeof(*dma_window);
60
61 while (dma_window < end) {
62 u32 cells;
63 const void *prop;
64
65 /* busno is one cell if supported */
66 if (busno)
67 *busno = be32_to_cpup(dma_window++);
68
69 prop = of_get_property(dn, addrname, NULL);
70 if (!prop)
71 prop = of_get_property(dn, "#address-cells", NULL);
72
73 cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn);
74 if (!cells)
75 return -EINVAL;
76 *addr = of_read_number(dma_window, cells);
77 dma_window += cells;
78
79 prop = of_get_property(dn, sizename, NULL);
80 cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn);
81 if (!cells)
82 return -EINVAL;
83 *size = of_read_number(dma_window, cells);
84 dma_window += cells;
85
86 if (cur_index++ == index)
87 break;
88 }
89 return 0;
90}
91EXPORT_SYMBOL_GPL(of_get_dma_window);