1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2022 Nuvoton Technology Corporation
4 #include <linux/debugfs.h>
5 #include <linux/iopoll.h>
7 #include <linux/platform_device.h>
8 #include <linux/regmap.h>
9 #include "edac_module.h"
11 #define EDAC_MOD_NAME "npcm-edac"
12 #define EDAC_MSG_SIZE 256
15 #define NPCM7XX_CHIP BIT(0)
16 #define NPCM8XX_CHIP BIT(1)
19 #define UE_SYNDROME 0x03
22 #define ERROR_TYPE_CORRECTABLE 0
23 #define ERROR_TYPE_UNCORRECTABLE 1
24 #define ERROR_LOCATION_DATA 0
25 #define ERROR_LOCATION_CHECKCODE 1
26 #define ERROR_BIT_DATA_MAX 63
27 #define ERROR_BIT_CHECKCODE_MAX 7
29 static char data_synd
[] = {
30 0xf4, 0xf1, 0xec, 0xea, 0xe9, 0xe6, 0xe5, 0xe3,
31 0xdc, 0xda, 0xd9, 0xd6, 0xd5, 0xd3, 0xce, 0xcb,
32 0xb5, 0xb0, 0xad, 0xab, 0xa8, 0xa7, 0xa4, 0xa2,
33 0x9d, 0x9b, 0x98, 0x97, 0x94, 0x92, 0x8f, 0x8a,
34 0x75, 0x70, 0x6d, 0x6b, 0x68, 0x67, 0x64, 0x62,
35 0x5e, 0x5b, 0x58, 0x57, 0x54, 0x52, 0x4f, 0x4a,
36 0x34, 0x31, 0x2c, 0x2a, 0x29, 0x26, 0x25, 0x23,
37 0x1c, 0x1a, 0x19, 0x16, 0x15, 0x13, 0x0e, 0x0b
40 static struct regmap
*npcm_regmap
;
42 struct npcm_platform_data
{
46 /* memory controller registers */
50 u32 ctl_int_mask_master
;
63 u32 ctl_controller_busy
;
64 u32 ctl_xor_check_bits
;
66 /* masks and shifts */
68 u32 int_status_ce_mask
;
69 u32 int_status_ue_mask
;
72 u32 int_mask_master_non_ecc_mask
;
73 u32 int_mask_master_global_mask
;
74 u32 int_mask_ecc_non_event_mask
;
81 u32 source_id_ce_mask
;
82 u32 source_id_ce_shift
;
83 u32 source_id_ue_mask
;
84 u32 source_id_ue_shift
;
85 u32 controller_busy_mask
;
86 u32 xor_check_bits_mask
;
87 u32 xor_check_bits_shift
;
88 u32 writeback_en_mask
;
94 char message
[EDAC_MSG_SIZE
];
95 const struct npcm_platform_data
*pdata
;
98 struct dentry
*debugfs
;
104 static void handle_ce(struct mem_ctl_info
*mci
)
106 struct priv_data
*priv
= mci
->pvt_info
;
107 const struct npcm_platform_data
*pdata
;
108 u32 val_h
= 0, val_l
, id
, synd
;
109 u64 addr
= 0, data
= 0;
112 regmap_read(npcm_regmap
, pdata
->ctl_ce_addr_l
, &val_l
);
113 if (pdata
->chip
== NPCM8XX_CHIP
) {
114 regmap_read(npcm_regmap
, pdata
->ctl_ce_addr_h
, &val_h
);
115 val_h
&= pdata
->ce_addr_h_mask
;
117 addr
= ((addr
| val_h
) << 32) | val_l
;
119 regmap_read(npcm_regmap
, pdata
->ctl_ce_data_l
, &val_l
);
120 if (pdata
->chip
== NPCM8XX_CHIP
)
121 regmap_read(npcm_regmap
, pdata
->ctl_ce_data_h
, &val_h
);
122 data
= ((data
| val_h
) << 32) | val_l
;
124 regmap_read(npcm_regmap
, pdata
->ctl_source_id
, &id
);
125 id
= (id
& pdata
->source_id_ce_mask
) >> pdata
->source_id_ce_shift
;
127 regmap_read(npcm_regmap
, pdata
->ctl_ce_synd
, &synd
);
128 synd
= (synd
& pdata
->ce_synd_mask
) >> pdata
->ce_synd_shift
;
130 snprintf(priv
->message
, EDAC_MSG_SIZE
,
131 "addr = 0x%llx, data = 0x%llx, id = 0x%x", addr
, data
, id
);
133 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, 1, addr
>> PAGE_SHIFT
,
134 addr
& ~PAGE_MASK
, synd
, 0, 0, -1, priv
->message
, "");
137 static void handle_ue(struct mem_ctl_info
*mci
)
139 struct priv_data
*priv
= mci
->pvt_info
;
140 const struct npcm_platform_data
*pdata
;
141 u32 val_h
= 0, val_l
, id
, synd
;
142 u64 addr
= 0, data
= 0;
145 regmap_read(npcm_regmap
, pdata
->ctl_ue_addr_l
, &val_l
);
146 if (pdata
->chip
== NPCM8XX_CHIP
) {
147 regmap_read(npcm_regmap
, pdata
->ctl_ue_addr_h
, &val_h
);
148 val_h
&= pdata
->ue_addr_h_mask
;
150 addr
= ((addr
| val_h
) << 32) | val_l
;
152 regmap_read(npcm_regmap
, pdata
->ctl_ue_data_l
, &val_l
);
153 if (pdata
->chip
== NPCM8XX_CHIP
)
154 regmap_read(npcm_regmap
, pdata
->ctl_ue_data_h
, &val_h
);
155 data
= ((data
| val_h
) << 32) | val_l
;
157 regmap_read(npcm_regmap
, pdata
->ctl_source_id
, &id
);
158 id
= (id
& pdata
->source_id_ue_mask
) >> pdata
->source_id_ue_shift
;
160 regmap_read(npcm_regmap
, pdata
->ctl_ue_synd
, &synd
);
161 synd
= (synd
& pdata
->ue_synd_mask
) >> pdata
->ue_synd_shift
;
163 snprintf(priv
->message
, EDAC_MSG_SIZE
,
164 "addr = 0x%llx, data = 0x%llx, id = 0x%x", addr
, data
, id
);
166 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED
, mci
, 1, addr
>> PAGE_SHIFT
,
167 addr
& ~PAGE_MASK
, synd
, 0, 0, -1, priv
->message
, "");
170 static irqreturn_t
edac_ecc_isr(int irq
, void *dev_id
)
172 const struct npcm_platform_data
*pdata
;
173 struct mem_ctl_info
*mci
= dev_id
;
176 pdata
= ((struct priv_data
*)mci
->pvt_info
)->pdata
;
177 regmap_read(npcm_regmap
, pdata
->ctl_int_status
, &status
);
178 if (status
& pdata
->int_status_ce_mask
) {
181 /* acknowledge the CE interrupt */
182 regmap_write(npcm_regmap
, pdata
->ctl_int_ack
,
183 pdata
->int_ack_ce_mask
);
185 } else if (status
& pdata
->int_status_ue_mask
) {
188 /* acknowledge the UE interrupt */
189 regmap_write(npcm_regmap
, pdata
->ctl_int_ack
,
190 pdata
->int_ack_ue_mask
);
198 static ssize_t
force_ecc_error(struct file
*file
, const char __user
*data
,
199 size_t count
, loff_t
*ppos
)
201 struct device
*dev
= file
->private_data
;
202 struct mem_ctl_info
*mci
= to_mci(dev
);
203 struct priv_data
*priv
= mci
->pvt_info
;
204 const struct npcm_platform_data
*pdata
;
209 edac_printk(KERN_INFO
, EDAC_MOD_NAME
,
210 "force an ECC error, type = %d, location = %d, bit = %d\n",
211 priv
->error_type
, priv
->location
, priv
->bit
);
213 /* ensure no pending writes */
214 ret
= regmap_read_poll_timeout(npcm_regmap
, pdata
->ctl_controller_busy
,
215 val
, !(val
& pdata
->controller_busy_mask
),
218 edac_printk(KERN_INFO
, EDAC_MOD_NAME
,
219 "wait pending writes timeout\n");
223 regmap_read(npcm_regmap
, pdata
->ctl_xor_check_bits
, &val
);
224 val
&= ~pdata
->xor_check_bits_mask
;
226 /* write syndrome to XOR_CHECK_BITS */
227 if (priv
->error_type
== ERROR_TYPE_CORRECTABLE
) {
228 if (priv
->location
== ERROR_LOCATION_DATA
&&
229 priv
->bit
> ERROR_BIT_DATA_MAX
) {
230 edac_printk(KERN_INFO
, EDAC_MOD_NAME
,
231 "data bit should not exceed %d (%d)\n",
232 ERROR_BIT_DATA_MAX
, priv
->bit
);
236 if (priv
->location
== ERROR_LOCATION_CHECKCODE
&&
237 priv
->bit
> ERROR_BIT_CHECKCODE_MAX
) {
238 edac_printk(KERN_INFO
, EDAC_MOD_NAME
,
239 "checkcode bit should not exceed %d (%d)\n",
240 ERROR_BIT_CHECKCODE_MAX
, priv
->bit
);
244 syndrome
= priv
->location
? 1 << priv
->bit
245 : data_synd
[priv
->bit
];
247 regmap_write(npcm_regmap
, pdata
->ctl_xor_check_bits
,
248 val
| (syndrome
<< pdata
->xor_check_bits_shift
) |
249 pdata
->writeback_en_mask
);
250 } else if (priv
->error_type
== ERROR_TYPE_UNCORRECTABLE
) {
251 regmap_write(npcm_regmap
, pdata
->ctl_xor_check_bits
,
252 val
| (UE_SYNDROME
<< pdata
->xor_check_bits_shift
));
255 /* force write check */
256 regmap_update_bits(npcm_regmap
, pdata
->ctl_xor_check_bits
,
257 pdata
->fwc_mask
, pdata
->fwc_mask
);
262 static const struct file_operations force_ecc_error_fops
= {
264 .write
= force_ecc_error
,
265 .llseek
= generic_file_llseek
,
269 * Setup debugfs for error injection.
272 * error_type - 0: CE, 1: UE
273 * location - 0: data, 1: checkcode
274 * bit - 0 ~ 63 for data and 0 ~ 7 for checkcode
275 * force_ecc_error - trigger
278 * 1. Inject a correctable error (CE) at checkcode bit 7.
279 * ~# echo 0 > /sys/kernel/debug/edac/npcm-edac/error_type
280 * ~# echo 1 > /sys/kernel/debug/edac/npcm-edac/location
281 * ~# echo 7 > /sys/kernel/debug/edac/npcm-edac/bit
282 * ~# echo 1 > /sys/kernel/debug/edac/npcm-edac/force_ecc_error
284 * 2. Inject an uncorrectable error (UE).
285 * ~# echo 1 > /sys/kernel/debug/edac/npcm-edac/error_type
286 * ~# echo 1 > /sys/kernel/debug/edac/npcm-edac/force_ecc_error
288 static void setup_debugfs(struct mem_ctl_info
*mci
)
290 struct priv_data
*priv
= mci
->pvt_info
;
292 priv
->debugfs
= edac_debugfs_create_dir(mci
->mod_name
);
296 edac_debugfs_create_x8("error_type", 0644, priv
->debugfs
, &priv
->error_type
);
297 edac_debugfs_create_x8("location", 0644, priv
->debugfs
, &priv
->location
);
298 edac_debugfs_create_x8("bit", 0644, priv
->debugfs
, &priv
->bit
);
299 edac_debugfs_create_file("force_ecc_error", 0200, priv
->debugfs
,
300 &mci
->dev
, &force_ecc_error_fops
);
303 static int setup_irq(struct mem_ctl_info
*mci
, struct platform_device
*pdev
)
305 const struct npcm_platform_data
*pdata
;
308 pdata
= ((struct priv_data
*)mci
->pvt_info
)->pdata
;
309 irq
= platform_get_irq(pdev
, 0);
311 edac_printk(KERN_ERR
, EDAC_MOD_NAME
, "IRQ not defined in DTS\n");
315 ret
= devm_request_irq(&pdev
->dev
, irq
, edac_ecc_isr
, 0,
316 dev_name(&pdev
->dev
), mci
);
318 edac_printk(KERN_ERR
, EDAC_MOD_NAME
, "failed to request IRQ\n");
322 /* enable the functional group of ECC and mask the others */
323 regmap_write(npcm_regmap
, pdata
->ctl_int_mask_master
,
324 pdata
->int_mask_master_non_ecc_mask
);
326 if (pdata
->chip
== NPCM8XX_CHIP
)
327 regmap_write(npcm_regmap
, pdata
->ctl_int_mask_ecc
,
328 pdata
->int_mask_ecc_non_event_mask
);
333 static const struct regmap_config npcm_regmap_cfg
= {
339 static int edac_probe(struct platform_device
*pdev
)
341 const struct npcm_platform_data
*pdata
;
342 struct device
*dev
= &pdev
->dev
;
343 struct edac_mc_layer layers
[1];
344 struct mem_ctl_info
*mci
;
345 struct priv_data
*priv
;
350 reg
= devm_platform_ioremap_resource(pdev
, 0);
354 npcm_regmap
= devm_regmap_init_mmio(dev
, reg
, &npcm_regmap_cfg
);
355 if (IS_ERR(npcm_regmap
))
356 return PTR_ERR(npcm_regmap
);
358 pdata
= of_device_get_match_data(dev
);
362 /* bail out if ECC is not enabled */
363 regmap_read(npcm_regmap
, pdata
->ctl_ecc_en
, &val
);
364 if (!(val
& pdata
->ecc_en_mask
)) {
365 edac_printk(KERN_ERR
, EDAC_MOD_NAME
, "ECC is not enabled\n");
369 edac_op_state
= EDAC_OPSTATE_INT
;
371 layers
[0].type
= EDAC_MC_LAYER_ALL_MEM
;
374 mci
= edac_mc_alloc(0, ARRAY_SIZE(layers
), layers
,
375 sizeof(struct priv_data
));
379 mci
->pdev
= &pdev
->dev
;
380 priv
= mci
->pvt_info
;
383 platform_set_drvdata(pdev
, mci
);
385 mci
->mtype_cap
= MEM_FLAG_DDR4
;
386 mci
->edac_ctl_cap
= EDAC_FLAG_SECDED
;
387 mci
->scrub_cap
= SCRUB_FLAG_HW_SRC
;
388 mci
->scrub_mode
= SCRUB_HW_SRC
;
389 mci
->edac_cap
= EDAC_FLAG_SECDED
;
390 mci
->ctl_name
= "npcm_ddr_controller";
391 mci
->dev_name
= dev_name(&pdev
->dev
);
392 mci
->mod_name
= EDAC_MOD_NAME
;
393 mci
->ctl_page_to_phys
= NULL
;
395 rc
= setup_irq(mci
, pdev
);
399 rc
= edac_mc_add_mc(mci
);
403 if (IS_ENABLED(CONFIG_EDAC_DEBUG
) && pdata
->chip
== NPCM8XX_CHIP
)
413 static void edac_remove(struct platform_device
*pdev
)
415 struct mem_ctl_info
*mci
= platform_get_drvdata(pdev
);
416 struct priv_data
*priv
= mci
->pvt_info
;
417 const struct npcm_platform_data
*pdata
;
420 if (IS_ENABLED(CONFIG_EDAC_DEBUG
) && pdata
->chip
== NPCM8XX_CHIP
)
421 edac_debugfs_remove_recursive(priv
->debugfs
);
423 edac_mc_del_mc(&pdev
->dev
);
426 regmap_write(npcm_regmap
, pdata
->ctl_int_mask_master
,
427 pdata
->int_mask_master_global_mask
);
428 regmap_update_bits(npcm_regmap
, pdata
->ctl_ecc_en
, pdata
->ecc_en_mask
, 0);
431 static const struct npcm_platform_data npcm750_edac
= {
432 .chip
= NPCM7XX_CHIP
,
434 /* memory controller registers */
436 .ctl_int_status
= 0x1d0,
437 .ctl_int_ack
= 0x1d4,
438 .ctl_int_mask_master
= 0x1d8,
439 .ctl_ce_addr_l
= 0x188,
440 .ctl_ce_data_l
= 0x190,
441 .ctl_ce_synd
= 0x18c,
442 .ctl_ue_addr_l
= 0x17c,
443 .ctl_ue_data_l
= 0x184,
444 .ctl_ue_synd
= 0x180,
445 .ctl_source_id
= 0x194,
447 /* masks and shifts */
448 .ecc_en_mask
= BIT(24),
449 .int_status_ce_mask
= GENMASK(4, 3),
450 .int_status_ue_mask
= GENMASK(6, 5),
451 .int_ack_ce_mask
= GENMASK(4, 3),
452 .int_ack_ue_mask
= GENMASK(6, 5),
453 .int_mask_master_non_ecc_mask
= GENMASK(30, 7) | GENMASK(2, 0),
454 .int_mask_master_global_mask
= BIT(31),
455 .ce_synd_mask
= GENMASK(6, 0),
457 .ue_synd_mask
= GENMASK(6, 0),
459 .source_id_ce_mask
= GENMASK(29, 16),
460 .source_id_ce_shift
= 16,
461 .source_id_ue_mask
= GENMASK(13, 0),
462 .source_id_ue_shift
= 0,
465 static const struct npcm_platform_data npcm845_edac
= {
466 .chip
= NPCM8XX_CHIP
,
468 /* memory controller registers */
470 .ctl_int_status
= 0x228,
471 .ctl_int_ack
= 0x244,
472 .ctl_int_mask_master
= 0x220,
473 .ctl_int_mask_ecc
= 0x260,
474 .ctl_ce_addr_l
= 0x18c,
475 .ctl_ce_addr_h
= 0x190,
476 .ctl_ce_data_l
= 0x194,
477 .ctl_ce_data_h
= 0x198,
478 .ctl_ce_synd
= 0x190,
479 .ctl_ue_addr_l
= 0x17c,
480 .ctl_ue_addr_h
= 0x180,
481 .ctl_ue_data_l
= 0x184,
482 .ctl_ue_data_h
= 0x188,
483 .ctl_ue_synd
= 0x180,
484 .ctl_source_id
= 0x19c,
485 .ctl_controller_busy
= 0x20c,
486 .ctl_xor_check_bits
= 0x174,
488 /* masks and shifts */
489 .ecc_en_mask
= GENMASK(17, 16),
490 .int_status_ce_mask
= GENMASK(1, 0),
491 .int_status_ue_mask
= GENMASK(3, 2),
492 .int_ack_ce_mask
= GENMASK(1, 0),
493 .int_ack_ue_mask
= GENMASK(3, 2),
494 .int_mask_master_non_ecc_mask
= GENMASK(30, 3) | GENMASK(1, 0),
495 .int_mask_master_global_mask
= BIT(31),
496 .int_mask_ecc_non_event_mask
= GENMASK(8, 4),
497 .ce_addr_h_mask
= GENMASK(1, 0),
498 .ce_synd_mask
= GENMASK(15, 8),
500 .ue_addr_h_mask
= GENMASK(1, 0),
501 .ue_synd_mask
= GENMASK(15, 8),
503 .source_id_ce_mask
= GENMASK(29, 16),
504 .source_id_ce_shift
= 16,
505 .source_id_ue_mask
= GENMASK(13, 0),
506 .source_id_ue_shift
= 0,
507 .controller_busy_mask
= BIT(0),
508 .xor_check_bits_mask
= GENMASK(23, 16),
509 .xor_check_bits_shift
= 16,
510 .writeback_en_mask
= BIT(24),
514 static const struct of_device_id npcm_edac_of_match
[] = {
516 .compatible
= "nuvoton,npcm750-memory-controller",
517 .data
= &npcm750_edac
520 .compatible
= "nuvoton,npcm845-memory-controller",
521 .data
= &npcm845_edac
526 MODULE_DEVICE_TABLE(of
, npcm_edac_of_match
);
528 static struct platform_driver npcm_edac_driver
= {
531 .of_match_table
= npcm_edac_of_match
,
534 .remove_new
= edac_remove
,
537 module_platform_driver(npcm_edac_driver
);
539 MODULE_AUTHOR("Medad CChien <medadyoung@gmail.com>");
540 MODULE_AUTHOR("Marvin Lin <kflin@nuvoton.com>");
541 MODULE_DESCRIPTION("Nuvoton NPCM EDAC Driver");
542 MODULE_LICENSE("GPL");