1 /* Copyright 2017 NXP Semiconductor, Inc.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 * * Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * * Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
10 * * Neither the name of NXP Semiconductor nor the
11 * names of its contributors may be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 *
14 * ALTERNATIVELY, this software may be distributed under the terms of the
15 * GNU General Public License ("GPL") as published by the Free Software
16 * Foundation, either version 2 of that License or (at your option) any
17 * later version.
18 *
19 * THIS SOFTWARE IS PROVIDED BY NXP Semiconductor ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL NXP Semiconductor BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <linux/dma-mapping.h>
32 #include "dpaa_sys.h"
33
34 /*
35 * Initialize a devices private memory region
36 */
qbman_init_private_mem(struct device * dev,int idx,const char * compat,dma_addr_t * addr,size_t * size)37 int qbman_init_private_mem(struct device *dev, int idx, const char *compat,
38 dma_addr_t *addr, size_t *size)
39 {
40 struct device_node *mem_node;
41 struct reserved_mem *rmem;
42 int err;
43 __be32 *res_array;
44
45 mem_node = of_parse_phandle(dev->of_node, "memory-region", idx);
46 if (!mem_node) {
47 mem_node = of_find_compatible_node(NULL, NULL, compat);
48 if (!mem_node) {
49 dev_err(dev, "No memory-region found for index %d or compatible '%s'\n",
50 idx, compat);
51 return -ENODEV;
52 }
53 }
54
55 rmem = of_reserved_mem_lookup(mem_node);
56 if (!rmem) {
57 dev_err(dev, "of_reserved_mem_lookup() returned NULL\n");
58 return -ENODEV;
59 }
60 *addr = rmem->base;
61 *size = rmem->size;
62
63 /*
64 * Check if the reg property exists - if not insert the node
65 * so upon kexec() the same memory region address will be preserved.
66 * This is needed because QBMan HW does not allow the base address/
67 * size to be modified once set.
68 */
69 if (!of_property_present(mem_node, "reg")) {
70 struct property *prop;
71
72 prop = devm_kzalloc(dev, sizeof(*prop), GFP_KERNEL);
73 if (!prop)
74 return -ENOMEM;
75 prop->value = res_array = devm_kzalloc(dev, sizeof(__be32) * 4,
76 GFP_KERNEL);
77 if (!prop->value)
78 return -ENOMEM;
79 res_array[0] = cpu_to_be32(upper_32_bits(*addr));
80 res_array[1] = cpu_to_be32(lower_32_bits(*addr));
81 res_array[2] = cpu_to_be32(upper_32_bits(*size));
82 res_array[3] = cpu_to_be32(lower_32_bits(*size));
83 prop->length = sizeof(__be32) * 4;
84 prop->name = devm_kstrdup(dev, "reg", GFP_KERNEL);
85 if (!prop->name)
86 return -ENOMEM;
87 err = of_add_property(mem_node, prop);
88 if (err)
89 return err;
90 }
91
92 return 0;
93 }
94