0001
0002
0003
0004
0005
0006
0007 #include <linux/edac.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/irq.h>
0010 #include <linux/kernel.h>
0011 #include <linux/mm.h>
0012 #include <linux/module.h>
0013 #include <linux/of_device.h>
0014 #include <linux/of_irq.h>
0015 #include <linux/of_platform.h>
0016 #include <linux/types.h>
0017
0018 #include <asm/dcr.h>
0019
0020 #include "edac_module.h"
0021 #include "ppc4xx_edac.h"
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107 #define EDAC_OPSTATE_INT_STR "interrupt"
0108 #define EDAC_OPSTATE_POLL_STR "polled"
0109 #define EDAC_OPSTATE_UNKNOWN_STR "unknown"
0110
0111 #define PPC4XX_EDAC_MODULE_NAME "ppc4xx_edac"
0112 #define PPC4XX_EDAC_MODULE_REVISION "v1.0.0"
0113
0114 #define PPC4XX_EDAC_MESSAGE_SIZE 256
0115
0116
0117
0118
0119 #define ppc4xx_edac_printk(level, fmt, arg...) \
0120 edac_printk(level, "PPC4xx MC", fmt, ##arg)
0121
0122
0123
0124
0125 #define ppc4xx_edac_mc_printk(level, mci, fmt, arg...) \
0126 edac_mc_chipset_printk(mci, level, "PPC4xx", fmt, ##arg)
0127
0128
0129
0130
0131
0132 #define SDRAM_MBCF_SZ_MiB_MIN 4
0133 #define SDRAM_MBCF_SZ_TO_MiB(n) (SDRAM_MBCF_SZ_MiB_MIN \
0134 << (SDRAM_MBCF_SZ_DECODE(n)))
0135 #define SDRAM_MBCF_SZ_TO_PAGES(n) (SDRAM_MBCF_SZ_MiB_MIN \
0136 << (20 - PAGE_SHIFT + \
0137 SDRAM_MBCF_SZ_DECODE(n)))
0138
0139
0140
0141
0142
0143
0144
0145
0146 #define SDRAM_DCR_RESOURCE_LEN 2
0147 #define SDRAM_DCR_ADDR_OFFSET 0
0148 #define SDRAM_DCR_DATA_OFFSET 1
0149
0150
0151
0152
0153 #define INTMAP_ECCDED_INDEX 0
0154 #define INTMAP_ECCSEC_INDEX 1
0155
0156
0157
0158
0159
0160
0161 struct ppc4xx_edac_pdata {
0162 dcr_host_t dcr_host;
0163 struct {
0164 int sec;
0165 int ded;
0166 } irqs;
0167 };
0168
0169
0170
0171
0172
0173 struct ppc4xx_ecc_status {
0174 u32 ecces;
0175 u32 besr;
0176 u32 bearh;
0177 u32 bearl;
0178 u32 wmirq;
0179 };
0180
0181
0182
0183 static int ppc4xx_edac_probe(struct platform_device *device);
0184 static int ppc4xx_edac_remove(struct platform_device *device);
0185
0186
0187
0188
0189
0190
0191
0192 static const struct of_device_id ppc4xx_edac_match[] = {
0193 {
0194 .compatible = "ibm,sdram-4xx-ddr2"
0195 },
0196 { }
0197 };
0198 MODULE_DEVICE_TABLE(of, ppc4xx_edac_match);
0199
0200 static struct platform_driver ppc4xx_edac_driver = {
0201 .probe = ppc4xx_edac_probe,
0202 .remove = ppc4xx_edac_remove,
0203 .driver = {
0204 .name = PPC4XX_EDAC_MODULE_NAME,
0205 .of_match_table = ppc4xx_edac_match,
0206 },
0207 };
0208
0209
0210
0211
0212
0213 static const unsigned ppc4xx_edac_nr_csrows = 2;
0214 static const unsigned ppc4xx_edac_nr_chans = 1;
0215
0216
0217
0218
0219
0220 static const char * const ppc4xx_plb_masters[9] = {
0221 [SDRAM_PLB_M0ID_ICU] = "ICU",
0222 [SDRAM_PLB_M0ID_PCIE0] = "PCI-E 0",
0223 [SDRAM_PLB_M0ID_PCIE1] = "PCI-E 1",
0224 [SDRAM_PLB_M0ID_DMA] = "DMA",
0225 [SDRAM_PLB_M0ID_DCU] = "DCU",
0226 [SDRAM_PLB_M0ID_OPB] = "OPB",
0227 [SDRAM_PLB_M0ID_MAL] = "MAL",
0228 [SDRAM_PLB_M0ID_SEC] = "SEC",
0229 [SDRAM_PLB_M0ID_AHB] = "AHB"
0230 };
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 static inline u32
0243 mfsdram(const dcr_host_t *dcr_host, unsigned int idcr_n)
0244 {
0245 return __mfdcri(dcr_host->base + SDRAM_DCR_ADDR_OFFSET,
0246 dcr_host->base + SDRAM_DCR_DATA_OFFSET,
0247 idcr_n);
0248 }
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259 static inline void
0260 mtsdram(const dcr_host_t *dcr_host, unsigned int idcr_n, u32 value)
0261 {
0262 return __mtdcri(dcr_host->base + SDRAM_DCR_ADDR_OFFSET,
0263 dcr_host->base + SDRAM_DCR_DATA_OFFSET,
0264 idcr_n,
0265 value);
0266 }
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280 static bool
0281 ppc4xx_edac_check_bank_error(const struct ppc4xx_ecc_status *status,
0282 unsigned int bank)
0283 {
0284 switch (bank) {
0285 case 0:
0286 return status->ecces & SDRAM_ECCES_BK0ER;
0287 case 1:
0288 return status->ecces & SDRAM_ECCES_BK1ER;
0289 default:
0290 return false;
0291 }
0292 }
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311 static int
0312 ppc4xx_edac_generate_bank_message(const struct mem_ctl_info *mci,
0313 const struct ppc4xx_ecc_status *status,
0314 char *buffer,
0315 size_t size)
0316 {
0317 int n, total = 0;
0318 unsigned int row, rows;
0319
0320 n = snprintf(buffer, size, "%s: Banks: ", mci->dev_name);
0321
0322 if (n < 0 || n >= size)
0323 goto fail;
0324
0325 buffer += n;
0326 size -= n;
0327 total += n;
0328
0329 for (rows = 0, row = 0; row < mci->nr_csrows; row++) {
0330 if (ppc4xx_edac_check_bank_error(status, row)) {
0331 n = snprintf(buffer, size, "%s%u",
0332 (rows++ ? ", " : ""), row);
0333
0334 if (n < 0 || n >= size)
0335 goto fail;
0336
0337 buffer += n;
0338 size -= n;
0339 total += n;
0340 }
0341 }
0342
0343 n = snprintf(buffer, size, "%s; ", rows ? "" : "None");
0344
0345 if (n < 0 || n >= size)
0346 goto fail;
0347
0348 buffer += n;
0349 size -= n;
0350 total += n;
0351
0352 fail:
0353 return total;
0354 }
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373 static int
0374 ppc4xx_edac_generate_checkbit_message(const struct mem_ctl_info *mci,
0375 const struct ppc4xx_ecc_status *status,
0376 char *buffer,
0377 size_t size)
0378 {
0379 const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
0380 const char *ckber = NULL;
0381
0382 switch (status->ecces & SDRAM_ECCES_CKBER_MASK) {
0383 case SDRAM_ECCES_CKBER_NONE:
0384 ckber = "None";
0385 break;
0386 case SDRAM_ECCES_CKBER_32_ECC_0_3:
0387 ckber = "ECC0:3";
0388 break;
0389 case SDRAM_ECCES_CKBER_32_ECC_4_8:
0390 switch (mfsdram(&pdata->dcr_host, SDRAM_MCOPT1) &
0391 SDRAM_MCOPT1_WDTH_MASK) {
0392 case SDRAM_MCOPT1_WDTH_16:
0393 ckber = "ECC0:3";
0394 break;
0395 case SDRAM_MCOPT1_WDTH_32:
0396 ckber = "ECC4:8";
0397 break;
0398 default:
0399 ckber = "Unknown";
0400 break;
0401 }
0402 break;
0403 case SDRAM_ECCES_CKBER_32_ECC_0_8:
0404 ckber = "ECC0:8";
0405 break;
0406 default:
0407 ckber = "Unknown";
0408 break;
0409 }
0410
0411 return snprintf(buffer, size, "Checkbit Error: %s", ckber);
0412 }
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431 static int
0432 ppc4xx_edac_generate_lane_message(const struct mem_ctl_info *mci,
0433 const struct ppc4xx_ecc_status *status,
0434 char *buffer,
0435 size_t size)
0436 {
0437 int n, total = 0;
0438 unsigned int lane, lanes;
0439 const unsigned int first_lane = 0;
0440 const unsigned int lane_count = 16;
0441
0442 n = snprintf(buffer, size, "; Byte Lane Errors: ");
0443
0444 if (n < 0 || n >= size)
0445 goto fail;
0446
0447 buffer += n;
0448 size -= n;
0449 total += n;
0450
0451 for (lanes = 0, lane = first_lane; lane < lane_count; lane++) {
0452 if ((status->ecces & SDRAM_ECCES_BNCE_ENCODE(lane)) != 0) {
0453 n = snprintf(buffer, size,
0454 "%s%u",
0455 (lanes++ ? ", " : ""), lane);
0456
0457 if (n < 0 || n >= size)
0458 goto fail;
0459
0460 buffer += n;
0461 size -= n;
0462 total += n;
0463 }
0464 }
0465
0466 n = snprintf(buffer, size, "%s; ", lanes ? "" : "None");
0467
0468 if (n < 0 || n >= size)
0469 goto fail;
0470
0471 buffer += n;
0472 size -= n;
0473 total += n;
0474
0475 fail:
0476 return total;
0477 }
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496 static int
0497 ppc4xx_edac_generate_ecc_message(const struct mem_ctl_info *mci,
0498 const struct ppc4xx_ecc_status *status,
0499 char *buffer,
0500 size_t size)
0501 {
0502 int n, total = 0;
0503
0504 n = ppc4xx_edac_generate_bank_message(mci, status, buffer, size);
0505
0506 if (n < 0 || n >= size)
0507 goto fail;
0508
0509 buffer += n;
0510 size -= n;
0511 total += n;
0512
0513 n = ppc4xx_edac_generate_checkbit_message(mci, status, buffer, size);
0514
0515 if (n < 0 || n >= size)
0516 goto fail;
0517
0518 buffer += n;
0519 size -= n;
0520 total += n;
0521
0522 n = ppc4xx_edac_generate_lane_message(mci, status, buffer, size);
0523
0524 if (n < 0 || n >= size)
0525 goto fail;
0526
0527 buffer += n;
0528 size -= n;
0529 total += n;
0530
0531 fail:
0532 return total;
0533 }
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552 static int
0553 ppc4xx_edac_generate_plb_message(const struct mem_ctl_info *mci,
0554 const struct ppc4xx_ecc_status *status,
0555 char *buffer,
0556 size_t size)
0557 {
0558 unsigned int master;
0559 bool read;
0560
0561 if ((status->besr & SDRAM_BESR_MASK) == 0)
0562 return 0;
0563
0564 if ((status->besr & SDRAM_BESR_M0ET_MASK) == SDRAM_BESR_M0ET_NONE)
0565 return 0;
0566
0567 read = ((status->besr & SDRAM_BESR_M0RW_MASK) == SDRAM_BESR_M0RW_READ);
0568
0569 master = SDRAM_BESR_M0ID_DECODE(status->besr);
0570
0571 return snprintf(buffer, size,
0572 "%s error w/ PLB master %u \"%s\"; ",
0573 (read ? "Read" : "Write"),
0574 master,
0575 (((master >= SDRAM_PLB_M0ID_FIRST) &&
0576 (master <= SDRAM_PLB_M0ID_LAST)) ?
0577 ppc4xx_plb_masters[master] : "UNKNOWN"));
0578 }
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593 static void
0594 ppc4xx_edac_generate_message(const struct mem_ctl_info *mci,
0595 const struct ppc4xx_ecc_status *status,
0596 char *buffer,
0597 size_t size)
0598 {
0599 int n;
0600
0601 if (buffer == NULL || size == 0)
0602 return;
0603
0604 n = ppc4xx_edac_generate_ecc_message(mci, status, buffer, size);
0605
0606 if (n < 0 || n >= size)
0607 return;
0608
0609 buffer += n;
0610 size -= n;
0611
0612 ppc4xx_edac_generate_plb_message(mci, status, buffer, size);
0613 }
0614
0615 #ifdef DEBUG
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626 static void
0627 ppc4xx_ecc_dump_status(const struct mem_ctl_info *mci,
0628 const struct ppc4xx_ecc_status *status)
0629 {
0630 char message[PPC4XX_EDAC_MESSAGE_SIZE];
0631
0632 ppc4xx_edac_generate_message(mci, status, message, sizeof(message));
0633
0634 ppc4xx_edac_mc_printk(KERN_INFO, mci,
0635 "\n"
0636 "\tECCES: 0x%08x\n"
0637 "\tWMIRQ: 0x%08x\n"
0638 "\tBESR: 0x%08x\n"
0639 "\tBEAR: 0x%08x%08x\n"
0640 "\t%s\n",
0641 status->ecces,
0642 status->wmirq,
0643 status->besr,
0644 status->bearh,
0645 status->bearl,
0646 message);
0647 }
0648 #endif
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663 static void
0664 ppc4xx_ecc_get_status(const struct mem_ctl_info *mci,
0665 struct ppc4xx_ecc_status *status)
0666 {
0667 const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
0668 const dcr_host_t *dcr_host = &pdata->dcr_host;
0669
0670 status->ecces = mfsdram(dcr_host, SDRAM_ECCES) & SDRAM_ECCES_MASK;
0671 status->wmirq = mfsdram(dcr_host, SDRAM_WMIRQ) & SDRAM_WMIRQ_MASK;
0672 status->besr = mfsdram(dcr_host, SDRAM_BESR) & SDRAM_BESR_MASK;
0673 status->bearl = mfsdram(dcr_host, SDRAM_BEARL);
0674 status->bearh = mfsdram(dcr_host, SDRAM_BEARH);
0675 }
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688 static void
0689 ppc4xx_ecc_clear_status(const struct mem_ctl_info *mci,
0690 const struct ppc4xx_ecc_status *status)
0691 {
0692 const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
0693 const dcr_host_t *dcr_host = &pdata->dcr_host;
0694
0695 mtsdram(dcr_host, SDRAM_ECCES, status->ecces & SDRAM_ECCES_MASK);
0696 mtsdram(dcr_host, SDRAM_WMIRQ, status->wmirq & SDRAM_WMIRQ_MASK);
0697 mtsdram(dcr_host, SDRAM_BESR, status->besr & SDRAM_BESR_MASK);
0698 mtsdram(dcr_host, SDRAM_BEARL, 0);
0699 mtsdram(dcr_host, SDRAM_BEARH, 0);
0700 }
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715 static void
0716 ppc4xx_edac_handle_ce(struct mem_ctl_info *mci,
0717 const struct ppc4xx_ecc_status *status)
0718 {
0719 int row;
0720 char message[PPC4XX_EDAC_MESSAGE_SIZE];
0721
0722 ppc4xx_edac_generate_message(mci, status, message, sizeof(message));
0723
0724 for (row = 0; row < mci->nr_csrows; row++)
0725 if (ppc4xx_edac_check_bank_error(status, row))
0726 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
0727 0, 0, 0,
0728 row, 0, -1,
0729 message, "");
0730 }
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743 static void
0744 ppc4xx_edac_handle_ue(struct mem_ctl_info *mci,
0745 const struct ppc4xx_ecc_status *status)
0746 {
0747 const u64 bear = ((u64)status->bearh << 32 | status->bearl);
0748 const unsigned long page = bear >> PAGE_SHIFT;
0749 const unsigned long offset = bear & ~PAGE_MASK;
0750 int row;
0751 char message[PPC4XX_EDAC_MESSAGE_SIZE];
0752
0753 ppc4xx_edac_generate_message(mci, status, message, sizeof(message));
0754
0755 for (row = 0; row < mci->nr_csrows; row++)
0756 if (ppc4xx_edac_check_bank_error(status, row))
0757 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
0758 page, offset, 0,
0759 row, 0, -1,
0760 message, "");
0761 }
0762
0763
0764
0765
0766
0767
0768
0769
0770
0771
0772
0773 static void
0774 ppc4xx_edac_check(struct mem_ctl_info *mci)
0775 {
0776 #ifdef DEBUG
0777 static unsigned int count;
0778 #endif
0779 struct ppc4xx_ecc_status status;
0780
0781 ppc4xx_ecc_get_status(mci, &status);
0782
0783 #ifdef DEBUG
0784 if (count++ % 30 == 0)
0785 ppc4xx_ecc_dump_status(mci, &status);
0786 #endif
0787
0788 if (status.ecces & SDRAM_ECCES_UE)
0789 ppc4xx_edac_handle_ue(mci, &status);
0790
0791 if (status.ecces & SDRAM_ECCES_CE)
0792 ppc4xx_edac_handle_ce(mci, &status);
0793
0794 ppc4xx_ecc_clear_status(mci, &status);
0795 }
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810 static irqreturn_t
0811 ppc4xx_edac_isr(int irq, void *dev_id)
0812 {
0813 struct mem_ctl_info *mci = dev_id;
0814
0815 ppc4xx_edac_check(mci);
0816
0817 return IRQ_HANDLED;
0818 }
0819
0820
0821
0822
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836
0837 static enum dev_type ppc4xx_edac_get_dtype(u32 mcopt1)
0838 {
0839 switch (mcopt1 & SDRAM_MCOPT1_WDTH_MASK) {
0840 case SDRAM_MCOPT1_WDTH_16:
0841 return DEV_X2;
0842 case SDRAM_MCOPT1_WDTH_32:
0843 return DEV_X4;
0844 default:
0845 return DEV_UNKNOWN;
0846 }
0847 }
0848
0849
0850
0851
0852
0853
0854
0855
0856
0857
0858
0859
0860 static enum mem_type ppc4xx_edac_get_mtype(u32 mcopt1)
0861 {
0862 bool rden = ((mcopt1 & SDRAM_MCOPT1_RDEN_MASK) == SDRAM_MCOPT1_RDEN);
0863
0864 switch (mcopt1 & SDRAM_MCOPT1_DDR_TYPE_MASK) {
0865 case SDRAM_MCOPT1_DDR2_TYPE:
0866 return rden ? MEM_RDDR2 : MEM_DDR2;
0867 case SDRAM_MCOPT1_DDR1_TYPE:
0868 return rden ? MEM_RDDR : MEM_DDR;
0869 default:
0870 return MEM_UNKNOWN;
0871 }
0872 }
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890 static int ppc4xx_edac_init_csrows(struct mem_ctl_info *mci, u32 mcopt1)
0891 {
0892 const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
0893 int status = 0;
0894 enum mem_type mtype;
0895 enum dev_type dtype;
0896 enum edac_type edac_mode;
0897 int row, j;
0898 u32 mbxcf, size, nr_pages;
0899
0900
0901
0902 mtype = ppc4xx_edac_get_mtype(mcopt1);
0903 dtype = ppc4xx_edac_get_dtype(mcopt1);
0904
0905
0906
0907 if (mci->edac_cap & EDAC_FLAG_SECDED)
0908 edac_mode = EDAC_SECDED;
0909 else if (mci->edac_cap & EDAC_FLAG_EC)
0910 edac_mode = EDAC_EC;
0911 else
0912 edac_mode = EDAC_NONE;
0913
0914
0915
0916
0917
0918
0919 for (row = 0; row < mci->nr_csrows; row++) {
0920 struct csrow_info *csi = mci->csrows[row];
0921
0922
0923
0924
0925
0926
0927 mbxcf = mfsdram(&pdata->dcr_host, SDRAM_MBXCF(row));
0928
0929 if ((mbxcf & SDRAM_MBCF_BE_MASK) != SDRAM_MBCF_BE_ENABLE)
0930 continue;
0931
0932
0933
0934 size = mbxcf & SDRAM_MBCF_SZ_MASK;
0935
0936 switch (size) {
0937 case SDRAM_MBCF_SZ_4MB:
0938 case SDRAM_MBCF_SZ_8MB:
0939 case SDRAM_MBCF_SZ_16MB:
0940 case SDRAM_MBCF_SZ_32MB:
0941 case SDRAM_MBCF_SZ_64MB:
0942 case SDRAM_MBCF_SZ_128MB:
0943 case SDRAM_MBCF_SZ_256MB:
0944 case SDRAM_MBCF_SZ_512MB:
0945 case SDRAM_MBCF_SZ_1GB:
0946 case SDRAM_MBCF_SZ_2GB:
0947 case SDRAM_MBCF_SZ_4GB:
0948 case SDRAM_MBCF_SZ_8GB:
0949 nr_pages = SDRAM_MBCF_SZ_TO_PAGES(size);
0950 break;
0951 default:
0952 ppc4xx_edac_mc_printk(KERN_ERR, mci,
0953 "Unrecognized memory bank %d "
0954 "size 0x%08x\n",
0955 row, SDRAM_MBCF_SZ_DECODE(size));
0956 status = -EINVAL;
0957 goto done;
0958 }
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970
0971
0972 for (j = 0; j < csi->nr_channels; j++) {
0973 struct dimm_info *dimm = csi->channels[j]->dimm;
0974
0975 dimm->nr_pages = nr_pages / csi->nr_channels;
0976 dimm->grain = 1;
0977
0978 dimm->mtype = mtype;
0979 dimm->dtype = dtype;
0980
0981 dimm->edac_mode = edac_mode;
0982 }
0983 }
0984
0985 done:
0986 return status;
0987 }
0988
0989
0990
0991
0992
0993
0994
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007 static int ppc4xx_edac_mc_init(struct mem_ctl_info *mci,
1008 struct platform_device *op,
1009 const dcr_host_t *dcr_host, u32 mcopt1)
1010 {
1011 int status = 0;
1012 const u32 memcheck = (mcopt1 & SDRAM_MCOPT1_MCHK_MASK);
1013 struct ppc4xx_edac_pdata *pdata = NULL;
1014 const struct device_node *np = op->dev.of_node;
1015
1016 if (of_match_device(ppc4xx_edac_match, &op->dev) == NULL)
1017 return -EINVAL;
1018
1019
1020
1021 mci->pdev = &op->dev;
1022
1023 dev_set_drvdata(mci->pdev, mci);
1024
1025 pdata = mci->pvt_info;
1026
1027 pdata->dcr_host = *dcr_host;
1028
1029
1030
1031 mci->mtype_cap = (MEM_FLAG_DDR | MEM_FLAG_RDDR |
1032 MEM_FLAG_DDR2 | MEM_FLAG_RDDR2);
1033
1034 mci->edac_ctl_cap = (EDAC_FLAG_NONE |
1035 EDAC_FLAG_EC |
1036 EDAC_FLAG_SECDED);
1037
1038 mci->scrub_cap = SCRUB_NONE;
1039 mci->scrub_mode = SCRUB_NONE;
1040
1041
1042
1043
1044
1045
1046 switch (memcheck) {
1047 case SDRAM_MCOPT1_MCHK_CHK:
1048 mci->edac_cap = EDAC_FLAG_EC;
1049 break;
1050 case SDRAM_MCOPT1_MCHK_CHK_REP:
1051 mci->edac_cap = (EDAC_FLAG_EC | EDAC_FLAG_SECDED);
1052 mci->scrub_mode = SCRUB_SW_SRC;
1053 break;
1054 default:
1055 mci->edac_cap = EDAC_FLAG_NONE;
1056 break;
1057 }
1058
1059
1060
1061 mci->mod_name = PPC4XX_EDAC_MODULE_NAME;
1062 mci->ctl_name = ppc4xx_edac_match->compatible;
1063 mci->dev_name = np->full_name;
1064
1065
1066
1067 mci->edac_check = ppc4xx_edac_check;
1068 mci->ctl_page_to_phys = NULL;
1069
1070
1071
1072 status = ppc4xx_edac_init_csrows(mci, mcopt1);
1073
1074 if (status)
1075 ppc4xx_edac_mc_printk(KERN_ERR, mci,
1076 "Failed to initialize rows!\n");
1077
1078 return status;
1079 }
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096 static int ppc4xx_edac_register_irq(struct platform_device *op,
1097 struct mem_ctl_info *mci)
1098 {
1099 int status = 0;
1100 int ded_irq, sec_irq;
1101 struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
1102 struct device_node *np = op->dev.of_node;
1103
1104 ded_irq = irq_of_parse_and_map(np, INTMAP_ECCDED_INDEX);
1105 sec_irq = irq_of_parse_and_map(np, INTMAP_ECCSEC_INDEX);
1106
1107 if (!ded_irq || !sec_irq) {
1108 ppc4xx_edac_mc_printk(KERN_ERR, mci,
1109 "Unable to map interrupts.\n");
1110 status = -ENODEV;
1111 goto fail;
1112 }
1113
1114 status = request_irq(ded_irq,
1115 ppc4xx_edac_isr,
1116 0,
1117 "[EDAC] MC ECCDED",
1118 mci);
1119
1120 if (status < 0) {
1121 ppc4xx_edac_mc_printk(KERN_ERR, mci,
1122 "Unable to request irq %d for ECC DED",
1123 ded_irq);
1124 status = -ENODEV;
1125 goto fail1;
1126 }
1127
1128 status = request_irq(sec_irq,
1129 ppc4xx_edac_isr,
1130 0,
1131 "[EDAC] MC ECCSEC",
1132 mci);
1133
1134 if (status < 0) {
1135 ppc4xx_edac_mc_printk(KERN_ERR, mci,
1136 "Unable to request irq %d for ECC SEC",
1137 sec_irq);
1138 status = -ENODEV;
1139 goto fail2;
1140 }
1141
1142 ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCDED irq is %d\n", ded_irq);
1143 ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCSEC irq is %d\n", sec_irq);
1144
1145 pdata->irqs.ded = ded_irq;
1146 pdata->irqs.sec = sec_irq;
1147
1148 return 0;
1149
1150 fail2:
1151 free_irq(sec_irq, mci);
1152
1153 fail1:
1154 free_irq(ded_irq, mci);
1155
1156 fail:
1157 return status;
1158 }
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174 static int ppc4xx_edac_map_dcrs(const struct device_node *np,
1175 dcr_host_t *dcr_host)
1176 {
1177 unsigned int dcr_base, dcr_len;
1178
1179 if (np == NULL || dcr_host == NULL)
1180 return -EINVAL;
1181
1182
1183
1184 dcr_base = dcr_resource_start(np, 0);
1185 dcr_len = dcr_resource_len(np, 0);
1186
1187 if (dcr_base == 0 || dcr_len == 0) {
1188 ppc4xx_edac_printk(KERN_ERR,
1189 "Failed to obtain DCR property.\n");
1190 return -ENODEV;
1191 }
1192
1193 if (dcr_len != SDRAM_DCR_RESOURCE_LEN) {
1194 ppc4xx_edac_printk(KERN_ERR,
1195 "Unexpected DCR length %d, expected %d.\n",
1196 dcr_len, SDRAM_DCR_RESOURCE_LEN);
1197 return -ENODEV;
1198 }
1199
1200
1201
1202 *dcr_host = dcr_map(np, dcr_base, dcr_len);
1203
1204 if (!DCR_MAP_OK(*dcr_host)) {
1205 ppc4xx_edac_printk(KERN_INFO, "Failed to map DCRs.\n");
1206 return -ENODEV;
1207 }
1208
1209 return 0;
1210 }
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223 static int ppc4xx_edac_probe(struct platform_device *op)
1224 {
1225 int status = 0;
1226 u32 mcopt1, memcheck;
1227 dcr_host_t dcr_host;
1228 const struct device_node *np = op->dev.of_node;
1229 struct mem_ctl_info *mci = NULL;
1230 struct edac_mc_layer layers[2];
1231 static int ppc4xx_edac_instance;
1232
1233
1234
1235
1236
1237
1238 if (!of_device_is_compatible(np, "ibm,sdram-405ex") &&
1239 !of_device_is_compatible(np, "ibm,sdram-405exr")) {
1240 ppc4xx_edac_printk(KERN_NOTICE,
1241 "Only the PPC405EX[r] is supported.\n");
1242 return -ENODEV;
1243 }
1244
1245
1246
1247
1248
1249
1250 status = ppc4xx_edac_map_dcrs(np, &dcr_host);
1251
1252 if (status)
1253 return status;
1254
1255
1256
1257
1258
1259
1260
1261 mcopt1 = mfsdram(&dcr_host, SDRAM_MCOPT1);
1262 memcheck = (mcopt1 & SDRAM_MCOPT1_MCHK_MASK);
1263
1264 if (memcheck == SDRAM_MCOPT1_MCHK_NON) {
1265 ppc4xx_edac_printk(KERN_INFO, "%pOF: No ECC memory detected or "
1266 "ECC is disabled.\n", np);
1267 status = -ENODEV;
1268 goto done;
1269 }
1270
1271
1272
1273
1274
1275
1276 layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
1277 layers[0].size = ppc4xx_edac_nr_csrows;
1278 layers[0].is_virt_csrow = true;
1279 layers[1].type = EDAC_MC_LAYER_CHANNEL;
1280 layers[1].size = ppc4xx_edac_nr_chans;
1281 layers[1].is_virt_csrow = false;
1282 mci = edac_mc_alloc(ppc4xx_edac_instance, ARRAY_SIZE(layers), layers,
1283 sizeof(struct ppc4xx_edac_pdata));
1284 if (mci == NULL) {
1285 ppc4xx_edac_printk(KERN_ERR, "%pOF: "
1286 "Failed to allocate EDAC MC instance!\n",
1287 np);
1288 status = -ENOMEM;
1289 goto done;
1290 }
1291
1292 status = ppc4xx_edac_mc_init(mci, op, &dcr_host, mcopt1);
1293
1294 if (status) {
1295 ppc4xx_edac_mc_printk(KERN_ERR, mci,
1296 "Failed to initialize instance!\n");
1297 goto fail;
1298 }
1299
1300
1301
1302
1303
1304
1305
1306 if (edac_mc_add_mc(mci)) {
1307 ppc4xx_edac_mc_printk(KERN_ERR, mci,
1308 "Failed to add instance!\n");
1309 status = -ENODEV;
1310 goto fail;
1311 }
1312
1313 if (edac_op_state == EDAC_OPSTATE_INT) {
1314 status = ppc4xx_edac_register_irq(op, mci);
1315
1316 if (status)
1317 goto fail1;
1318 }
1319
1320 ppc4xx_edac_instance++;
1321
1322 return 0;
1323
1324 fail1:
1325 edac_mc_del_mc(mci->pdev);
1326
1327 fail:
1328 edac_mc_free(mci);
1329
1330 done:
1331 return status;
1332 }
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346 static int
1347 ppc4xx_edac_remove(struct platform_device *op)
1348 {
1349 struct mem_ctl_info *mci = dev_get_drvdata(&op->dev);
1350 struct ppc4xx_edac_pdata *pdata = mci->pvt_info;
1351
1352 if (edac_op_state == EDAC_OPSTATE_INT) {
1353 free_irq(pdata->irqs.sec, mci);
1354 free_irq(pdata->irqs.ded, mci);
1355 }
1356
1357 dcr_unmap(pdata->dcr_host, SDRAM_DCR_RESOURCE_LEN);
1358
1359 edac_mc_del_mc(mci->pdev);
1360 edac_mc_free(mci);
1361
1362 return 0;
1363 }
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374 static inline void __init
1375 ppc4xx_edac_opstate_init(void)
1376 {
1377 switch (edac_op_state) {
1378 case EDAC_OPSTATE_POLL:
1379 case EDAC_OPSTATE_INT:
1380 break;
1381 default:
1382 edac_op_state = EDAC_OPSTATE_INT;
1383 break;
1384 }
1385
1386 ppc4xx_edac_printk(KERN_INFO, "Reporting type: %s\n",
1387 ((edac_op_state == EDAC_OPSTATE_POLL) ?
1388 EDAC_OPSTATE_POLL_STR :
1389 ((edac_op_state == EDAC_OPSTATE_INT) ?
1390 EDAC_OPSTATE_INT_STR :
1391 EDAC_OPSTATE_UNKNOWN_STR)));
1392 }
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402 static int __init
1403 ppc4xx_edac_init(void)
1404 {
1405 ppc4xx_edac_printk(KERN_INFO, PPC4XX_EDAC_MODULE_REVISION "\n");
1406
1407 ppc4xx_edac_opstate_init();
1408
1409 return platform_driver_register(&ppc4xx_edac_driver);
1410 }
1411
1412
1413
1414
1415
1416
1417
1418
1419 static void __exit
1420 ppc4xx_edac_exit(void)
1421 {
1422 platform_driver_unregister(&ppc4xx_edac_driver);
1423 }
1424
1425 module_init(ppc4xx_edac_init);
1426 module_exit(ppc4xx_edac_exit);
1427
1428 MODULE_LICENSE("GPL v2");
1429 MODULE_AUTHOR("Grant Erickson <gerickson@nuovations.com>");
1430 MODULE_DESCRIPTION("EDAC MC Driver for the PPC4xx IBM DDR2 Memory Controller");
1431 module_param(edac_op_state, int, 0444);
1432 MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting State: "
1433 "0=" EDAC_OPSTATE_POLL_STR ", 2=" EDAC_OPSTATE_INT_STR);