1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * CoreNet Coherency Fabric error reporting
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2014 Freescale Semiconductor Inc.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/interrupt.h>
9*4882a593Smuzhiyun #include <linux/io.h>
10*4882a593Smuzhiyun #include <linux/irq.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/of.h>
13*4882a593Smuzhiyun #include <linux/of_address.h>
14*4882a593Smuzhiyun #include <linux/of_device.h>
15*4882a593Smuzhiyun #include <linux/of_irq.h>
16*4882a593Smuzhiyun #include <linux/platform_device.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun enum ccf_version {
19*4882a593Smuzhiyun CCF1,
20*4882a593Smuzhiyun CCF2,
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun struct ccf_info {
24*4882a593Smuzhiyun enum ccf_version version;
25*4882a593Smuzhiyun int err_reg_offs;
26*4882a593Smuzhiyun bool has_brr;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun static const struct ccf_info ccf1_info = {
30*4882a593Smuzhiyun .version = CCF1,
31*4882a593Smuzhiyun .err_reg_offs = 0xa00,
32*4882a593Smuzhiyun .has_brr = false,
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun static const struct ccf_info ccf2_info = {
36*4882a593Smuzhiyun .version = CCF2,
37*4882a593Smuzhiyun .err_reg_offs = 0xe40,
38*4882a593Smuzhiyun .has_brr = true,
39*4882a593Smuzhiyun };
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun * This register is present but not documented, with different values for
43*4882a593Smuzhiyun * IP_ID, on other chips with fsl,corenet2-cf such as t4240 and b4860.
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun #define CCF_BRR 0xbf8
46*4882a593Smuzhiyun #define CCF_BRR_IPID 0xffff0000
47*4882a593Smuzhiyun #define CCF_BRR_IPID_T1040 0x09310000
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun static const struct of_device_id ccf_matches[] = {
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun .compatible = "fsl,corenet1-cf",
52*4882a593Smuzhiyun .data = &ccf1_info,
53*4882a593Smuzhiyun },
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun .compatible = "fsl,corenet2-cf",
56*4882a593Smuzhiyun .data = &ccf2_info,
57*4882a593Smuzhiyun },
58*4882a593Smuzhiyun {}
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ccf_matches);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun struct ccf_err_regs {
63*4882a593Smuzhiyun u32 errdet; /* 0x00 Error Detect Register */
64*4882a593Smuzhiyun /* 0x04 Error Enable (ccf1)/Disable (ccf2) Register */
65*4882a593Smuzhiyun u32 errdis;
66*4882a593Smuzhiyun /* 0x08 Error Interrupt Enable Register (ccf2 only) */
67*4882a593Smuzhiyun u32 errinten;
68*4882a593Smuzhiyun u32 cecar; /* 0x0c Error Capture Attribute Register */
69*4882a593Smuzhiyun u32 cecaddrh; /* 0x10 Error Capture Address High */
70*4882a593Smuzhiyun u32 cecaddrl; /* 0x14 Error Capture Address Low */
71*4882a593Smuzhiyun u32 cecar2; /* 0x18 Error Capture Attribute Register 2 */
72*4882a593Smuzhiyun };
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* LAE/CV also valid for errdis and errinten */
75*4882a593Smuzhiyun #define ERRDET_LAE (1 << 0) /* Local Access Error */
76*4882a593Smuzhiyun #define ERRDET_CV (1 << 1) /* Coherency Violation */
77*4882a593Smuzhiyun #define ERRDET_UTID (1 << 2) /* Unavailable Target ID (t1040) */
78*4882a593Smuzhiyun #define ERRDET_MCST (1 << 3) /* Multicast Stash (t1040) */
79*4882a593Smuzhiyun #define ERRDET_CTYPE_SHIFT 26 /* Capture Type (ccf2 only) */
80*4882a593Smuzhiyun #define ERRDET_CTYPE_MASK (0x1f << ERRDET_CTYPE_SHIFT)
81*4882a593Smuzhiyun #define ERRDET_CAP (1 << 31) /* Capture Valid (ccf2 only) */
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun #define CECAR_VAL (1 << 0) /* Valid (ccf1 only) */
84*4882a593Smuzhiyun #define CECAR_UVT (1 << 15) /* Unavailable target ID (ccf1) */
85*4882a593Smuzhiyun #define CECAR_SRCID_SHIFT_CCF1 24
86*4882a593Smuzhiyun #define CECAR_SRCID_MASK_CCF1 (0xff << CECAR_SRCID_SHIFT_CCF1)
87*4882a593Smuzhiyun #define CECAR_SRCID_SHIFT_CCF2 18
88*4882a593Smuzhiyun #define CECAR_SRCID_MASK_CCF2 (0xff << CECAR_SRCID_SHIFT_CCF2)
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun #define CECADDRH_ADDRH 0xff
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun struct ccf_private {
93*4882a593Smuzhiyun const struct ccf_info *info;
94*4882a593Smuzhiyun struct device *dev;
95*4882a593Smuzhiyun void __iomem *regs;
96*4882a593Smuzhiyun struct ccf_err_regs __iomem *err_regs;
97*4882a593Smuzhiyun bool t1040;
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun
ccf_irq(int irq,void * dev_id)100*4882a593Smuzhiyun static irqreturn_t ccf_irq(int irq, void *dev_id)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun struct ccf_private *ccf = dev_id;
103*4882a593Smuzhiyun static DEFINE_RATELIMIT_STATE(ratelimit, DEFAULT_RATELIMIT_INTERVAL,
104*4882a593Smuzhiyun DEFAULT_RATELIMIT_BURST);
105*4882a593Smuzhiyun u32 errdet, cecar, cecar2;
106*4882a593Smuzhiyun u64 addr;
107*4882a593Smuzhiyun u32 src_id;
108*4882a593Smuzhiyun bool uvt = false;
109*4882a593Smuzhiyun bool cap_valid = false;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun errdet = ioread32be(&ccf->err_regs->errdet);
112*4882a593Smuzhiyun cecar = ioread32be(&ccf->err_regs->cecar);
113*4882a593Smuzhiyun cecar2 = ioread32be(&ccf->err_regs->cecar2);
114*4882a593Smuzhiyun addr = ioread32be(&ccf->err_regs->cecaddrl);
115*4882a593Smuzhiyun addr |= ((u64)(ioread32be(&ccf->err_regs->cecaddrh) &
116*4882a593Smuzhiyun CECADDRH_ADDRH)) << 32;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun if (!__ratelimit(&ratelimit))
119*4882a593Smuzhiyun goto out;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun switch (ccf->info->version) {
122*4882a593Smuzhiyun case CCF1:
123*4882a593Smuzhiyun if (cecar & CECAR_VAL) {
124*4882a593Smuzhiyun if (cecar & CECAR_UVT)
125*4882a593Smuzhiyun uvt = true;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun src_id = (cecar & CECAR_SRCID_MASK_CCF1) >>
128*4882a593Smuzhiyun CECAR_SRCID_SHIFT_CCF1;
129*4882a593Smuzhiyun cap_valid = true;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun break;
133*4882a593Smuzhiyun case CCF2:
134*4882a593Smuzhiyun if (errdet & ERRDET_CAP) {
135*4882a593Smuzhiyun src_id = (cecar & CECAR_SRCID_MASK_CCF2) >>
136*4882a593Smuzhiyun CECAR_SRCID_SHIFT_CCF2;
137*4882a593Smuzhiyun cap_valid = true;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun break;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun dev_crit(ccf->dev, "errdet 0x%08x cecar 0x%08x cecar2 0x%08x\n",
144*4882a593Smuzhiyun errdet, cecar, cecar2);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun if (errdet & ERRDET_LAE) {
147*4882a593Smuzhiyun if (uvt)
148*4882a593Smuzhiyun dev_crit(ccf->dev, "LAW Unavailable Target ID\n");
149*4882a593Smuzhiyun else
150*4882a593Smuzhiyun dev_crit(ccf->dev, "Local Access Window Error\n");
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun if (errdet & ERRDET_CV)
154*4882a593Smuzhiyun dev_crit(ccf->dev, "Coherency Violation\n");
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun if (errdet & ERRDET_UTID)
157*4882a593Smuzhiyun dev_crit(ccf->dev, "Unavailable Target ID\n");
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun if (errdet & ERRDET_MCST)
160*4882a593Smuzhiyun dev_crit(ccf->dev, "Multicast Stash\n");
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun if (cap_valid) {
163*4882a593Smuzhiyun dev_crit(ccf->dev, "address 0x%09llx, src id 0x%x\n",
164*4882a593Smuzhiyun addr, src_id);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun out:
168*4882a593Smuzhiyun iowrite32be(errdet, &ccf->err_regs->errdet);
169*4882a593Smuzhiyun return errdet ? IRQ_HANDLED : IRQ_NONE;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
ccf_probe(struct platform_device * pdev)172*4882a593Smuzhiyun static int ccf_probe(struct platform_device *pdev)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun struct ccf_private *ccf;
175*4882a593Smuzhiyun struct resource *r;
176*4882a593Smuzhiyun const struct of_device_id *match;
177*4882a593Smuzhiyun u32 errinten;
178*4882a593Smuzhiyun int ret, irq;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun match = of_match_device(ccf_matches, &pdev->dev);
181*4882a593Smuzhiyun if (WARN_ON(!match))
182*4882a593Smuzhiyun return -ENODEV;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun ccf = devm_kzalloc(&pdev->dev, sizeof(*ccf), GFP_KERNEL);
185*4882a593Smuzhiyun if (!ccf)
186*4882a593Smuzhiyun return -ENOMEM;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
189*4882a593Smuzhiyun if (!r) {
190*4882a593Smuzhiyun dev_err(&pdev->dev, "%s: no mem resource\n", __func__);
191*4882a593Smuzhiyun return -ENXIO;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun ccf->regs = devm_ioremap_resource(&pdev->dev, r);
195*4882a593Smuzhiyun if (IS_ERR(ccf->regs)) {
196*4882a593Smuzhiyun dev_err(&pdev->dev, "%s: can't map mem resource\n", __func__);
197*4882a593Smuzhiyun return PTR_ERR(ccf->regs);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun ccf->dev = &pdev->dev;
201*4882a593Smuzhiyun ccf->info = match->data;
202*4882a593Smuzhiyun ccf->err_regs = ccf->regs + ccf->info->err_reg_offs;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun if (ccf->info->has_brr) {
205*4882a593Smuzhiyun u32 brr = ioread32be(ccf->regs + CCF_BRR);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if ((brr & CCF_BRR_IPID) == CCF_BRR_IPID_T1040)
208*4882a593Smuzhiyun ccf->t1040 = true;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun dev_set_drvdata(&pdev->dev, ccf);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun irq = platform_get_irq(pdev, 0);
214*4882a593Smuzhiyun if (irq < 0)
215*4882a593Smuzhiyun return irq;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun ret = devm_request_irq(&pdev->dev, irq, ccf_irq, 0, pdev->name, ccf);
218*4882a593Smuzhiyun if (ret) {
219*4882a593Smuzhiyun dev_err(&pdev->dev, "%s: can't request irq\n", __func__);
220*4882a593Smuzhiyun return ret;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun errinten = ERRDET_LAE | ERRDET_CV;
224*4882a593Smuzhiyun if (ccf->t1040)
225*4882a593Smuzhiyun errinten |= ERRDET_UTID | ERRDET_MCST;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun switch (ccf->info->version) {
228*4882a593Smuzhiyun case CCF1:
229*4882a593Smuzhiyun /* On CCF1 this register enables rather than disables. */
230*4882a593Smuzhiyun iowrite32be(errinten, &ccf->err_regs->errdis);
231*4882a593Smuzhiyun break;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun case CCF2:
234*4882a593Smuzhiyun iowrite32be(0, &ccf->err_regs->errdis);
235*4882a593Smuzhiyun iowrite32be(errinten, &ccf->err_regs->errinten);
236*4882a593Smuzhiyun break;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun return 0;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
ccf_remove(struct platform_device * pdev)242*4882a593Smuzhiyun static int ccf_remove(struct platform_device *pdev)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun struct ccf_private *ccf = dev_get_drvdata(&pdev->dev);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun switch (ccf->info->version) {
247*4882a593Smuzhiyun case CCF1:
248*4882a593Smuzhiyun iowrite32be(0, &ccf->err_regs->errdis);
249*4882a593Smuzhiyun break;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun case CCF2:
252*4882a593Smuzhiyun /*
253*4882a593Smuzhiyun * We clear errdis on ccf1 because that's the only way to
254*4882a593Smuzhiyun * disable interrupts, but on ccf2 there's no need to disable
255*4882a593Smuzhiyun * detection.
256*4882a593Smuzhiyun */
257*4882a593Smuzhiyun iowrite32be(0, &ccf->err_regs->errinten);
258*4882a593Smuzhiyun break;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun return 0;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun static struct platform_driver ccf_driver = {
265*4882a593Smuzhiyun .driver = {
266*4882a593Smuzhiyun .name = KBUILD_MODNAME,
267*4882a593Smuzhiyun .of_match_table = ccf_matches,
268*4882a593Smuzhiyun },
269*4882a593Smuzhiyun .probe = ccf_probe,
270*4882a593Smuzhiyun .remove = ccf_remove,
271*4882a593Smuzhiyun };
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun module_platform_driver(ccf_driver);
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun MODULE_LICENSE("GPL");
276*4882a593Smuzhiyun MODULE_AUTHOR("Freescale Semiconductor");
277*4882a593Smuzhiyun MODULE_DESCRIPTION("Freescale CoreNet Coherency Fabric error reporting");
278