Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2008 Nuovation System Designs, LLC
0004  *   Grant Erickson <gerickson@nuovations.com>
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  * This file implements a driver for monitoring and handling events
0025  * associated with the IMB DDR2 ECC controller found in the AMCC/IBM
0026  * 405EX[r], 440SP, 440SPe, 460EX, 460GT and 460SX.
0027  *
0028  * As realized in the 405EX[r], this controller features:
0029  *
0030  *   - Support for registered- and non-registered DDR1 and DDR2 memory.
0031  *   - 32-bit or 16-bit memory interface with optional ECC.
0032  *
0033  *     o ECC support includes:
0034  *
0035  *       - 4-bit SEC/DED
0036  *       - Aligned-nibble error detect
0037  *       - Bypass mode
0038  *
0039  *   - Two (2) memory banks/ranks.
0040  *   - Up to 1 GiB per bank/rank in 32-bit mode and up to 512 MiB per
0041  *     bank/rank in 16-bit mode.
0042  *
0043  * As realized in the 440SP and 440SPe, this controller changes/adds:
0044  *
0045  *   - 64-bit or 32-bit memory interface with optional ECC.
0046  *
0047  *     o ECC support includes:
0048  *
0049  *       - 8-bit SEC/DED
0050  *       - Aligned-nibble error detect
0051  *       - Bypass mode
0052  *
0053  *   - Up to 4 GiB per bank/rank in 64-bit mode and up to 2 GiB
0054  *     per bank/rank in 32-bit mode.
0055  *
0056  * As realized in the 460EX and 460GT, this controller changes/adds:
0057  *
0058  *   - 64-bit or 32-bit memory interface with optional ECC.
0059  *
0060  *     o ECC support includes:
0061  *
0062  *       - 8-bit SEC/DED
0063  *       - Aligned-nibble error detect
0064  *       - Bypass mode
0065  *
0066  *   - Four (4) memory banks/ranks.
0067  *   - Up to 16 GiB per bank/rank in 64-bit mode and up to 8 GiB
0068  *     per bank/rank in 32-bit mode.
0069  *
0070  * At present, this driver has ONLY been tested against the controller
0071  * realization in the 405EX[r] on the AMCC Kilauea and Haleakala
0072  * boards (256 MiB w/o ECC memory soldered onto the board) and a
0073  * proprietary board based on those designs (128 MiB ECC memory, also
0074  * soldered onto the board).
0075  *
0076  * Dynamic feature detection and handling needs to be added for the
0077  * other realizations of this controller listed above.
0078  *
0079  * Eventually, this driver will likely be adapted to the above variant
0080  * realizations of this controller as well as broken apart to handle
0081  * the other known ECC-capable controllers prevalent in other 4xx
0082  * processors:
0083  *
0084  *   - IBM SDRAM (405GP, 405CR and 405EP) "ibm,sdram-4xx"
0085  *   - IBM DDR1 (440GP, 440GX, 440EP and 440GR) "ibm,sdram-4xx-ddr"
0086  *   - Denali DDR1/DDR2 (440EPX and 440GRX) "denali,sdram-4xx-ddr2"
0087  *
0088  * For this controller, unfortunately, correctable errors report
0089  * nothing more than the beat/cycle and byte/lane the correction
0090  * occurred on and the check bit group that covered the error.
0091  *
0092  * In contrast, uncorrectable errors also report the failing address,
0093  * the bus master and the transaction direction (i.e. read or write)
0094  *
0095  * Regardless of whether the error is a CE or a UE, we report the
0096  * following pieces of information in the driver-unique message to the
0097  * EDAC subsystem:
0098  *
0099  *   - Device tree path
0100  *   - Bank(s)
0101  *   - Check bit error group
0102  *   - Beat(s)/lane(s)
0103  */
0104 
0105 /* Preprocessor Definitions */
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  * Kernel logging without an EDAC instance
0118  */
0119 #define ppc4xx_edac_printk(level, fmt, arg...) \
0120     edac_printk(level, "PPC4xx MC", fmt, ##arg)
0121 
0122 /*
0123  * Kernel logging with an EDAC instance
0124  */
0125 #define ppc4xx_edac_mc_printk(level, mci, fmt, arg...) \
0126     edac_mc_chipset_printk(mci, level, "PPC4xx", fmt, ##arg)
0127 
0128 /*
0129  * Macros to convert bank configuration size enumerations into MiB and
0130  * page values.
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  * The ibm,sdram-4xx-ddr2 Device Control Registers (DCRs) are
0141  * indirectly accessed and have a base and length defined by the
0142  * device tree. The base can be anything; however, we expect the
0143  * length to be precisely two registers, the first for the address
0144  * window and the second for the data window.
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  * Device tree interrupt indices
0152  */
0153 #define INTMAP_ECCDED_INDEX     0   /* Double-bit Error Detect */
0154 #define INTMAP_ECCSEC_INDEX     1   /* Single-bit Error Correct */
0155 
0156 /* Type Definitions */
0157 
0158 /*
0159  * PPC4xx SDRAM memory controller private instance data
0160  */
0161 struct ppc4xx_edac_pdata {
0162     dcr_host_t dcr_host;    /* Indirect DCR address/data window mapping */
0163     struct {
0164         int sec;    /* Single-bit correctable error IRQ assigned */
0165         int ded;    /* Double-bit detectable error IRQ assigned */
0166     } irqs;
0167 };
0168 
0169 /*
0170  * Various status data gathered and manipulated when checking and
0171  * reporting ECC status.
0172  */
0173 struct ppc4xx_ecc_status {
0174     u32 ecces;
0175     u32 besr;
0176     u32 bearh;
0177     u32 bearl;
0178     u32 wmirq;
0179 };
0180 
0181 /* Function Prototypes */
0182 
0183 static int ppc4xx_edac_probe(struct platform_device *device);
0184 static int ppc4xx_edac_remove(struct platform_device *device);
0185 
0186 /* Global Variables */
0187 
0188 /*
0189  * Device tree node type and compatible tuples this driver can match
0190  * on.
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  * TODO: The row and channel parameters likely need to be dynamically
0211  * set based on the aforementioned variant controller realizations.
0212  */
0213 static const unsigned ppc4xx_edac_nr_csrows = 2;
0214 static const unsigned ppc4xx_edac_nr_chans = 1;
0215 
0216 /*
0217  * Strings associated with PLB master IDs capable of being posted in
0218  * SDRAM_BESR or SDRAM_WMIRQ on uncorrectable ECC errors.
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  * mfsdram - read and return controller register data
0234  * @dcr_host: A pointer to the DCR mapping.
0235  * @idcr_n: The indirect DCR register to read.
0236  *
0237  * This routine reads and returns the data associated with the
0238  * controller's specified indirect DCR register.
0239  *
0240  * Returns the read data.
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  * mtsdram - write controller register data
0252  * @dcr_host: A pointer to the DCR mapping.
0253  * @idcr_n: The indirect DCR register to write.
0254  * @value: The data to write.
0255  *
0256  * This routine writes the provided data to the controller's specified
0257  * indirect DCR register.
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  * ppc4xx_edac_check_bank_error - check a bank for an ECC bank error
0270  * @status: A pointer to the ECC status structure to check for an
0271  *          ECC bank error.
0272  * @bank: The bank to check for an ECC error.
0273  *
0274  * This routine determines whether the specified bank has an ECC
0275  * error.
0276  *
0277  * Returns true if the specified bank has an ECC error; otherwise,
0278  * false.
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  * ppc4xx_edac_generate_bank_message - generate interpretted bank status message
0296  * @mci: A pointer to the EDAC memory controller instance associated
0297  *       with the bank message being generated.
0298  * @status: A pointer to the ECC status structure to generate the
0299  *          message from.
0300  * @buffer: A pointer to the buffer in which to generate the
0301  *          message.
0302  * @size: The size, in bytes, of space available in buffer.
0303  *
0304  * This routine generates to the provided buffer the portion of the
0305  * driver-unique report message associated with the ECCESS[BKNER]
0306  * field of the specified ECC status.
0307  *
0308  * Returns the number of characters generated on success; otherwise, <
0309  * 0 on error.
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  * ppc4xx_edac_generate_checkbit_message - generate interpretted checkbit message
0358  * @mci: A pointer to the EDAC memory controller instance associated
0359  *       with the checkbit message being generated.
0360  * @status: A pointer to the ECC status structure to generate the
0361  *          message from.
0362  * @buffer: A pointer to the buffer in which to generate the
0363  *          message.
0364  * @size: The size, in bytes, of space available in buffer.
0365  *
0366  * This routine generates to the provided buffer the portion of the
0367  * driver-unique report message associated with the ECCESS[CKBER]
0368  * field of the specified ECC status.
0369  *
0370  * Returns the number of characters generated on success; otherwise, <
0371  * 0 on error.
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  * ppc4xx_edac_generate_lane_message - generate interpretted byte lane message
0416  * @mci: A pointer to the EDAC memory controller instance associated
0417  *       with the byte lane message being generated.
0418  * @status: A pointer to the ECC status structure to generate the
0419  *          message from.
0420  * @buffer: A pointer to the buffer in which to generate the
0421  *          message.
0422  * @size: The size, in bytes, of space available in buffer.
0423  *
0424  * This routine generates to the provided buffer the portion of the
0425  * driver-unique report message associated with the ECCESS[BNCE]
0426  * field of the specified ECC status.
0427  *
0428  * Returns the number of characters generated on success; otherwise, <
0429  * 0 on error.
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  * ppc4xx_edac_generate_ecc_message - generate interpretted ECC status message
0481  * @mci: A pointer to the EDAC memory controller instance associated
0482  *       with the ECCES message being generated.
0483  * @status: A pointer to the ECC status structure to generate the
0484  *          message from.
0485  * @buffer: A pointer to the buffer in which to generate the
0486  *          message.
0487  * @size: The size, in bytes, of space available in buffer.
0488  *
0489  * This routine generates to the provided buffer the portion of the
0490  * driver-unique report message associated with the ECCESS register of
0491  * the specified ECC status.
0492  *
0493  * Returns the number of characters generated on success; otherwise, <
0494  * 0 on error.
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  * ppc4xx_edac_generate_plb_message - generate interpretted PLB status message
0537  * @mci: A pointer to the EDAC memory controller instance associated
0538  *       with the PLB message being generated.
0539  * @status: A pointer to the ECC status structure to generate the
0540  *          message from.
0541  * @buffer: A pointer to the buffer in which to generate the
0542  *          message.
0543  * @size: The size, in bytes, of space available in buffer.
0544  *
0545  * This routine generates to the provided buffer the portion of the
0546  * driver-unique report message associated with the PLB-related BESR
0547  * and/or WMIRQ registers of the specified ECC status.
0548  *
0549  * Returns the number of characters generated on success; otherwise, <
0550  * 0 on error.
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  * ppc4xx_edac_generate_message - generate interpretted status message
0582  * @mci: A pointer to the EDAC memory controller instance associated
0583  *       with the driver-unique message being generated.
0584  * @status: A pointer to the ECC status structure to generate the
0585  *          message from.
0586  * @buffer: A pointer to the buffer in which to generate the
0587  *          message.
0588  * @size: The size, in bytes, of space available in buffer.
0589  *
0590  * This routine generates to the provided buffer the driver-unique
0591  * EDAC report message from the specified ECC status.
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  * ppc4xx_ecc_dump_status - dump controller ECC status registers
0618  * @mci: A pointer to the EDAC memory controller instance
0619  *       associated with the status being dumped.
0620  * @status: A pointer to the ECC status structure to generate the
0621  *          dump from.
0622  *
0623  * This routine dumps to the kernel log buffer the raw and
0624  * interpretted specified ECC status.
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 /* DEBUG */
0649 
0650 /**
0651  * ppc4xx_ecc_get_status - get controller ECC status
0652  * @mci: A pointer to the EDAC memory controller instance
0653  *       associated with the status being retrieved.
0654  * @status: A pointer to the ECC status structure to populate the
0655  *          ECC status with.
0656  *
0657  * This routine reads and masks, as appropriate, all the relevant
0658  * status registers that deal with ibm,sdram-4xx-ddr2 ECC errors.
0659  * While we read all of them, for correctable errors, we only expect
0660  * to deal with ECCES. For uncorrectable errors, we expect to deal
0661  * with all of them.
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  * ppc4xx_ecc_clear_status - clear controller ECC status
0679  * @mci: A pointer to the EDAC memory controller instance
0680  *       associated with the status being cleared.
0681  * @status: A pointer to the ECC status structure containing the
0682  *          values to write to clear the ECC status.
0683  *
0684  * This routine clears--by writing the masked (as appropriate) status
0685  * values back to--the status registers that deal with
0686  * ibm,sdram-4xx-ddr2 ECC errors.
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  * ppc4xx_edac_handle_ce - handle controller correctable ECC error (CE)
0704  * @mci: A pointer to the EDAC memory controller instance
0705  *       associated with the correctable error being handled and reported.
0706  * @status: A pointer to the ECC status structure associated with
0707  *          the correctable error being handled and reported.
0708  *
0709  * This routine handles an ibm,sdram-4xx-ddr2 controller ECC
0710  * correctable error. Per the aforementioned discussion, there's not
0711  * enough status available to use the full EDAC correctable error
0712  * interface, so we just pass driver-unique message to the "no info"
0713  * interface.
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  * ppc4xx_edac_handle_ue - handle controller uncorrectable ECC error (UE)
0734  * @mci: A pointer to the EDAC memory controller instance
0735  *       associated with the uncorrectable error being handled and
0736  *       reported.
0737  * @status: A pointer to the ECC status structure associated with
0738  *          the uncorrectable error being handled and reported.
0739  *
0740  * This routine handles an ibm,sdram-4xx-ddr2 controller ECC
0741  * uncorrectable error.
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  * ppc4xx_edac_check - check controller for ECC errors
0765  * @mci: A pointer to the EDAC memory controller instance
0766  *       associated with the ibm,sdram-4xx-ddr2 controller being
0767  *       checked.
0768  *
0769  * This routine is used to check and post ECC errors and is called by
0770  * both the EDAC polling thread and this driver's CE and UE interrupt
0771  * handler.
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  * ppc4xx_edac_isr - SEC (CE) and DED (UE) interrupt service routine
0799  * @irq:    The virtual interrupt number being serviced.
0800  * @dev_id: A pointer to the EDAC memory controller instance
0801  *          associated with the interrupt being handled.
0802  *
0803  * This routine implements the interrupt handler for both correctable
0804  * (CE) and uncorrectable (UE) ECC errors for the ibm,sdram-4xx-ddr2
0805  * controller. It simply calls through to the same routine used during
0806  * polling to check, report and clear the ECC status.
0807  *
0808  * Unconditionally returns IRQ_HANDLED.
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  * ppc4xx_edac_get_dtype - return the controller memory width
0822  * @mcopt1: The 32-bit Memory Controller Option 1 register value
0823  *          currently set for the controller, from which the width
0824  *          is derived.
0825  *
0826  * This routine returns the EDAC device type width appropriate for the
0827  * current controller configuration.
0828  *
0829  * TODO: This needs to be conditioned dynamically through feature
0830  * flags or some such when other controller variants are supported as
0831  * the 405EX[r] is 16-/32-bit and the others are 32-/64-bit with the
0832  * 16- and 64-bit field definition/value/enumeration (b1) overloaded
0833  * among them.
0834  *
0835  * Returns a device type width enumeration.
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  * ppc4xx_edac_get_mtype - return controller memory type
0851  * @mcopt1: The 32-bit Memory Controller Option 1 register value
0852  *          currently set for the controller, from which the memory type
0853  *          is derived.
0854  *
0855  * This routine returns the EDAC memory type appropriate for the
0856  * current controller configuration.
0857  *
0858  * Returns a memory type enumeration.
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  * ppc4xx_edac_init_csrows - initialize driver instance rows
0876  * @mci: A pointer to the EDAC memory controller instance
0877  *       associated with the ibm,sdram-4xx-ddr2 controller for which
0878  *       the csrows (i.e. banks/ranks) are being initialized.
0879  * @mcopt1: The 32-bit Memory Controller Option 1 register value
0880  *          currently set for the controller, from which bank width
0881  *          and memory typ information is derived.
0882  *
0883  * This routine initializes the virtual "chip select rows" associated
0884  * with the EDAC memory controller instance. An ibm,sdram-4xx-ddr2
0885  * controller bank/rank is mapped to a row.
0886  *
0887  * Returns 0 if OK; otherwise, -EINVAL if the memory bank size
0888  * configuration cannot be determined.
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     /* Establish the memory type and width */
0901 
0902     mtype = ppc4xx_edac_get_mtype(mcopt1);
0903     dtype = ppc4xx_edac_get_dtype(mcopt1);
0904 
0905     /* Establish EDAC mode */
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      * Initialize each chip select row structure which correspond
0916      * 1:1 with a controller bank/rank.
0917      */
0918 
0919     for (row = 0; row < mci->nr_csrows; row++) {
0920         struct csrow_info *csi = mci->csrows[row];
0921 
0922         /*
0923          * Get the configuration settings for this
0924          * row/bank/rank and skip disabled banks.
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         /* Map the bank configuration size setting to pages. */
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          * It's unclear exactly what grain should be set to
0962          * here. The SDRAM_ECCES register allows resolution of
0963          * an error down to a nibble which would potentially
0964          * argue for a grain of '1' byte, even though we only
0965          * know the associated address for uncorrectable
0966          * errors. This value is not used at present for
0967          * anything other than error reporting so getting it
0968          * wrong should be of little consequence. Other
0969          * possible values would be the PLB width (16), the
0970          * page size (PAGE_SIZE) or the memory width (2 or 4).
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  * ppc4xx_edac_mc_init - initialize driver instance
0991  * @mci: A pointer to the EDAC memory controller instance being
0992  *       initialized.
0993  * @op: A pointer to the OpenFirmware device tree node associated
0994  *      with the controller this EDAC instance is bound to.
0995  * @dcr_host: A pointer to the DCR data containing the DCR mapping
0996  *            for this controller instance.
0997  * @mcopt1: The 32-bit Memory Controller Option 1 register value
0998  *          currently set for the controller, from which ECC capabilities
0999  *          and scrub mode are derived.
1000  *
1001  * This routine performs initialization of the EDAC memory controller
1002  * instance and related driver-private data associated with the
1003  * ibm,sdram-4xx-ddr2 memory controller the instance is bound to.
1004  *
1005  * Returns 0 if OK; otherwise, < 0 on error.
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     /* Initial driver pointers and private data */
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     /* Initialize controller capabilities and configuration */
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      * Update the actual capabilites based on the MCOPT1[MCHK]
1043      * settings. Scrubbing is only useful if reporting is enabled.
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     /* Initialize strings */
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     /* Initialize callbacks */
1066 
1067     mci->edac_check     = ppc4xx_edac_check;
1068     mci->ctl_page_to_phys   = NULL;
1069 
1070     /* Initialize chip select rows */
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  * ppc4xx_edac_register_irq - setup and register controller interrupts
1083  * @op: A pointer to the OpenFirmware device tree node associated
1084  *      with the controller this EDAC instance is bound to.
1085  * @mci: A pointer to the EDAC memory controller instance
1086  *       associated with the ibm,sdram-4xx-ddr2 controller for which
1087  *       interrupts are being registered.
1088  *
1089  * This routine parses the correctable (CE) and uncorrectable error (UE)
1090  * interrupts from the device tree node and maps and assigns them to
1091  * the associated EDAC memory controller instance.
1092  *
1093  * Returns 0 if OK; otherwise, -ENODEV if the interrupts could not be
1094  * mapped and assigned.
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  * ppc4xx_edac_map_dcrs - locate and map controller registers
1162  * @np: A pointer to the device tree node containing the DCR
1163  *      resources to map.
1164  * @dcr_host: A pointer to the DCR data to populate with the
1165  *            DCR mapping.
1166  *
1167  * This routine attempts to locate in the device tree and map the DCR
1168  * register resources associated with the controller's indirect DCR
1169  * address and data windows.
1170  *
1171  * Returns 0 if the DCRs were successfully mapped; otherwise, < 0 on
1172  * error.
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     /* Get the DCR resource extent and sanity check the values. */
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     /*  Attempt to map the DCR extent. */
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  * ppc4xx_edac_probe - check controller and bind driver
1214  * @op: A pointer to the OpenFirmware device tree node associated
1215  *      with the controller being probed for driver binding.
1216  *
1217  * This routine probes a specific ibm,sdram-4xx-ddr2 controller
1218  * instance for binding with the driver.
1219  *
1220  * Returns 0 if the controller instance was successfully bound to the
1221  * driver; otherwise, < 0 on error.
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      * At this point, we only support the controller realized on
1235      * the AMCC PPC 405EX[r]. Reject anything else.
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      * Next, get the DCR property and attempt to map it so that we
1247      * can probe the controller.
1248      */
1249 
1250     status = ppc4xx_edac_map_dcrs(np, &dcr_host);
1251 
1252     if (status)
1253         return status;
1254 
1255     /*
1256      * First determine whether ECC is enabled at all. If not,
1257      * there is no useful checking or monitoring that can be done
1258      * for this controller.
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      * At this point, we know ECC is enabled, allocate an EDAC
1273      * controller instance and perform the appropriate
1274      * initialization.
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      * We have a valid, initialized EDAC instance bound to the
1302      * controller. Attempt to register it with the EDAC subsystem
1303      * and, if necessary, register interrupts.
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  * ppc4xx_edac_remove - unbind driver from controller
1336  * @op: A pointer to the OpenFirmware device tree node associated
1337  *      with the controller this EDAC instance is to be unbound/removed
1338  *      from.
1339  *
1340  * This routine unbinds the EDAC memory controller instance associated
1341  * with the specified ibm,sdram-4xx-ddr2 controller described by the
1342  * OpenFirmware device tree node passed as a parameter.
1343  *
1344  * Unconditionally returns 0.
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  * ppc4xx_edac_opstate_init - initialize EDAC reporting method
1367  *
1368  * This routine ensures that the EDAC memory controller reporting
1369  * method is mapped to a sane value as the EDAC core defines the value
1370  * to EDAC_OPSTATE_INVAL by default. We don't call the global
1371  * opstate_init as that defaults to polling and we want interrupt as
1372  * the default.
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  * ppc4xx_edac_init - driver/module insertion entry point
1396  *
1397  * This routine is the driver/module insertion entry point. It
1398  * initializes the EDAC memory controller reporting state and
1399  * registers the driver as an OpenFirmware device tree platform
1400  * driver.
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  * ppc4xx_edac_exit - driver/module removal entry point
1414  *
1415  * This routine is the driver/module removal entry point. It
1416  * unregisters the driver as an OpenFirmware device tree platform
1417  * driver.
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);