162306a36Sopenharmony_ci/* Copyright 2017 NXP Semiconductor, Inc.
262306a36Sopenharmony_ci *
362306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
462306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
562306a36Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
662306a36Sopenharmony_ci *	 notice, this list of conditions and the following disclaimer.
762306a36Sopenharmony_ci *     * Redistributions in binary form must reproduce the above copyright
862306a36Sopenharmony_ci *	 notice, this list of conditions and the following disclaimer in the
962306a36Sopenharmony_ci *	 documentation and/or other materials provided with the distribution.
1062306a36Sopenharmony_ci *     * Neither the name of NXP Semiconductor nor the
1162306a36Sopenharmony_ci *	 names of its contributors may be used to endorse or promote products
1262306a36Sopenharmony_ci *	 derived from this software without specific prior written permission.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * ALTERNATIVELY, this software may be distributed under the terms of the
1562306a36Sopenharmony_ci * GNU General Public License ("GPL") as published by the Free Software
1662306a36Sopenharmony_ci * Foundation, either version 2 of that License or (at your option) any
1762306a36Sopenharmony_ci * later version.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY NXP Semiconductor ``AS IS'' AND ANY
2062306a36Sopenharmony_ci * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2162306a36Sopenharmony_ci * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2262306a36Sopenharmony_ci * DISCLAIMED. IN NO EVENT SHALL NXP Semiconductor BE LIABLE FOR ANY
2362306a36Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2462306a36Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2562306a36Sopenharmony_ci * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2662306a36Sopenharmony_ci * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2762306a36Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2862306a36Sopenharmony_ci * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#include <linux/dma-mapping.h>
3262306a36Sopenharmony_ci#include "dpaa_sys.h"
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/*
3562306a36Sopenharmony_ci * Initialize a devices private memory region
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_ciint qbman_init_private_mem(struct device *dev, int idx, dma_addr_t *addr,
3862306a36Sopenharmony_ci				size_t *size)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	struct device_node *mem_node;
4162306a36Sopenharmony_ci	struct reserved_mem *rmem;
4262306a36Sopenharmony_ci	int err;
4362306a36Sopenharmony_ci	__be32 *res_array;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	mem_node = of_parse_phandle(dev->of_node, "memory-region", idx);
4662306a36Sopenharmony_ci	if (!mem_node) {
4762306a36Sopenharmony_ci		dev_err(dev, "No memory-region found for index %d\n", idx);
4862306a36Sopenharmony_ci		return -ENODEV;
4962306a36Sopenharmony_ci	}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	rmem = of_reserved_mem_lookup(mem_node);
5262306a36Sopenharmony_ci	if (!rmem) {
5362306a36Sopenharmony_ci		dev_err(dev, "of_reserved_mem_lookup() returned NULL\n");
5462306a36Sopenharmony_ci		return -ENODEV;
5562306a36Sopenharmony_ci	}
5662306a36Sopenharmony_ci	*addr = rmem->base;
5762306a36Sopenharmony_ci	*size = rmem->size;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	/*
6062306a36Sopenharmony_ci	 * Check if the reg property exists - if not insert the node
6162306a36Sopenharmony_ci	 * so upon kexec() the same memory region address will be preserved.
6262306a36Sopenharmony_ci	 * This is needed because QBMan HW does not allow the base address/
6362306a36Sopenharmony_ci	 * size to be modified once set.
6462306a36Sopenharmony_ci	 */
6562306a36Sopenharmony_ci	if (!of_property_present(mem_node, "reg")) {
6662306a36Sopenharmony_ci		struct property *prop;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci		prop = devm_kzalloc(dev, sizeof(*prop), GFP_KERNEL);
6962306a36Sopenharmony_ci		if (!prop)
7062306a36Sopenharmony_ci			return -ENOMEM;
7162306a36Sopenharmony_ci		prop->value = res_array = devm_kzalloc(dev, sizeof(__be32) * 4,
7262306a36Sopenharmony_ci						       GFP_KERNEL);
7362306a36Sopenharmony_ci		if (!prop->value)
7462306a36Sopenharmony_ci			return -ENOMEM;
7562306a36Sopenharmony_ci		res_array[0] = cpu_to_be32(upper_32_bits(*addr));
7662306a36Sopenharmony_ci		res_array[1] = cpu_to_be32(lower_32_bits(*addr));
7762306a36Sopenharmony_ci		res_array[2] = cpu_to_be32(upper_32_bits(*size));
7862306a36Sopenharmony_ci		res_array[3] = cpu_to_be32(lower_32_bits(*size));
7962306a36Sopenharmony_ci		prop->length = sizeof(__be32) * 4;
8062306a36Sopenharmony_ci		prop->name = devm_kstrdup(dev, "reg", GFP_KERNEL);
8162306a36Sopenharmony_ci		if (!prop->name)
8262306a36Sopenharmony_ci			return -ENOMEM;
8362306a36Sopenharmony_ci		err = of_add_property(mem_node, prop);
8462306a36Sopenharmony_ci		if (err)
8562306a36Sopenharmony_ci			return err;
8662306a36Sopenharmony_ci	}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	return 0;
8962306a36Sopenharmony_ci}
90