RISC-V: sifive_l2_cache: Add L2 cache controller driver for SiFive SoCs
[linux-2.6-microblaze.git] / arch / riscv / mm / sifive_l2_cache.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SiFive L2 cache controller Driver
4  *
5  * Copyright (C) 2018-2019 SiFive, Inc.
6  *
7  */
8 #include <linux/debugfs.h>
9 #include <linux/interrupt.h>
10 #include <linux/of_irq.h>
11 #include <linux/of_address.h>
12 #include <asm/sifive_l2_cache.h>
13
14 #define SIFIVE_L2_DIRECCFIX_LOW 0x100
15 #define SIFIVE_L2_DIRECCFIX_HIGH 0x104
16 #define SIFIVE_L2_DIRECCFIX_COUNT 0x108
17
18 #define SIFIVE_L2_DATECCFIX_LOW 0x140
19 #define SIFIVE_L2_DATECCFIX_HIGH 0x144
20 #define SIFIVE_L2_DATECCFIX_COUNT 0x148
21
22 #define SIFIVE_L2_DATECCFAIL_LOW 0x160
23 #define SIFIVE_L2_DATECCFAIL_HIGH 0x164
24 #define SIFIVE_L2_DATECCFAIL_COUNT 0x168
25
26 #define SIFIVE_L2_CONFIG 0x00
27 #define SIFIVE_L2_WAYENABLE 0x08
28 #define SIFIVE_L2_ECCINJECTERR 0x40
29
30 #define SIFIVE_L2_MAX_ECCINTR 3
31
32 static void __iomem *l2_base;
33 static int g_irq[SIFIVE_L2_MAX_ECCINTR];
34
35 enum {
36         DIR_CORR = 0,
37         DATA_CORR,
38         DATA_UNCORR,
39 };
40
41 #ifdef CONFIG_DEBUG_FS
42 static struct dentry *sifive_test;
43
44 static ssize_t l2_write(struct file *file, const char __user *data,
45                         size_t count, loff_t *ppos)
46 {
47         unsigned int val;
48
49         if (kstrtouint_from_user(data, count, 0, &val))
50                 return -EINVAL;
51         if ((val >= 0 && val < 0xFF) || (val >= 0x10000 && val < 0x100FF))
52                 writel(val, l2_base + SIFIVE_L2_ECCINJECTERR);
53         else
54                 return -EINVAL;
55         return count;
56 }
57
58 static const struct file_operations l2_fops = {
59         .owner = THIS_MODULE,
60         .open = simple_open,
61         .write = l2_write
62 };
63
64 static void setup_sifive_debug(void)
65 {
66         sifive_test = debugfs_create_dir("sifive_l2_cache", NULL);
67
68         debugfs_create_file("sifive_debug_inject_error", 0200,
69                             sifive_test, NULL, &l2_fops);
70 }
71 #endif
72
73 static void l2_config_read(void)
74 {
75         u32 regval, val;
76
77         regval = readl(l2_base + SIFIVE_L2_CONFIG);
78         val = regval & 0xFF;
79         pr_info("L2CACHE: No. of Banks in the cache: %d\n", val);
80         val = (regval & 0xFF00) >> 8;
81         pr_info("L2CACHE: No. of ways per bank: %d\n", val);
82         val = (regval & 0xFF0000) >> 16;
83         pr_info("L2CACHE: Sets per bank: %llu\n", (uint64_t)1 << val);
84         val = (regval & 0xFF000000) >> 24;
85         pr_info("L2CACHE: Bytes per cache block: %llu\n", (uint64_t)1 << val);
86
87         regval = readl(l2_base + SIFIVE_L2_WAYENABLE);
88         pr_info("L2CACHE: Index of the largest way enabled: %d\n", regval);
89 }
90
91 static const struct of_device_id sifive_l2_ids[] = {
92         { .compatible = "sifive,fu540-c000-ccache" },
93         { /* end of table */ },
94 };
95
96 static ATOMIC_NOTIFIER_HEAD(l2_err_chain);
97
98 int register_sifive_l2_error_notifier(struct notifier_block *nb)
99 {
100         return atomic_notifier_chain_register(&l2_err_chain, nb);
101 }
102 EXPORT_SYMBOL_GPL(register_sifive_l2_error_notifier);
103
104 int unregister_sifive_l2_error_notifier(struct notifier_block *nb)
105 {
106         return atomic_notifier_chain_unregister(&l2_err_chain, nb);
107 }
108 EXPORT_SYMBOL_GPL(unregister_sifive_l2_error_notifier);
109
110 static irqreturn_t l2_int_handler(int irq, void *device)
111 {
112         unsigned int regval, add_h, add_l;
113
114         if (irq == g_irq[DIR_CORR]) {
115                 add_h = readl(l2_base + SIFIVE_L2_DIRECCFIX_HIGH);
116                 add_l = readl(l2_base + SIFIVE_L2_DIRECCFIX_LOW);
117                 pr_err("L2CACHE: DirError @ 0x%08X.%08X\n", add_h, add_l);
118                 regval = readl(l2_base + SIFIVE_L2_DIRECCFIX_COUNT);
119                 atomic_notifier_call_chain(&l2_err_chain, SIFIVE_L2_ERR_TYPE_CE,
120                                            "DirECCFix");
121         }
122         if (irq == g_irq[DATA_CORR]) {
123                 add_h = readl(l2_base + SIFIVE_L2_DATECCFIX_HIGH);
124                 add_l = readl(l2_base + SIFIVE_L2_DATECCFIX_LOW);
125                 pr_err("L2CACHE: DataError @ 0x%08X.%08X\n", add_h, add_l);
126                 regval = readl(l2_base + SIFIVE_L2_DATECCFIX_COUNT);
127                 atomic_notifier_call_chain(&l2_err_chain, SIFIVE_L2_ERR_TYPE_CE,
128                                            "DatECCFix");
129         }
130         if (irq == g_irq[DATA_UNCORR]) {
131                 add_h = readl(l2_base + SIFIVE_L2_DATECCFAIL_HIGH);
132                 add_l = readl(l2_base + SIFIVE_L2_DATECCFAIL_LOW);
133                 pr_err("L2CACHE: DataFail @ 0x%08X.%08X\n", add_h, add_l);
134                 regval = readl(l2_base + SIFIVE_L2_DATECCFAIL_COUNT);
135                 atomic_notifier_call_chain(&l2_err_chain, SIFIVE_L2_ERR_TYPE_UE,
136                                            "DatECCFail");
137         }
138
139         return IRQ_HANDLED;
140 }
141
142 int __init sifive_l2_init(void)
143 {
144         struct device_node *np;
145         struct resource res;
146         int i, rc;
147
148         np = of_find_matching_node(NULL, sifive_l2_ids);
149         if (!np)
150                 return -ENODEV;
151
152         if (of_address_to_resource(np, 0, &res))
153                 return -ENODEV;
154
155         l2_base = ioremap(res.start, resource_size(&res));
156         if (!l2_base)
157                 return -ENOMEM;
158
159         for (i = 0; i < SIFIVE_L2_MAX_ECCINTR; i++) {
160                 g_irq[i] = irq_of_parse_and_map(np, i);
161                 rc = request_irq(g_irq[i], l2_int_handler, 0, "l2_ecc", NULL);
162                 if (rc) {
163                         pr_err("L2CACHE: Could not request IRQ %d\n", g_irq[i]);
164                         return rc;
165                 }
166         }
167
168         l2_config_read();
169
170 #ifdef CONFIG_DEBUG_FS
171         setup_sifive_debug();
172 #endif
173         return 0;
174 }
175 device_initcall(sifive_l2_init);