of: introduce of_dma_get_range() helper
authorGrygorii Strashko <grygorii.strashko@ti.com>
Thu, 24 Apr 2014 15:30:02 +0000 (11:30 -0400)
committerSantosh Shilimkar <santosh.shilimkar@ti.com>
Mon, 5 May 2014 22:11:13 +0000 (18:11 -0400)
The of_dma_get_range() allows to find "dma-range" property for
the specified device and parse it.
 dma-ranges format:
   DMA addr (dma_addr)          : naddr cells
   CPU addr (phys_addr_t)       : pna cells
   size                         : nsize cells

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Russell King <linux@arm.linux.org.uk>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Olof Johansson <olof@lixom.net>
Cc: Grant Likely <grant.likely@linaro.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Grygorii Strashko <grygorii.strashko@ti.com>
Signed-off-by: Santosh Shilimkar <santosh.shilimkar@ti.com>
drivers/of/address.c
include/linux/of_address.h

index cb4242a69cd529b513a8d339dd45ff96076da49b..c54baee87d93aba6e3e5a5879251dae750a36dad 100644 (file)
@@ -721,3 +721,90 @@ void __iomem *of_iomap(struct device_node *np, int index)
        return ioremap(res.start, resource_size(&res));
 }
 EXPORT_SYMBOL(of_iomap);
+
+/**
+ * of_dma_get_range - Get DMA range info
+ * @np:                device node to get DMA range info
+ * @dma_addr:  pointer to store initial DMA address of DMA range
+ * @paddr:     pointer to store initial CPU address of DMA range
+ * @size:      pointer to store size of DMA range
+ *
+ * Look in bottom up direction for the first "dma-ranges" property
+ * and parse it.
+ *  dma-ranges format:
+ *     DMA addr (dma_addr)     : naddr cells
+ *     CPU addr (phys_addr_t)  : pna cells
+ *     size                    : nsize cells
+ *
+ * It returns -ENODEV if "dma-ranges" property was not found
+ * for this device in DT.
+ */
+int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size)
+{
+       struct device_node *node = of_node_get(np);
+       const __be32 *ranges = NULL;
+       int len, naddr, nsize, pna;
+       int ret = 0;
+       u64 dmaaddr;
+
+       if (!node)
+               return -EINVAL;
+
+       while (1) {
+               naddr = of_n_addr_cells(node);
+               nsize = of_n_size_cells(node);
+               node = of_get_next_parent(node);
+               if (!node)
+                       break;
+
+               ranges = of_get_property(node, "dma-ranges", &len);
+
+               /* Ignore empty ranges, they imply no translation required */
+               if (ranges && len > 0)
+                       break;
+
+               /*
+                * At least empty ranges has to be defined for parent node if
+                * DMA is supported
+                */
+               if (!ranges)
+                       break;
+       }
+
+       if (!ranges) {
+               pr_debug("%s: no dma-ranges found for node(%s)\n",
+                        __func__, np->full_name);
+               ret = -ENODEV;
+               goto out;
+       }
+
+       len /= sizeof(u32);
+
+       pna = of_n_addr_cells(node);
+
+       /* dma-ranges format:
+        * DMA addr     : naddr cells
+        * CPU addr     : pna cells
+        * size         : nsize cells
+        */
+       dmaaddr = of_read_number(ranges, naddr);
+       *paddr = of_translate_dma_address(np, ranges);
+       if (*paddr == OF_BAD_ADDR) {
+               pr_err("%s: translation of DMA address(%pad) to CPU address failed node(%s)\n",
+                      __func__, dma_addr, np->full_name);
+               ret = -EINVAL;
+               goto out;
+       }
+       *dma_addr = dmaaddr;
+
+       *size = of_read_number(ranges + naddr + pna, nsize);
+
+       pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
+                *dma_addr, *paddr, *size);
+
+out:
+       of_node_put(node);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(of_dma_get_range);
index 5f6ed6b182b8ef9c7e01930288330d324f7f57b1..4d7b325af2ca49f68096b3d8e41ad5b4f6818dd0 100644 (file)
@@ -63,6 +63,8 @@ extern int of_pci_range_parser_init(struct of_pci_range_parser *parser,
 extern struct of_pci_range *of_pci_range_parser_one(
                                        struct of_pci_range_parser *parser,
                                        struct of_pci_range *range);
+extern int of_dma_get_range(struct device_node *np, u64 *dma_addr,
+                               u64 *paddr, u64 *size);
 #else /* CONFIG_OF_ADDRESS */
 static inline struct device_node *of_find_matching_node_by_address(
                                        struct device_node *from,
@@ -90,6 +92,12 @@ static inline struct of_pci_range *of_pci_range_parser_one(
 {
        return NULL;
 }
+
+static inline int of_dma_get_range(struct device_node *np, u64 *dma_addr,
+                               u64 *paddr, u64 *size)
+{
+       return -ENODEV;
+}
 #endif /* CONFIG_OF_ADDRESS */
 
 #ifdef CONFIG_OF