18c2ecf20Sopenharmony_ci/* Copyright 2017 NXP Semiconductor, Inc.
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
48c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
58c2ecf20Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
68c2ecf20Sopenharmony_ci *	 notice, this list of conditions and the following disclaimer.
78c2ecf20Sopenharmony_ci *     * Redistributions in binary form must reproduce the above copyright
88c2ecf20Sopenharmony_ci *	 notice, this list of conditions and the following disclaimer in the
98c2ecf20Sopenharmony_ci *	 documentation and/or other materials provided with the distribution.
108c2ecf20Sopenharmony_ci *     * Neither the name of NXP Semiconductor nor the
118c2ecf20Sopenharmony_ci *	 names of its contributors may be used to endorse or promote products
128c2ecf20Sopenharmony_ci *	 derived from this software without specific prior written permission.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * ALTERNATIVELY, this software may be distributed under the terms of the
158c2ecf20Sopenharmony_ci * GNU General Public License ("GPL") as published by the Free Software
168c2ecf20Sopenharmony_ci * Foundation, either version 2 of that License or (at your option) any
178c2ecf20Sopenharmony_ci * later version.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY NXP Semiconductor ``AS IS'' AND ANY
208c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
218c2ecf20Sopenharmony_ci * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
228c2ecf20Sopenharmony_ci * DISCLAIMED. IN NO EVENT SHALL NXP Semiconductor BE LIABLE FOR ANY
238c2ecf20Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
248c2ecf20Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
258c2ecf20Sopenharmony_ci * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
268c2ecf20Sopenharmony_ci * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
288c2ecf20Sopenharmony_ci * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
328c2ecf20Sopenharmony_ci#include "dpaa_sys.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * Initialize a devices private memory region
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ciint qbman_init_private_mem(struct device *dev, int idx, dma_addr_t *addr,
388c2ecf20Sopenharmony_ci				size_t *size)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct device_node *mem_node;
418c2ecf20Sopenharmony_ci	struct reserved_mem *rmem;
428c2ecf20Sopenharmony_ci	struct property *prop;
438c2ecf20Sopenharmony_ci	int len, err;
448c2ecf20Sopenharmony_ci	__be32 *res_array;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	mem_node = of_parse_phandle(dev->of_node, "memory-region", idx);
478c2ecf20Sopenharmony_ci	if (!mem_node) {
488c2ecf20Sopenharmony_ci		dev_err(dev, "No memory-region found for index %d\n", idx);
498c2ecf20Sopenharmony_ci		return -ENODEV;
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	rmem = of_reserved_mem_lookup(mem_node);
538c2ecf20Sopenharmony_ci	if (!rmem) {
548c2ecf20Sopenharmony_ci		dev_err(dev, "of_reserved_mem_lookup() returned NULL\n");
558c2ecf20Sopenharmony_ci		return -ENODEV;
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci	*addr = rmem->base;
588c2ecf20Sopenharmony_ci	*size = rmem->size;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/*
618c2ecf20Sopenharmony_ci	 * Check if the reg property exists - if not insert the node
628c2ecf20Sopenharmony_ci	 * so upon kexec() the same memory region address will be preserved.
638c2ecf20Sopenharmony_ci	 * This is needed because QBMan HW does not allow the base address/
648c2ecf20Sopenharmony_ci	 * size to be modified once set.
658c2ecf20Sopenharmony_ci	 */
668c2ecf20Sopenharmony_ci	prop = of_find_property(mem_node, "reg", &len);
678c2ecf20Sopenharmony_ci	if (!prop) {
688c2ecf20Sopenharmony_ci		prop = devm_kzalloc(dev, sizeof(*prop), GFP_KERNEL);
698c2ecf20Sopenharmony_ci		if (!prop)
708c2ecf20Sopenharmony_ci			return -ENOMEM;
718c2ecf20Sopenharmony_ci		prop->value = res_array = devm_kzalloc(dev, sizeof(__be32) * 4,
728c2ecf20Sopenharmony_ci						       GFP_KERNEL);
738c2ecf20Sopenharmony_ci		if (!prop->value)
748c2ecf20Sopenharmony_ci			return -ENOMEM;
758c2ecf20Sopenharmony_ci		res_array[0] = cpu_to_be32(upper_32_bits(*addr));
768c2ecf20Sopenharmony_ci		res_array[1] = cpu_to_be32(lower_32_bits(*addr));
778c2ecf20Sopenharmony_ci		res_array[2] = cpu_to_be32(upper_32_bits(*size));
788c2ecf20Sopenharmony_ci		res_array[3] = cpu_to_be32(lower_32_bits(*size));
798c2ecf20Sopenharmony_ci		prop->length = sizeof(__be32) * 4;
808c2ecf20Sopenharmony_ci		prop->name = devm_kstrdup(dev, "reg", GFP_KERNEL);
818c2ecf20Sopenharmony_ci		if (!prop->name)
828c2ecf20Sopenharmony_ci			return -ENOMEM;
838c2ecf20Sopenharmony_ci		err = of_add_property(mem_node, prop);
848c2ecf20Sopenharmony_ci		if (err)
858c2ecf20Sopenharmony_ci			return err;
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return 0;
898c2ecf20Sopenharmony_ci}
90