1*d5d1a1a5SChristophe Leroy // SPDX-License-Identifier: GPL-2.0-or-later
2*d5d1a1a5SChristophe Leroy /*
3*d5d1a1a5SChristophe Leroy * IBM/AMCC PPC4xx SoC setup code
4*d5d1a1a5SChristophe Leroy *
5*d5d1a1a5SChristophe Leroy * Copyright 2008 DENX Software Engineering, Stefan Roese <sr@denx.de>
6*d5d1a1a5SChristophe Leroy *
7*d5d1a1a5SChristophe Leroy * L2 cache routines cloned from arch/ppc/syslib/ibm440gx_common.c which is:
8*d5d1a1a5SChristophe Leroy * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
9*d5d1a1a5SChristophe Leroy * Copyright (c) 2003 - 2006 Zultys Technologies
10*d5d1a1a5SChristophe Leroy */
11*d5d1a1a5SChristophe Leroy
12*d5d1a1a5SChristophe Leroy #include <linux/stddef.h>
13*d5d1a1a5SChristophe Leroy #include <linux/kernel.h>
14*d5d1a1a5SChristophe Leroy #include <linux/init.h>
15*d5d1a1a5SChristophe Leroy #include <linux/errno.h>
16*d5d1a1a5SChristophe Leroy #include <linux/interrupt.h>
17*d5d1a1a5SChristophe Leroy #include <linux/irq.h>
18*d5d1a1a5SChristophe Leroy #include <linux/of.h>
19*d5d1a1a5SChristophe Leroy #include <linux/of_irq.h>
20*d5d1a1a5SChristophe Leroy
21*d5d1a1a5SChristophe Leroy #include <asm/dcr.h>
22*d5d1a1a5SChristophe Leroy #include <asm/dcr-regs.h>
23*d5d1a1a5SChristophe Leroy #include <asm/reg.h>
24*d5d1a1a5SChristophe Leroy #include <asm/ppc4xx.h>
25*d5d1a1a5SChristophe Leroy
26*d5d1a1a5SChristophe Leroy static u32 dcrbase_l2c;
27*d5d1a1a5SChristophe Leroy
28*d5d1a1a5SChristophe Leroy /*
29*d5d1a1a5SChristophe Leroy * L2-cache
30*d5d1a1a5SChristophe Leroy */
31*d5d1a1a5SChristophe Leroy
32*d5d1a1a5SChristophe Leroy /* Issue L2C diagnostic command */
l2c_diag(u32 addr)33*d5d1a1a5SChristophe Leroy static inline u32 l2c_diag(u32 addr)
34*d5d1a1a5SChristophe Leroy {
35*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_ADDR, addr);
36*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_CMD, L2C_CMD_DIAG);
37*d5d1a1a5SChristophe Leroy while (!(mfdcr(dcrbase_l2c + DCRN_L2C0_SR) & L2C_SR_CC))
38*d5d1a1a5SChristophe Leroy ;
39*d5d1a1a5SChristophe Leroy
40*d5d1a1a5SChristophe Leroy return mfdcr(dcrbase_l2c + DCRN_L2C0_DATA);
41*d5d1a1a5SChristophe Leroy }
42*d5d1a1a5SChristophe Leroy
l2c_error_handler(int irq,void * dev)43*d5d1a1a5SChristophe Leroy static irqreturn_t l2c_error_handler(int irq, void *dev)
44*d5d1a1a5SChristophe Leroy {
45*d5d1a1a5SChristophe Leroy u32 sr = mfdcr(dcrbase_l2c + DCRN_L2C0_SR);
46*d5d1a1a5SChristophe Leroy
47*d5d1a1a5SChristophe Leroy if (sr & L2C_SR_CPE) {
48*d5d1a1a5SChristophe Leroy /* Read cache trapped address */
49*d5d1a1a5SChristophe Leroy u32 addr = l2c_diag(0x42000000);
50*d5d1a1a5SChristophe Leroy printk(KERN_EMERG "L2C: Cache Parity Error, addr[16:26] = 0x%08x\n",
51*d5d1a1a5SChristophe Leroy addr);
52*d5d1a1a5SChristophe Leroy }
53*d5d1a1a5SChristophe Leroy if (sr & L2C_SR_TPE) {
54*d5d1a1a5SChristophe Leroy /* Read tag trapped address */
55*d5d1a1a5SChristophe Leroy u32 addr = l2c_diag(0x82000000) >> 16;
56*d5d1a1a5SChristophe Leroy printk(KERN_EMERG "L2C: Tag Parity Error, addr[16:26] = 0x%08x\n",
57*d5d1a1a5SChristophe Leroy addr);
58*d5d1a1a5SChristophe Leroy }
59*d5d1a1a5SChristophe Leroy
60*d5d1a1a5SChristophe Leroy /* Clear parity errors */
61*d5d1a1a5SChristophe Leroy if (sr & (L2C_SR_CPE | L2C_SR_TPE)){
62*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_ADDR, 0);
63*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_CMD, L2C_CMD_CCP | L2C_CMD_CTE);
64*d5d1a1a5SChristophe Leroy } else {
65*d5d1a1a5SChristophe Leroy printk(KERN_EMERG "L2C: LRU error\n");
66*d5d1a1a5SChristophe Leroy }
67*d5d1a1a5SChristophe Leroy
68*d5d1a1a5SChristophe Leroy return IRQ_HANDLED;
69*d5d1a1a5SChristophe Leroy }
70*d5d1a1a5SChristophe Leroy
ppc4xx_l2c_probe(void)71*d5d1a1a5SChristophe Leroy static int __init ppc4xx_l2c_probe(void)
72*d5d1a1a5SChristophe Leroy {
73*d5d1a1a5SChristophe Leroy struct device_node *np;
74*d5d1a1a5SChristophe Leroy u32 r;
75*d5d1a1a5SChristophe Leroy unsigned long flags;
76*d5d1a1a5SChristophe Leroy int irq;
77*d5d1a1a5SChristophe Leroy const u32 *dcrreg;
78*d5d1a1a5SChristophe Leroy u32 dcrbase_isram;
79*d5d1a1a5SChristophe Leroy int len;
80*d5d1a1a5SChristophe Leroy const u32 *prop;
81*d5d1a1a5SChristophe Leroy u32 l2_size;
82*d5d1a1a5SChristophe Leroy
83*d5d1a1a5SChristophe Leroy np = of_find_compatible_node(NULL, NULL, "ibm,l2-cache");
84*d5d1a1a5SChristophe Leroy if (!np)
85*d5d1a1a5SChristophe Leroy return 0;
86*d5d1a1a5SChristophe Leroy
87*d5d1a1a5SChristophe Leroy /* Get l2 cache size */
88*d5d1a1a5SChristophe Leroy prop = of_get_property(np, "cache-size", NULL);
89*d5d1a1a5SChristophe Leroy if (prop == NULL) {
90*d5d1a1a5SChristophe Leroy printk(KERN_ERR "%pOF: Can't get cache-size!\n", np);
91*d5d1a1a5SChristophe Leroy of_node_put(np);
92*d5d1a1a5SChristophe Leroy return -ENODEV;
93*d5d1a1a5SChristophe Leroy }
94*d5d1a1a5SChristophe Leroy l2_size = prop[0];
95*d5d1a1a5SChristophe Leroy
96*d5d1a1a5SChristophe Leroy /* Map DCRs */
97*d5d1a1a5SChristophe Leroy dcrreg = of_get_property(np, "dcr-reg", &len);
98*d5d1a1a5SChristophe Leroy if (!dcrreg || (len != 4 * sizeof(u32))) {
99*d5d1a1a5SChristophe Leroy printk(KERN_ERR "%pOF: Can't get DCR register base !", np);
100*d5d1a1a5SChristophe Leroy of_node_put(np);
101*d5d1a1a5SChristophe Leroy return -ENODEV;
102*d5d1a1a5SChristophe Leroy }
103*d5d1a1a5SChristophe Leroy dcrbase_isram = dcrreg[0];
104*d5d1a1a5SChristophe Leroy dcrbase_l2c = dcrreg[2];
105*d5d1a1a5SChristophe Leroy
106*d5d1a1a5SChristophe Leroy /* Get and map irq number from device tree */
107*d5d1a1a5SChristophe Leroy irq = irq_of_parse_and_map(np, 0);
108*d5d1a1a5SChristophe Leroy if (!irq) {
109*d5d1a1a5SChristophe Leroy printk(KERN_ERR "irq_of_parse_and_map failed\n");
110*d5d1a1a5SChristophe Leroy of_node_put(np);
111*d5d1a1a5SChristophe Leroy return -ENODEV;
112*d5d1a1a5SChristophe Leroy }
113*d5d1a1a5SChristophe Leroy
114*d5d1a1a5SChristophe Leroy /* Install error handler */
115*d5d1a1a5SChristophe Leroy if (request_irq(irq, l2c_error_handler, 0, "L2C", NULL) < 0) {
116*d5d1a1a5SChristophe Leroy printk(KERN_ERR "Cannot install L2C error handler"
117*d5d1a1a5SChristophe Leroy ", cache is not enabled\n");
118*d5d1a1a5SChristophe Leroy of_node_put(np);
119*d5d1a1a5SChristophe Leroy return -ENODEV;
120*d5d1a1a5SChristophe Leroy }
121*d5d1a1a5SChristophe Leroy
122*d5d1a1a5SChristophe Leroy local_irq_save(flags);
123*d5d1a1a5SChristophe Leroy asm volatile ("sync" ::: "memory");
124*d5d1a1a5SChristophe Leroy
125*d5d1a1a5SChristophe Leroy /* Disable SRAM */
126*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_isram + DCRN_SRAM0_DPC,
127*d5d1a1a5SChristophe Leroy mfdcr(dcrbase_isram + DCRN_SRAM0_DPC) & ~SRAM_DPC_ENABLE);
128*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_isram + DCRN_SRAM0_SB0CR,
129*d5d1a1a5SChristophe Leroy mfdcr(dcrbase_isram + DCRN_SRAM0_SB0CR) & ~SRAM_SBCR_BU_MASK);
130*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_isram + DCRN_SRAM0_SB1CR,
131*d5d1a1a5SChristophe Leroy mfdcr(dcrbase_isram + DCRN_SRAM0_SB1CR) & ~SRAM_SBCR_BU_MASK);
132*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_isram + DCRN_SRAM0_SB2CR,
133*d5d1a1a5SChristophe Leroy mfdcr(dcrbase_isram + DCRN_SRAM0_SB2CR) & ~SRAM_SBCR_BU_MASK);
134*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_isram + DCRN_SRAM0_SB3CR,
135*d5d1a1a5SChristophe Leroy mfdcr(dcrbase_isram + DCRN_SRAM0_SB3CR) & ~SRAM_SBCR_BU_MASK);
136*d5d1a1a5SChristophe Leroy
137*d5d1a1a5SChristophe Leroy /* Enable L2_MODE without ICU/DCU */
138*d5d1a1a5SChristophe Leroy r = mfdcr(dcrbase_l2c + DCRN_L2C0_CFG) &
139*d5d1a1a5SChristophe Leroy ~(L2C_CFG_ICU | L2C_CFG_DCU | L2C_CFG_SS_MASK);
140*d5d1a1a5SChristophe Leroy r |= L2C_CFG_L2M | L2C_CFG_SS_256;
141*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_CFG, r);
142*d5d1a1a5SChristophe Leroy
143*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_ADDR, 0);
144*d5d1a1a5SChristophe Leroy
145*d5d1a1a5SChristophe Leroy /* Hardware Clear Command */
146*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_CMD, L2C_CMD_HCC);
147*d5d1a1a5SChristophe Leroy while (!(mfdcr(dcrbase_l2c + DCRN_L2C0_SR) & L2C_SR_CC))
148*d5d1a1a5SChristophe Leroy ;
149*d5d1a1a5SChristophe Leroy
150*d5d1a1a5SChristophe Leroy /* Clear Cache Parity and Tag Errors */
151*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_CMD, L2C_CMD_CCP | L2C_CMD_CTE);
152*d5d1a1a5SChristophe Leroy
153*d5d1a1a5SChristophe Leroy /* Enable 64G snoop region starting at 0 */
154*d5d1a1a5SChristophe Leroy r = mfdcr(dcrbase_l2c + DCRN_L2C0_SNP0) &
155*d5d1a1a5SChristophe Leroy ~(L2C_SNP_BA_MASK | L2C_SNP_SSR_MASK);
156*d5d1a1a5SChristophe Leroy r |= L2C_SNP_SSR_32G | L2C_SNP_ESR;
157*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_SNP0, r);
158*d5d1a1a5SChristophe Leroy
159*d5d1a1a5SChristophe Leroy r = mfdcr(dcrbase_l2c + DCRN_L2C0_SNP1) &
160*d5d1a1a5SChristophe Leroy ~(L2C_SNP_BA_MASK | L2C_SNP_SSR_MASK);
161*d5d1a1a5SChristophe Leroy r |= 0x80000000 | L2C_SNP_SSR_32G | L2C_SNP_ESR;
162*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_SNP1, r);
163*d5d1a1a5SChristophe Leroy
164*d5d1a1a5SChristophe Leroy asm volatile ("sync" ::: "memory");
165*d5d1a1a5SChristophe Leroy
166*d5d1a1a5SChristophe Leroy /* Enable ICU/DCU ports */
167*d5d1a1a5SChristophe Leroy r = mfdcr(dcrbase_l2c + DCRN_L2C0_CFG);
168*d5d1a1a5SChristophe Leroy r &= ~(L2C_CFG_DCW_MASK | L2C_CFG_PMUX_MASK | L2C_CFG_PMIM
169*d5d1a1a5SChristophe Leroy | L2C_CFG_TPEI | L2C_CFG_CPEI | L2C_CFG_NAM | L2C_CFG_NBRM);
170*d5d1a1a5SChristophe Leroy r |= L2C_CFG_ICU | L2C_CFG_DCU | L2C_CFG_TPC | L2C_CFG_CPC | L2C_CFG_FRAN
171*d5d1a1a5SChristophe Leroy | L2C_CFG_CPIM | L2C_CFG_TPIM | L2C_CFG_LIM | L2C_CFG_SMCM;
172*d5d1a1a5SChristophe Leroy
173*d5d1a1a5SChristophe Leroy /* Check for 460EX/GT special handling */
174*d5d1a1a5SChristophe Leroy if (of_device_is_compatible(np, "ibm,l2-cache-460ex") ||
175*d5d1a1a5SChristophe Leroy of_device_is_compatible(np, "ibm,l2-cache-460gt"))
176*d5d1a1a5SChristophe Leroy r |= L2C_CFG_RDBW;
177*d5d1a1a5SChristophe Leroy
178*d5d1a1a5SChristophe Leroy mtdcr(dcrbase_l2c + DCRN_L2C0_CFG, r);
179*d5d1a1a5SChristophe Leroy
180*d5d1a1a5SChristophe Leroy asm volatile ("sync; isync" ::: "memory");
181*d5d1a1a5SChristophe Leroy local_irq_restore(flags);
182*d5d1a1a5SChristophe Leroy
183*d5d1a1a5SChristophe Leroy printk(KERN_INFO "%dk L2-cache enabled\n", l2_size >> 10);
184*d5d1a1a5SChristophe Leroy
185*d5d1a1a5SChristophe Leroy of_node_put(np);
186*d5d1a1a5SChristophe Leroy return 0;
187*d5d1a1a5SChristophe Leroy }
188*d5d1a1a5SChristophe Leroy arch_initcall(ppc4xx_l2c_probe);
189*d5d1a1a5SChristophe Leroy
190*d5d1a1a5SChristophe Leroy /*
191*d5d1a1a5SChristophe Leroy * Apply a system reset. Alternatively a board specific value may be
192*d5d1a1a5SChristophe Leroy * provided via the "reset-type" property in the cpu node.
193*d5d1a1a5SChristophe Leroy */
ppc4xx_reset_system(char * cmd)194*d5d1a1a5SChristophe Leroy void ppc4xx_reset_system(char *cmd)
195*d5d1a1a5SChristophe Leroy {
196*d5d1a1a5SChristophe Leroy struct device_node *np;
197*d5d1a1a5SChristophe Leroy u32 reset_type = DBCR0_RST_SYSTEM;
198*d5d1a1a5SChristophe Leroy const u32 *prop;
199*d5d1a1a5SChristophe Leroy
200*d5d1a1a5SChristophe Leroy np = of_get_cpu_node(0, NULL);
201*d5d1a1a5SChristophe Leroy if (np) {
202*d5d1a1a5SChristophe Leroy prop = of_get_property(np, "reset-type", NULL);
203*d5d1a1a5SChristophe Leroy
204*d5d1a1a5SChristophe Leroy /*
205*d5d1a1a5SChristophe Leroy * Check if property exists and if it is in range:
206*d5d1a1a5SChristophe Leroy * 1 - PPC4xx core reset
207*d5d1a1a5SChristophe Leroy * 2 - PPC4xx chip reset
208*d5d1a1a5SChristophe Leroy * 3 - PPC4xx system reset (default)
209*d5d1a1a5SChristophe Leroy */
210*d5d1a1a5SChristophe Leroy if ((prop) && ((prop[0] >= 1) && (prop[0] <= 3)))
211*d5d1a1a5SChristophe Leroy reset_type = prop[0] << 28;
212*d5d1a1a5SChristophe Leroy }
213*d5d1a1a5SChristophe Leroy
214*d5d1a1a5SChristophe Leroy mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) | reset_type);
215*d5d1a1a5SChristophe Leroy
216*d5d1a1a5SChristophe Leroy while (1)
217*d5d1a1a5SChristophe Leroy ; /* Just in case the reset doesn't work */
218*d5d1a1a5SChristophe Leroy }
219