18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015-2016 Socionext Inc.
48c2ecf20Sopenharmony_ci *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#define pr_fmt(fmt)		"uniphier: " fmt
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/bitops.h>
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/log2.h>
138c2ecf20Sopenharmony_ci#include <linux/of_address.h>
148c2ecf20Sopenharmony_ci#include <linux/slab.h>
158c2ecf20Sopenharmony_ci#include <asm/hardware/cache-uniphier.h>
168c2ecf20Sopenharmony_ci#include <asm/outercache.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* control registers */
198c2ecf20Sopenharmony_ci#define UNIPHIER_SSCC		0x0	/* Control Register */
208c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCC_BST			BIT(20)	/* UCWG burst read */
218c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCC_ACT			BIT(19)	/* Inst-Data separate */
228c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCC_WTG			BIT(18)	/* WT gathering on */
238c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCC_PRD			BIT(17)	/* enable pre-fetch */
248c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCC_ON			BIT(0)	/* enable cache */
258c2ecf20Sopenharmony_ci#define UNIPHIER_SSCLPDAWCR	0x30	/* Unified/Data Active Way Control */
268c2ecf20Sopenharmony_ci#define UNIPHIER_SSCLPIAWCR	0x34	/* Instruction Active Way Control */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* revision registers */
298c2ecf20Sopenharmony_ci#define UNIPHIER_SSCID		0x0	/* ID Register */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* operation registers */
328c2ecf20Sopenharmony_ci#define UNIPHIER_SSCOPE		0x244	/* Cache Operation Primitive Entry */
338c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOPE_CM_INV		0x0	/* invalidate */
348c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOPE_CM_CLEAN		0x1	/* clean */
358c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOPE_CM_FLUSH		0x2	/* flush */
368c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOPE_CM_SYNC		0x8	/* sync (drain bufs) */
378c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOPE_CM_FLUSH_PREFETCH	0x9	/* flush p-fetch buf */
388c2ecf20Sopenharmony_ci#define UNIPHIER_SSCOQM		0x248	/* Cache Operation Queue Mode */
398c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOQM_S_MASK		(0x3 << 17)
408c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOQM_S_RANGE		(0x0 << 17)
418c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOQM_S_ALL		(0x1 << 17)
428c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOQM_CE			BIT(15)	/* notify completion */
438c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOQM_CM_INV		0x0	/* invalidate */
448c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOQM_CM_CLEAN		0x1	/* clean */
458c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOQM_CM_FLUSH		0x2	/* flush */
468c2ecf20Sopenharmony_ci#define UNIPHIER_SSCOQAD	0x24c	/* Cache Operation Queue Address */
478c2ecf20Sopenharmony_ci#define UNIPHIER_SSCOQSZ	0x250	/* Cache Operation Queue Size */
488c2ecf20Sopenharmony_ci#define UNIPHIER_SSCOPPQSEF	0x25c	/* Cache Operation Queue Set Complete*/
498c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOPPQSEF_FE		BIT(1)
508c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOPPQSEF_OE		BIT(0)
518c2ecf20Sopenharmony_ci#define UNIPHIER_SSCOLPQS	0x260	/* Cache Operation Queue Status */
528c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOLPQS_EF			BIT(2)
538c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOLPQS_EST		BIT(1)
548c2ecf20Sopenharmony_ci#define    UNIPHIER_SSCOLPQS_QST		BIT(0)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Is the operation region specified by address range? */
578c2ecf20Sopenharmony_ci#define UNIPHIER_SSCOQM_S_IS_RANGE(op) \
588c2ecf20Sopenharmony_ci		((op & UNIPHIER_SSCOQM_S_MASK) == UNIPHIER_SSCOQM_S_RANGE)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/**
618c2ecf20Sopenharmony_ci * uniphier_cache_data - UniPhier outer cache specific data
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * @ctrl_base: virtual base address of control registers
648c2ecf20Sopenharmony_ci * @rev_base: virtual base address of revision registers
658c2ecf20Sopenharmony_ci * @op_base: virtual base address of operation registers
668c2ecf20Sopenharmony_ci * @way_mask: each bit specifies if the way is present
678c2ecf20Sopenharmony_ci * @nsets: number of associativity sets
688c2ecf20Sopenharmony_ci * @line_size: line size in bytes
698c2ecf20Sopenharmony_ci * @range_op_max_size: max size that can be handled by a single range operation
708c2ecf20Sopenharmony_ci * @list: list node to include this level in the whole cache hierarchy
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_cistruct uniphier_cache_data {
738c2ecf20Sopenharmony_ci	void __iomem *ctrl_base;
748c2ecf20Sopenharmony_ci	void __iomem *rev_base;
758c2ecf20Sopenharmony_ci	void __iomem *op_base;
768c2ecf20Sopenharmony_ci	void __iomem *way_ctrl_base;
778c2ecf20Sopenharmony_ci	u32 way_mask;
788c2ecf20Sopenharmony_ci	u32 nsets;
798c2ecf20Sopenharmony_ci	u32 line_size;
808c2ecf20Sopenharmony_ci	u32 range_op_max_size;
818c2ecf20Sopenharmony_ci	struct list_head list;
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/*
858c2ecf20Sopenharmony_ci * List of the whole outer cache hierarchy.  This list is only modified during
868c2ecf20Sopenharmony_ci * the early boot stage, so no mutex is taken for the access to the list.
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_cistatic LIST_HEAD(uniphier_cache_list);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/**
918c2ecf20Sopenharmony_ci * __uniphier_cache_sync - perform a sync point for a particular cache level
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * @data: cache controller specific data
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_cistatic void __uniphier_cache_sync(struct uniphier_cache_data *data)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	/* This sequence need not be atomic.  Do not disable IRQ. */
988c2ecf20Sopenharmony_ci	writel_relaxed(UNIPHIER_SSCOPE_CM_SYNC,
998c2ecf20Sopenharmony_ci		       data->op_base + UNIPHIER_SSCOPE);
1008c2ecf20Sopenharmony_ci	/* need a read back to confirm */
1018c2ecf20Sopenharmony_ci	readl_relaxed(data->op_base + UNIPHIER_SSCOPE);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/**
1058c2ecf20Sopenharmony_ci * __uniphier_cache_maint_common - run a queue operation for a particular level
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci * @data: cache controller specific data
1088c2ecf20Sopenharmony_ci * @start: start address of range operation (don't care for "all" operation)
1098c2ecf20Sopenharmony_ci * @size: data size of range operation (don't care for "all" operation)
1108c2ecf20Sopenharmony_ci * @operation: flags to specify the desired cache operation
1118c2ecf20Sopenharmony_ci */
1128c2ecf20Sopenharmony_cistatic void __uniphier_cache_maint_common(struct uniphier_cache_data *data,
1138c2ecf20Sopenharmony_ci					  unsigned long start,
1148c2ecf20Sopenharmony_ci					  unsigned long size,
1158c2ecf20Sopenharmony_ci					  u32 operation)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	unsigned long flags;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/*
1208c2ecf20Sopenharmony_ci	 * No spin lock is necessary here because:
1218c2ecf20Sopenharmony_ci	 *
1228c2ecf20Sopenharmony_ci	 * [1] This outer cache controller is able to accept maintenance
1238c2ecf20Sopenharmony_ci	 * operations from multiple CPUs at a time in an SMP system; if a
1248c2ecf20Sopenharmony_ci	 * maintenance operation is under way and another operation is issued,
1258c2ecf20Sopenharmony_ci	 * the new one is stored in the queue.  The controller performs one
1268c2ecf20Sopenharmony_ci	 * operation after another.  If the queue is full, the status register,
1278c2ecf20Sopenharmony_ci	 * UNIPHIER_SSCOPPQSEF, indicates that the queue registration has
1288c2ecf20Sopenharmony_ci	 * failed.  The status registers, UNIPHIER_{SSCOPPQSEF, SSCOLPQS}, have
1298c2ecf20Sopenharmony_ci	 * different instances for each CPU, i.e. each CPU can track the status
1308c2ecf20Sopenharmony_ci	 * of the maintenance operations triggered by itself.
1318c2ecf20Sopenharmony_ci	 *
1328c2ecf20Sopenharmony_ci	 * [2] The cache command registers, UNIPHIER_{SSCOQM, SSCOQAD, SSCOQSZ,
1338c2ecf20Sopenharmony_ci	 * SSCOQWN}, are shared between multiple CPUs, but the hardware still
1348c2ecf20Sopenharmony_ci	 * guarantees the registration sequence is atomic; the write access to
1358c2ecf20Sopenharmony_ci	 * them are arbitrated by the hardware.  The first accessor to the
1368c2ecf20Sopenharmony_ci	 * register, UNIPHIER_SSCOQM, holds the access right and it is released
1378c2ecf20Sopenharmony_ci	 * by reading the status register, UNIPHIER_SSCOPPQSEF.  While one CPU
1388c2ecf20Sopenharmony_ci	 * is holding the access right, other CPUs fail to register operations.
1398c2ecf20Sopenharmony_ci	 * One CPU should not hold the access right for a long time, so local
1408c2ecf20Sopenharmony_ci	 * IRQs should be disabled while the following sequence.
1418c2ecf20Sopenharmony_ci	 */
1428c2ecf20Sopenharmony_ci	local_irq_save(flags);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* clear the complete notification flag */
1458c2ecf20Sopenharmony_ci	writel_relaxed(UNIPHIER_SSCOLPQS_EF, data->op_base + UNIPHIER_SSCOLPQS);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	do {
1488c2ecf20Sopenharmony_ci		/* set cache operation */
1498c2ecf20Sopenharmony_ci		writel_relaxed(UNIPHIER_SSCOQM_CE | operation,
1508c2ecf20Sopenharmony_ci			       data->op_base + UNIPHIER_SSCOQM);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci		/* set address range if needed */
1538c2ecf20Sopenharmony_ci		if (likely(UNIPHIER_SSCOQM_S_IS_RANGE(operation))) {
1548c2ecf20Sopenharmony_ci			writel_relaxed(start, data->op_base + UNIPHIER_SSCOQAD);
1558c2ecf20Sopenharmony_ci			writel_relaxed(size, data->op_base + UNIPHIER_SSCOQSZ);
1568c2ecf20Sopenharmony_ci		}
1578c2ecf20Sopenharmony_ci	} while (unlikely(readl_relaxed(data->op_base + UNIPHIER_SSCOPPQSEF) &
1588c2ecf20Sopenharmony_ci			  (UNIPHIER_SSCOPPQSEF_FE | UNIPHIER_SSCOPPQSEF_OE)));
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* wait until the operation is completed */
1618c2ecf20Sopenharmony_ci	while (likely(readl_relaxed(data->op_base + UNIPHIER_SSCOLPQS) !=
1628c2ecf20Sopenharmony_ci		      UNIPHIER_SSCOLPQS_EF))
1638c2ecf20Sopenharmony_ci		cpu_relax();
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	local_irq_restore(flags);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic void __uniphier_cache_maint_all(struct uniphier_cache_data *data,
1698c2ecf20Sopenharmony_ci				       u32 operation)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	__uniphier_cache_maint_common(data, 0, 0,
1728c2ecf20Sopenharmony_ci				      UNIPHIER_SSCOQM_S_ALL | operation);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	__uniphier_cache_sync(data);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic void __uniphier_cache_maint_range(struct uniphier_cache_data *data,
1788c2ecf20Sopenharmony_ci					 unsigned long start, unsigned long end,
1798c2ecf20Sopenharmony_ci					 u32 operation)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	unsigned long size;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/*
1848c2ecf20Sopenharmony_ci	 * If the start address is not aligned,
1858c2ecf20Sopenharmony_ci	 * perform a cache operation for the first cache-line
1868c2ecf20Sopenharmony_ci	 */
1878c2ecf20Sopenharmony_ci	start = start & ~(data->line_size - 1);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	size = end - start;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (unlikely(size >= (unsigned long)(-data->line_size))) {
1928c2ecf20Sopenharmony_ci		/* this means cache operation for all range */
1938c2ecf20Sopenharmony_ci		__uniphier_cache_maint_all(data, operation);
1948c2ecf20Sopenharmony_ci		return;
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/*
1988c2ecf20Sopenharmony_ci	 * If the end address is not aligned,
1998c2ecf20Sopenharmony_ci	 * perform a cache operation for the last cache-line
2008c2ecf20Sopenharmony_ci	 */
2018c2ecf20Sopenharmony_ci	size = ALIGN(size, data->line_size);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	while (size) {
2048c2ecf20Sopenharmony_ci		unsigned long chunk_size = min_t(unsigned long, size,
2058c2ecf20Sopenharmony_ci						 data->range_op_max_size);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci		__uniphier_cache_maint_common(data, start, chunk_size,
2088c2ecf20Sopenharmony_ci					UNIPHIER_SSCOQM_S_RANGE | operation);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci		start += chunk_size;
2118c2ecf20Sopenharmony_ci		size -= chunk_size;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	__uniphier_cache_sync(data);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic void __uniphier_cache_enable(struct uniphier_cache_data *data, bool on)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	u32 val = 0;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	if (on)
2228c2ecf20Sopenharmony_ci		val = UNIPHIER_SSCC_WTG | UNIPHIER_SSCC_PRD | UNIPHIER_SSCC_ON;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	writel_relaxed(val, data->ctrl_base + UNIPHIER_SSCC);
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic void __init __uniphier_cache_set_active_ways(
2288c2ecf20Sopenharmony_ci					struct uniphier_cache_data *data)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	unsigned int cpu;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	for_each_possible_cpu(cpu)
2338c2ecf20Sopenharmony_ci		writel_relaxed(data->way_mask, data->way_ctrl_base + 4 * cpu);
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic void uniphier_cache_maint_range(unsigned long start, unsigned long end,
2378c2ecf20Sopenharmony_ci				       u32 operation)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct uniphier_cache_data *data;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	list_for_each_entry(data, &uniphier_cache_list, list)
2428c2ecf20Sopenharmony_ci		__uniphier_cache_maint_range(data, start, end, operation);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void uniphier_cache_maint_all(u32 operation)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	struct uniphier_cache_data *data;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	list_for_each_entry(data, &uniphier_cache_list, list)
2508c2ecf20Sopenharmony_ci		__uniphier_cache_maint_all(data, operation);
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic void uniphier_cache_inv_range(unsigned long start, unsigned long end)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	uniphier_cache_maint_range(start, end, UNIPHIER_SSCOQM_CM_INV);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic void uniphier_cache_clean_range(unsigned long start, unsigned long end)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	uniphier_cache_maint_range(start, end, UNIPHIER_SSCOQM_CM_CLEAN);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic void uniphier_cache_flush_range(unsigned long start, unsigned long end)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	uniphier_cache_maint_range(start, end, UNIPHIER_SSCOQM_CM_FLUSH);
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic void __init uniphier_cache_inv_all(void)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_INV);
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_cistatic void uniphier_cache_flush_all(void)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_FLUSH);
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic void uniphier_cache_disable(void)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	struct uniphier_cache_data *data;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	list_for_each_entry_reverse(data, &uniphier_cache_list, list)
2838c2ecf20Sopenharmony_ci		__uniphier_cache_enable(data, false);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	uniphier_cache_flush_all();
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic void __init uniphier_cache_enable(void)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	struct uniphier_cache_data *data;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	uniphier_cache_inv_all();
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	list_for_each_entry(data, &uniphier_cache_list, list) {
2958c2ecf20Sopenharmony_ci		__uniphier_cache_enable(data, true);
2968c2ecf20Sopenharmony_ci		__uniphier_cache_set_active_ways(data);
2978c2ecf20Sopenharmony_ci	}
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic void uniphier_cache_sync(void)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct uniphier_cache_data *data;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	list_for_each_entry(data, &uniphier_cache_list, list)
3058c2ecf20Sopenharmony_ci		__uniphier_cache_sync(data);
3068c2ecf20Sopenharmony_ci}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_cistatic const struct of_device_id uniphier_cache_match[] __initconst = {
3098c2ecf20Sopenharmony_ci	{ .compatible = "socionext,uniphier-system-cache" },
3108c2ecf20Sopenharmony_ci	{ /* sentinel */ }
3118c2ecf20Sopenharmony_ci};
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic int __init __uniphier_cache_init(struct device_node *np,
3148c2ecf20Sopenharmony_ci					unsigned int *cache_level)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	struct uniphier_cache_data *data;
3178c2ecf20Sopenharmony_ci	u32 level, cache_size;
3188c2ecf20Sopenharmony_ci	struct device_node *next_np;
3198c2ecf20Sopenharmony_ci	int ret = 0;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	if (!of_match_node(uniphier_cache_match, np)) {
3228c2ecf20Sopenharmony_ci		pr_err("L%d: not compatible with uniphier cache\n",
3238c2ecf20Sopenharmony_ci		       *cache_level);
3248c2ecf20Sopenharmony_ci		return -EINVAL;
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "cache-level", &level)) {
3288c2ecf20Sopenharmony_ci		pr_err("L%d: cache-level is not specified\n", *cache_level);
3298c2ecf20Sopenharmony_ci		return -EINVAL;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	if (level != *cache_level) {
3338c2ecf20Sopenharmony_ci		pr_err("L%d: cache-level is unexpected value %d\n",
3348c2ecf20Sopenharmony_ci		       *cache_level, level);
3358c2ecf20Sopenharmony_ci		return -EINVAL;
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	if (!of_property_read_bool(np, "cache-unified")) {
3398c2ecf20Sopenharmony_ci		pr_err("L%d: cache-unified is not specified\n", *cache_level);
3408c2ecf20Sopenharmony_ci		return -EINVAL;
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	data = kzalloc(sizeof(*data), GFP_KERNEL);
3448c2ecf20Sopenharmony_ci	if (!data)
3458c2ecf20Sopenharmony_ci		return -ENOMEM;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "cache-line-size", &data->line_size) ||
3488c2ecf20Sopenharmony_ci	    !is_power_of_2(data->line_size)) {
3498c2ecf20Sopenharmony_ci		pr_err("L%d: cache-line-size is unspecified or invalid\n",
3508c2ecf20Sopenharmony_ci		       *cache_level);
3518c2ecf20Sopenharmony_ci		ret = -EINVAL;
3528c2ecf20Sopenharmony_ci		goto err;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "cache-sets", &data->nsets) ||
3568c2ecf20Sopenharmony_ci	    !is_power_of_2(data->nsets)) {
3578c2ecf20Sopenharmony_ci		pr_err("L%d: cache-sets is unspecified or invalid\n",
3588c2ecf20Sopenharmony_ci		       *cache_level);
3598c2ecf20Sopenharmony_ci		ret = -EINVAL;
3608c2ecf20Sopenharmony_ci		goto err;
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (of_property_read_u32(np, "cache-size", &cache_size) ||
3648c2ecf20Sopenharmony_ci	    cache_size == 0 || cache_size % (data->nsets * data->line_size)) {
3658c2ecf20Sopenharmony_ci		pr_err("L%d: cache-size is unspecified or invalid\n",
3668c2ecf20Sopenharmony_ci		       *cache_level);
3678c2ecf20Sopenharmony_ci		ret = -EINVAL;
3688c2ecf20Sopenharmony_ci		goto err;
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	data->way_mask = GENMASK(cache_size / data->nsets / data->line_size - 1,
3728c2ecf20Sopenharmony_ci				 0);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	data->ctrl_base = of_iomap(np, 0);
3758c2ecf20Sopenharmony_ci	if (!data->ctrl_base) {
3768c2ecf20Sopenharmony_ci		pr_err("L%d: failed to map control register\n", *cache_level);
3778c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3788c2ecf20Sopenharmony_ci		goto err;
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	data->rev_base = of_iomap(np, 1);
3828c2ecf20Sopenharmony_ci	if (!data->rev_base) {
3838c2ecf20Sopenharmony_ci		pr_err("L%d: failed to map revision register\n", *cache_level);
3848c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3858c2ecf20Sopenharmony_ci		goto err;
3868c2ecf20Sopenharmony_ci	}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	data->op_base = of_iomap(np, 2);
3898c2ecf20Sopenharmony_ci	if (!data->op_base) {
3908c2ecf20Sopenharmony_ci		pr_err("L%d: failed to map operation register\n", *cache_level);
3918c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3928c2ecf20Sopenharmony_ci		goto err;
3938c2ecf20Sopenharmony_ci	}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	data->way_ctrl_base = data->ctrl_base + 0xc00;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	if (*cache_level == 2) {
3988c2ecf20Sopenharmony_ci		u32 revision = readl(data->rev_base + UNIPHIER_SSCID);
3998c2ecf20Sopenharmony_ci		/*
4008c2ecf20Sopenharmony_ci		 * The size of range operation is limited to (1 << 22) or less
4018c2ecf20Sopenharmony_ci		 * for PH-sLD8 or older SoCs.
4028c2ecf20Sopenharmony_ci		 */
4038c2ecf20Sopenharmony_ci		if (revision <= 0x16)
4048c2ecf20Sopenharmony_ci			data->range_op_max_size = (u32)1 << 22;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci		/*
4078c2ecf20Sopenharmony_ci		 * Unfortunatly, the offset address of active way control base
4088c2ecf20Sopenharmony_ci		 * varies from SoC to SoC.
4098c2ecf20Sopenharmony_ci		 */
4108c2ecf20Sopenharmony_ci		switch (revision) {
4118c2ecf20Sopenharmony_ci		case 0x11:	/* sLD3 */
4128c2ecf20Sopenharmony_ci			data->way_ctrl_base = data->ctrl_base + 0x870;
4138c2ecf20Sopenharmony_ci			break;
4148c2ecf20Sopenharmony_ci		case 0x12:	/* LD4 */
4158c2ecf20Sopenharmony_ci		case 0x16:	/* sld8 */
4168c2ecf20Sopenharmony_ci			data->way_ctrl_base = data->ctrl_base + 0x840;
4178c2ecf20Sopenharmony_ci			break;
4188c2ecf20Sopenharmony_ci		default:
4198c2ecf20Sopenharmony_ci			break;
4208c2ecf20Sopenharmony_ci		}
4218c2ecf20Sopenharmony_ci	}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	data->range_op_max_size -= data->line_size;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&data->list);
4268c2ecf20Sopenharmony_ci	list_add_tail(&data->list, &uniphier_cache_list); /* no mutex */
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	/*
4298c2ecf20Sopenharmony_ci	 * OK, this level has been successfully initialized.  Look for the next
4308c2ecf20Sopenharmony_ci	 * level cache.  Do not roll back even if the initialization of the
4318c2ecf20Sopenharmony_ci	 * next level cache fails because we want to continue with available
4328c2ecf20Sopenharmony_ci	 * cache levels.
4338c2ecf20Sopenharmony_ci	 */
4348c2ecf20Sopenharmony_ci	next_np = of_find_next_cache_node(np);
4358c2ecf20Sopenharmony_ci	if (next_np) {
4368c2ecf20Sopenharmony_ci		(*cache_level)++;
4378c2ecf20Sopenharmony_ci		ret = __uniphier_cache_init(next_np, cache_level);
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci	of_node_put(next_np);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	return ret;
4428c2ecf20Sopenharmony_cierr:
4438c2ecf20Sopenharmony_ci	iounmap(data->op_base);
4448c2ecf20Sopenharmony_ci	iounmap(data->rev_base);
4458c2ecf20Sopenharmony_ci	iounmap(data->ctrl_base);
4468c2ecf20Sopenharmony_ci	kfree(data);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	return ret;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ciint __init uniphier_cache_init(void)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	struct device_node *np = NULL;
4548c2ecf20Sopenharmony_ci	unsigned int cache_level;
4558c2ecf20Sopenharmony_ci	int ret = 0;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	/* look for level 2 cache */
4588c2ecf20Sopenharmony_ci	while ((np = of_find_matching_node(np, uniphier_cache_match)))
4598c2ecf20Sopenharmony_ci		if (!of_property_read_u32(np, "cache-level", &cache_level) &&
4608c2ecf20Sopenharmony_ci		    cache_level == 2)
4618c2ecf20Sopenharmony_ci			break;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	if (!np)
4648c2ecf20Sopenharmony_ci		return -ENODEV;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	ret = __uniphier_cache_init(np, &cache_level);
4678c2ecf20Sopenharmony_ci	of_node_put(np);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	if (ret) {
4708c2ecf20Sopenharmony_ci		/*
4718c2ecf20Sopenharmony_ci		 * Error out iif L2 initialization fails.  Continue with any
4728c2ecf20Sopenharmony_ci		 * error on L3 or outer because they are optional.
4738c2ecf20Sopenharmony_ci		 */
4748c2ecf20Sopenharmony_ci		if (cache_level == 2) {
4758c2ecf20Sopenharmony_ci			pr_err("failed to initialize L2 cache\n");
4768c2ecf20Sopenharmony_ci			return ret;
4778c2ecf20Sopenharmony_ci		}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci		cache_level--;
4808c2ecf20Sopenharmony_ci		ret = 0;
4818c2ecf20Sopenharmony_ci	}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	outer_cache.inv_range = uniphier_cache_inv_range;
4848c2ecf20Sopenharmony_ci	outer_cache.clean_range = uniphier_cache_clean_range;
4858c2ecf20Sopenharmony_ci	outer_cache.flush_range = uniphier_cache_flush_range;
4868c2ecf20Sopenharmony_ci	outer_cache.flush_all = uniphier_cache_flush_all;
4878c2ecf20Sopenharmony_ci	outer_cache.disable = uniphier_cache_disable;
4888c2ecf20Sopenharmony_ci	outer_cache.sync = uniphier_cache_sync;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	uniphier_cache_enable();
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	pr_info("enabled outer cache (cache level: %d)\n", cache_level);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	return ret;
4958c2ecf20Sopenharmony_ci}
496