summaryrefslogtreecommitdiffstats
path: root/drivers/soc/sifive/sifive_l2_cache.c
blob: a1e9a10622042db208f6bdabff04bcf4410cb5e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// SPDX-License-Identifier: GPL-2.0
/*
 * SiFive L2 cache controller Driver
 *
 * Copyright (C) 2018-2019 SiFive, Inc.
 *
 */

#define pr_fmt(fmt) "sifive-l2: " fmt

#include <io.h>
#include <printk.h>
#include <stdio.h>
#include <driver.h>
#include <init.h>
#include <soc/sifive/l2_cache.h>
#include <asm/barrier.h>
#include <linux/bitops.h>

#define SIFIVE_L2_DIRECCFIX_LOW 0x100
#define SIFIVE_L2_DIRECCFIX_HIGH 0x104
#define SIFIVE_L2_DIRECCFIX_COUNT 0x108

#define SIFIVE_L2_DIRECCFAIL_LOW 0x120
#define SIFIVE_L2_DIRECCFAIL_HIGH 0x124
#define SIFIVE_L2_DIRECCFAIL_COUNT 0x128

#define SIFIVE_L2_DATECCFIX_LOW 0x140
#define SIFIVE_L2_DATECCFIX_HIGH 0x144
#define SIFIVE_L2_DATECCFIX_COUNT 0x148

#define SIFIVE_L2_DATECCFAIL_LOW 0x160
#define SIFIVE_L2_DATECCFAIL_HIGH 0x164
#define SIFIVE_L2_DATECCFAIL_COUNT 0x168

#define SIFIVE_L2_FLUSH64 0x200

#define SIFIVE_L2_CONFIG 0x00
#define SIFIVE_L2_WAYENABLE 0x08
#define SIFIVE_L2_ECCINJECTERR 0x40

#define SIFIVE_L2_MAX_ECCINTR 4

#define MASK_NUM_WAYS   GENMASK(15, 8)
#define NUM_WAYS_SHIFT  8

#define SIFIVE_L2_FLUSH64_LINE_LEN 64

static void __iomem *l2_base = NULL;

static void sifive_l2_config_read(struct device_d *dev)
{
	u32 regval, val;

	printf("Cache configuration:\n");

	regval = readl(l2_base + SIFIVE_L2_CONFIG);
	val = regval & 0xFF;
	printf("  #Banks: %d\n", val);
	val = (regval & 0xFF00) >> 8;
	printf("  #Ways per bank: %d\n", val);
	val = (regval & 0xFF0000) >> 16;
	printf("  #Sets per bank: %llu\n", 1llu << val);
	val = (regval & 0xFF000000) >> 24;
	printf("  #Bytes per cache block: %llu\n", 1llu << val);

	regval = readl(l2_base + SIFIVE_L2_WAYENABLE);
	printf("  #Index of the largest way enabled: %d\n", regval);
}

void sifive_l2_flush64_range(dma_addr_t start, dma_addr_t end)
{
	unsigned long line;

	start = ALIGN_DOWN(start, 64);
	end = ALIGN(end, 64);

	if (WARN_ON(!l2_base))
		return;

	if (start == end)
		return;

	mb();
	for (line = start; line < end; line += SIFIVE_L2_FLUSH64_LINE_LEN) {
		writeq(line, l2_base + SIFIVE_L2_FLUSH64);
		mb();
	}
}

static void sifive_l2_enable_ways(void)
{
	u32 config;
	u32 ways;

	config = readl(l2_base + SIFIVE_L2_CONFIG);
	ways = (config & MASK_NUM_WAYS) >> NUM_WAYS_SHIFT;

	mb();
	writel(ways - 1, l2_base + SIFIVE_L2_WAYENABLE);
	mb();
}

static int sifive_l2_probe(struct device_d *dev)
{
	struct resource *iores;

	if (l2_base)
		return -EBUSY;

	iores = dev_request_mem_resource(dev, 0);
	if (IS_ERR(iores))
		return PTR_ERR(iores);

	l2_base = IOMEM(iores->start);

	sifive_l2_enable_ways();

	dev->info = sifive_l2_config_read;

	return 0;
}

static const struct of_device_id sifive_l2_ids[] = {
	{ .compatible = "sifive,fu540-c000-ccache" },
	{ .compatible = "sifive,fu740-c000-ccache" },
	{ .compatible = "starfive,ccache0" },
	{ /* end of table */ },
};

static struct driver_d sifive_l2_driver = {
	.name = "sfive-l2cache",
	.probe = sifive_l2_probe,
	.of_compatible = sifive_l2_ids,
};
postcore_platform_driver(sifive_l2_driver);