2 * Copyright Altera Corporation (C) 2016. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
16 #include <linux/delay.h>
18 #include <linux/genalloc.h>
19 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_platform.h>
25 #define ALTR_OCRAM_CLEAR_ECC 0x00000018
26 #define ALTR_OCRAM_ECC_EN 0x00000019
28 void socfpga_init_ocram_ecc(void)
30 struct device_node
*np
;
31 void __iomem
*mapped_ocr_edac_addr
;
33 /* Find the OCRAM EDAC device tree node */
34 np
= of_find_compatible_node(NULL
, NULL
, "altr,socfpga-ocram-ecc");
36 pr_err("Unable to find socfpga-ocram-ecc\n");
40 mapped_ocr_edac_addr
= of_iomap(np
, 0);
42 if (!mapped_ocr_edac_addr
) {
43 pr_err("Unable to map OCRAM ecc regs.\n");
47 /* Clear any pending OCRAM ECC interrupts, then enable ECC */
48 writel(ALTR_OCRAM_CLEAR_ECC
, mapped_ocr_edac_addr
);
49 writel(ALTR_OCRAM_ECC_EN
, mapped_ocr_edac_addr
);
51 iounmap(mapped_ocr_edac_addr
);
54 /* Arria10 OCRAM Section */
55 #define ALTR_A10_ECC_CTRL_OFST 0x08
56 #define ALTR_A10_OCRAM_ECC_EN_CTL (BIT(1) | BIT(0))
57 #define ALTR_A10_ECC_INITA BIT(16)
59 #define ALTR_A10_ECC_INITSTAT_OFST 0x0C
60 #define ALTR_A10_ECC_INITCOMPLETEA BIT(0)
61 #define ALTR_A10_ECC_INITCOMPLETEB BIT(8)
63 #define ALTR_A10_ECC_ERRINTEN_OFST 0x10
64 #define ALTR_A10_ECC_SERRINTEN BIT(0)
66 #define ALTR_A10_ECC_INTSTAT_OFST 0x20
67 #define ALTR_A10_ECC_SERRPENA BIT(0)
68 #define ALTR_A10_ECC_DERRPENA BIT(8)
69 #define ALTR_A10_ECC_ERRPENA_MASK (ALTR_A10_ECC_SERRPENA | \
70 ALTR_A10_ECC_DERRPENA)
71 /* ECC Manager Defines */
72 #define A10_SYSMGR_ECC_INTMASK_SET_OFST 0x94
73 #define A10_SYSMGR_ECC_INTMASK_CLR_OFST 0x98
74 #define A10_SYSMGR_ECC_INTMASK_OCRAM BIT(1)
76 #define ALTR_A10_ECC_INIT_WATCHDOG_10US 10000
78 static inline void ecc_set_bits(u32 bit_mask
, void __iomem
*ioaddr
)
80 u32 value
= readl(ioaddr
);
83 writel(value
, ioaddr
);
86 static inline void ecc_clear_bits(u32 bit_mask
, void __iomem
*ioaddr
)
88 u32 value
= readl(ioaddr
);
91 writel(value
, ioaddr
);
94 static inline int ecc_test_bits(u32 bit_mask
, void __iomem
*ioaddr
)
96 u32 value
= readl(ioaddr
);
98 return (value
& bit_mask
) ? 1 : 0;
102 * This function uses the memory initialization block in the Arria10 ECC
103 * controller to initialize/clear the entire memory data and ECC data.
105 static int altr_init_memory_port(void __iomem
*ioaddr
)
107 int limit
= ALTR_A10_ECC_INIT_WATCHDOG_10US
;
109 ecc_set_bits(ALTR_A10_ECC_INITA
, (ioaddr
+ ALTR_A10_ECC_CTRL_OFST
));
111 if (ecc_test_bits(ALTR_A10_ECC_INITCOMPLETEA
,
112 (ioaddr
+ ALTR_A10_ECC_INITSTAT_OFST
)))
119 /* Clear any pending ECC interrupts */
120 writel(ALTR_A10_ECC_ERRPENA_MASK
,
121 (ioaddr
+ ALTR_A10_ECC_INTSTAT_OFST
));
126 void socfpga_init_arria10_ocram_ecc(void)
128 struct device_node
*np
;
130 void __iomem
*ecc_block_base
;
132 if (!sys_manager_base_addr
) {
133 pr_err("SOCFPGA: sys-mgr is not initialized\n");
137 /* Find the OCRAM EDAC device tree node */
138 np
= of_find_compatible_node(NULL
, NULL
, "altr,socfpga-a10-ocram-ecc");
140 pr_err("Unable to find socfpga-a10-ocram-ecc\n");
144 /* Map the ECC Block */
145 ecc_block_base
= of_iomap(np
, 0);
147 if (!ecc_block_base
) {
148 pr_err("Unable to map OCRAM ECC block\n");
153 writel(ALTR_A10_OCRAM_ECC_EN_CTL
,
154 sys_manager_base_addr
+ A10_SYSMGR_ECC_INTMASK_SET_OFST
);
155 ecc_clear_bits(ALTR_A10_ECC_SERRINTEN
,
156 (ecc_block_base
+ ALTR_A10_ECC_ERRINTEN_OFST
));
157 ecc_clear_bits(ALTR_A10_OCRAM_ECC_EN_CTL
,
158 (ecc_block_base
+ ALTR_A10_ECC_CTRL_OFST
));
160 /* Ensure all writes complete */
163 /* Use HW initialization block to initialize memory for ECC */
164 ret
= altr_init_memory_port(ecc_block_base
);
166 pr_err("ECC: cannot init OCRAM PORTA memory\n");
171 ecc_set_bits(ALTR_A10_OCRAM_ECC_EN_CTL
,
172 (ecc_block_base
+ ALTR_A10_ECC_CTRL_OFST
));
173 ecc_set_bits(ALTR_A10_ECC_SERRINTEN
,
174 (ecc_block_base
+ ALTR_A10_ECC_ERRINTEN_OFST
));
175 writel(ALTR_A10_OCRAM_ECC_EN_CTL
,
176 sys_manager_base_addr
+ A10_SYSMGR_ECC_INTMASK_CLR_OFST
);
178 /* Ensure all writes complete */
181 iounmap(ecc_block_base
);