/*1* Copyright (c) 2008 Nuovation System Designs, LLC2* Grant Erickson <[email protected]>3*4* This program is free software; you can redistribute it and/or5* modify it under the terms of the GNU General Public License as6* published by the Free Software Foundation; version 2 of the7* License.8*9*/1011#include <linux/edac.h>12#include <linux/interrupt.h>13#include <linux/irq.h>14#include <linux/kernel.h>15#include <linux/mm.h>16#include <linux/module.h>17#include <linux/of_device.h>18#include <linux/of_platform.h>19#include <linux/types.h>2021#include <asm/dcr.h>2223#include "edac_core.h"24#include "ppc4xx_edac.h"2526/*27* This file implements a driver for monitoring and handling events28* associated with the IMB DDR2 ECC controller found in the AMCC/IBM29* 405EX[r], 440SP, 440SPe, 460EX, 460GT and 460SX.30*31* As realized in the 405EX[r], this controller features:32*33* - Support for registered- and non-registered DDR1 and DDR2 memory.34* - 32-bit or 16-bit memory interface with optional ECC.35*36* o ECC support includes:37*38* - 4-bit SEC/DED39* - Aligned-nibble error detect40* - Bypass mode41*42* - Two (2) memory banks/ranks.43* - Up to 1 GiB per bank/rank in 32-bit mode and up to 512 MiB per44* bank/rank in 16-bit mode.45*46* As realized in the 440SP and 440SPe, this controller changes/adds:47*48* - 64-bit or 32-bit memory interface with optional ECC.49*50* o ECC support includes:51*52* - 8-bit SEC/DED53* - Aligned-nibble error detect54* - Bypass mode55*56* - Up to 4 GiB per bank/rank in 64-bit mode and up to 2 GiB57* per bank/rank in 32-bit mode.58*59* As realized in the 460EX and 460GT, this controller changes/adds:60*61* - 64-bit or 32-bit memory interface with optional ECC.62*63* o ECC support includes:64*65* - 8-bit SEC/DED66* - Aligned-nibble error detect67* - Bypass mode68*69* - Four (4) memory banks/ranks.70* - Up to 16 GiB per bank/rank in 64-bit mode and up to 8 GiB71* per bank/rank in 32-bit mode.72*73* At present, this driver has ONLY been tested against the controller74* realization in the 405EX[r] on the AMCC Kilauea and Haleakala75* boards (256 MiB w/o ECC memory soldered onto the board) and a76* proprietary board based on those designs (128 MiB ECC memory, also77* soldered onto the board).78*79* Dynamic feature detection and handling needs to be added for the80* other realizations of this controller listed above.81*82* Eventually, this driver will likely be adapted to the above variant83* realizations of this controller as well as broken apart to handle84* the other known ECC-capable controllers prevalent in other 4xx85* processors:86*87* - IBM SDRAM (405GP, 405CR and 405EP) "ibm,sdram-4xx"88* - IBM DDR1 (440GP, 440GX, 440EP and 440GR) "ibm,sdram-4xx-ddr"89* - Denali DDR1/DDR2 (440EPX and 440GRX) "denali,sdram-4xx-ddr2"90*91* For this controller, unfortunately, correctable errors report92* nothing more than the beat/cycle and byte/lane the correction93* occurred on and the check bit group that covered the error.94*95* In contrast, uncorrectable errors also report the failing address,96* the bus master and the transaction direction (i.e. read or write)97*98* Regardless of whether the error is a CE or a UE, we report the99* following pieces of information in the driver-unique message to the100* EDAC subsystem:101*102* - Device tree path103* - Bank(s)104* - Check bit error group105* - Beat(s)/lane(s)106*/107108/* Preprocessor Definitions */109110#define EDAC_OPSTATE_INT_STR "interrupt"111#define EDAC_OPSTATE_POLL_STR "polled"112#define EDAC_OPSTATE_UNKNOWN_STR "unknown"113114#define PPC4XX_EDAC_MODULE_NAME "ppc4xx_edac"115#define PPC4XX_EDAC_MODULE_REVISION "v1.0.0"116117#define PPC4XX_EDAC_MESSAGE_SIZE 256118119/*120* Kernel logging without an EDAC instance121*/122#define ppc4xx_edac_printk(level, fmt, arg...) \123edac_printk(level, "PPC4xx MC", fmt, ##arg)124125/*126* Kernel logging with an EDAC instance127*/128#define ppc4xx_edac_mc_printk(level, mci, fmt, arg...) \129edac_mc_chipset_printk(mci, level, "PPC4xx", fmt, ##arg)130131/*132* Macros to convert bank configuration size enumerations into MiB and133* page values.134*/135#define SDRAM_MBCF_SZ_MiB_MIN 4136#define SDRAM_MBCF_SZ_TO_MiB(n) (SDRAM_MBCF_SZ_MiB_MIN \137<< (SDRAM_MBCF_SZ_DECODE(n)))138#define SDRAM_MBCF_SZ_TO_PAGES(n) (SDRAM_MBCF_SZ_MiB_MIN \139<< (20 - PAGE_SHIFT + \140SDRAM_MBCF_SZ_DECODE(n)))141142/*143* The ibm,sdram-4xx-ddr2 Device Control Registers (DCRs) are144* indirectly acccessed and have a base and length defined by the145* device tree. The base can be anything; however, we expect the146* length to be precisely two registers, the first for the address147* window and the second for the data window.148*/149#define SDRAM_DCR_RESOURCE_LEN 2150#define SDRAM_DCR_ADDR_OFFSET 0151#define SDRAM_DCR_DATA_OFFSET 1152153/*154* Device tree interrupt indices155*/156#define INTMAP_ECCDED_INDEX 0 /* Double-bit Error Detect */157#define INTMAP_ECCSEC_INDEX 1 /* Single-bit Error Correct */158159/* Type Definitions */160161/*162* PPC4xx SDRAM memory controller private instance data163*/164struct ppc4xx_edac_pdata {165dcr_host_t dcr_host; /* Indirect DCR address/data window mapping */166struct {167int sec; /* Single-bit correctable error IRQ assigned */168int ded; /* Double-bit detectable error IRQ assigned */169} irqs;170};171172/*173* Various status data gathered and manipulated when checking and174* reporting ECC status.175*/176struct ppc4xx_ecc_status {177u32 ecces;178u32 besr;179u32 bearh;180u32 bearl;181u32 wmirq;182};183184/* Function Prototypes */185186static int ppc4xx_edac_probe(struct platform_device *device)187static int ppc4xx_edac_remove(struct platform_device *device);188189/* Global Variables */190191/*192* Device tree node type and compatible tuples this driver can match193* on.194*/195static struct of_device_id ppc4xx_edac_match[] = {196{197.compatible = "ibm,sdram-4xx-ddr2"198},199{ }200};201202static struct platform_driver ppc4xx_edac_driver = {203.probe = ppc4xx_edac_probe,204.remove = ppc4xx_edac_remove,205.driver = {206.owner = THIS_MODULE,207.name = PPC4XX_EDAC_MODULE_NAME208.of_match_table = ppc4xx_edac_match,209},210};211212/*213* TODO: The row and channel parameters likely need to be dynamically214* set based on the aforementioned variant controller realizations.215*/216static const unsigned ppc4xx_edac_nr_csrows = 2;217static const unsigned ppc4xx_edac_nr_chans = 1;218219/*220* Strings associated with PLB master IDs capable of being posted in221* SDRAM_BESR or SDRAM_WMIRQ on uncorrectable ECC errors.222*/223static const char * const ppc4xx_plb_masters[9] = {224[SDRAM_PLB_M0ID_ICU] = "ICU",225[SDRAM_PLB_M0ID_PCIE0] = "PCI-E 0",226[SDRAM_PLB_M0ID_PCIE1] = "PCI-E 1",227[SDRAM_PLB_M0ID_DMA] = "DMA",228[SDRAM_PLB_M0ID_DCU] = "DCU",229[SDRAM_PLB_M0ID_OPB] = "OPB",230[SDRAM_PLB_M0ID_MAL] = "MAL",231[SDRAM_PLB_M0ID_SEC] = "SEC",232[SDRAM_PLB_M0ID_AHB] = "AHB"233};234235/**236* mfsdram - read and return controller register data237* @dcr_host: A pointer to the DCR mapping.238* @idcr_n: The indirect DCR register to read.239*240* This routine reads and returns the data associated with the241* controller's specified indirect DCR register.242*243* Returns the read data.244*/245static inline u32246mfsdram(const dcr_host_t *dcr_host, unsigned int idcr_n)247{248return __mfdcri(dcr_host->base + SDRAM_DCR_ADDR_OFFSET,249dcr_host->base + SDRAM_DCR_DATA_OFFSET,250idcr_n);251}252253/**254* mtsdram - write controller register data255* @dcr_host: A pointer to the DCR mapping.256* @idcr_n: The indirect DCR register to write.257* @value: The data to write.258*259* This routine writes the provided data to the controller's specified260* indirect DCR register.261*/262static inline void263mtsdram(const dcr_host_t *dcr_host, unsigned int idcr_n, u32 value)264{265return __mtdcri(dcr_host->base + SDRAM_DCR_ADDR_OFFSET,266dcr_host->base + SDRAM_DCR_DATA_OFFSET,267idcr_n,268value);269}270271/**272* ppc4xx_edac_check_bank_error - check a bank for an ECC bank error273* @status: A pointer to the ECC status structure to check for an274* ECC bank error.275* @bank: The bank to check for an ECC error.276*277* This routine determines whether the specified bank has an ECC278* error.279*280* Returns true if the specified bank has an ECC error; otherwise,281* false.282*/283static bool284ppc4xx_edac_check_bank_error(const struct ppc4xx_ecc_status *status,285unsigned int bank)286{287switch (bank) {288case 0:289return status->ecces & SDRAM_ECCES_BK0ER;290case 1:291return status->ecces & SDRAM_ECCES_BK1ER;292default:293return false;294}295}296297/**298* ppc4xx_edac_generate_bank_message - generate interpretted bank status message299* @mci: A pointer to the EDAC memory controller instance associated300* with the bank message being generated.301* @status: A pointer to the ECC status structure to generate the302* message from.303* @buffer: A pointer to the buffer in which to generate the304* message.305* @size: The size, in bytes, of space available in buffer.306*307* This routine generates to the provided buffer the portion of the308* driver-unique report message associated with the ECCESS[BKNER]309* field of the specified ECC status.310*311* Returns the number of characters generated on success; otherwise, <312* 0 on error.313*/314static int315ppc4xx_edac_generate_bank_message(const struct mem_ctl_info *mci,316const struct ppc4xx_ecc_status *status,317char *buffer,318size_t size)319{320int n, total = 0;321unsigned int row, rows;322323n = snprintf(buffer, size, "%s: Banks: ", mci->dev_name);324325if (n < 0 || n >= size)326goto fail;327328buffer += n;329size -= n;330total += n;331332for (rows = 0, row = 0; row < mci->nr_csrows; row++) {333if (ppc4xx_edac_check_bank_error(status, row)) {334n = snprintf(buffer, size, "%s%u",335(rows++ ? ", " : ""), row);336337if (n < 0 || n >= size)338goto fail;339340buffer += n;341size -= n;342total += n;343}344}345346n = snprintf(buffer, size, "%s; ", rows ? "" : "None");347348if (n < 0 || n >= size)349goto fail;350351buffer += n;352size -= n;353total += n;354355fail:356return total;357}358359/**360* ppc4xx_edac_generate_checkbit_message - generate interpretted checkbit message361* @mci: A pointer to the EDAC memory controller instance associated362* with the checkbit message being generated.363* @status: A pointer to the ECC status structure to generate the364* message from.365* @buffer: A pointer to the buffer in which to generate the366* message.367* @size: The size, in bytes, of space available in buffer.368*369* This routine generates to the provided buffer the portion of the370* driver-unique report message associated with the ECCESS[CKBER]371* field of the specified ECC status.372*373* Returns the number of characters generated on success; otherwise, <374* 0 on error.375*/376static int377ppc4xx_edac_generate_checkbit_message(const struct mem_ctl_info *mci,378const struct ppc4xx_ecc_status *status,379char *buffer,380size_t size)381{382const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;383const char *ckber = NULL;384385switch (status->ecces & SDRAM_ECCES_CKBER_MASK) {386case SDRAM_ECCES_CKBER_NONE:387ckber = "None";388break;389case SDRAM_ECCES_CKBER_32_ECC_0_3:390ckber = "ECC0:3";391break;392case SDRAM_ECCES_CKBER_32_ECC_4_8:393switch (mfsdram(&pdata->dcr_host, SDRAM_MCOPT1) &394SDRAM_MCOPT1_WDTH_MASK) {395case SDRAM_MCOPT1_WDTH_16:396ckber = "ECC0:3";397break;398case SDRAM_MCOPT1_WDTH_32:399ckber = "ECC4:8";400break;401default:402ckber = "Unknown";403break;404}405break;406case SDRAM_ECCES_CKBER_32_ECC_0_8:407ckber = "ECC0:8";408break;409default:410ckber = "Unknown";411break;412}413414return snprintf(buffer, size, "Checkbit Error: %s", ckber);415}416417/**418* ppc4xx_edac_generate_lane_message - generate interpretted byte lane message419* @mci: A pointer to the EDAC memory controller instance associated420* with the byte lane message being generated.421* @status: A pointer to the ECC status structure to generate the422* message from.423* @buffer: A pointer to the buffer in which to generate the424* message.425* @size: The size, in bytes, of space available in buffer.426*427* This routine generates to the provided buffer the portion of the428* driver-unique report message associated with the ECCESS[BNCE]429* field of the specified ECC status.430*431* Returns the number of characters generated on success; otherwise, <432* 0 on error.433*/434static int435ppc4xx_edac_generate_lane_message(const struct mem_ctl_info *mci,436const struct ppc4xx_ecc_status *status,437char *buffer,438size_t size)439{440int n, total = 0;441unsigned int lane, lanes;442const unsigned int first_lane = 0;443const unsigned int lane_count = 16;444445n = snprintf(buffer, size, "; Byte Lane Errors: ");446447if (n < 0 || n >= size)448goto fail;449450buffer += n;451size -= n;452total += n;453454for (lanes = 0, lane = first_lane; lane < lane_count; lane++) {455if ((status->ecces & SDRAM_ECCES_BNCE_ENCODE(lane)) != 0) {456n = snprintf(buffer, size,457"%s%u",458(lanes++ ? ", " : ""), lane);459460if (n < 0 || n >= size)461goto fail;462463buffer += n;464size -= n;465total += n;466}467}468469n = snprintf(buffer, size, "%s; ", lanes ? "" : "None");470471if (n < 0 || n >= size)472goto fail;473474buffer += n;475size -= n;476total += n;477478fail:479return total;480}481482/**483* ppc4xx_edac_generate_ecc_message - generate interpretted ECC status message484* @mci: A pointer to the EDAC memory controller instance associated485* with the ECCES message being generated.486* @status: A pointer to the ECC status structure to generate the487* message from.488* @buffer: A pointer to the buffer in which to generate the489* message.490* @size: The size, in bytes, of space available in buffer.491*492* This routine generates to the provided buffer the portion of the493* driver-unique report message associated with the ECCESS register of494* the specified ECC status.495*496* Returns the number of characters generated on success; otherwise, <497* 0 on error.498*/499static int500ppc4xx_edac_generate_ecc_message(const struct mem_ctl_info *mci,501const struct ppc4xx_ecc_status *status,502char *buffer,503size_t size)504{505int n, total = 0;506507n = ppc4xx_edac_generate_bank_message(mci, status, buffer, size);508509if (n < 0 || n >= size)510goto fail;511512buffer += n;513size -= n;514total += n;515516n = ppc4xx_edac_generate_checkbit_message(mci, status, buffer, size);517518if (n < 0 || n >= size)519goto fail;520521buffer += n;522size -= n;523total += n;524525n = ppc4xx_edac_generate_lane_message(mci, status, buffer, size);526527if (n < 0 || n >= size)528goto fail;529530buffer += n;531size -= n;532total += n;533534fail:535return total;536}537538/**539* ppc4xx_edac_generate_plb_message - generate interpretted PLB status message540* @mci: A pointer to the EDAC memory controller instance associated541* with the PLB message being generated.542* @status: A pointer to the ECC status structure to generate the543* message from.544* @buffer: A pointer to the buffer in which to generate the545* message.546* @size: The size, in bytes, of space available in buffer.547*548* This routine generates to the provided buffer the portion of the549* driver-unique report message associated with the PLB-related BESR550* and/or WMIRQ registers of the specified ECC status.551*552* Returns the number of characters generated on success; otherwise, <553* 0 on error.554*/555static int556ppc4xx_edac_generate_plb_message(const struct mem_ctl_info *mci,557const struct ppc4xx_ecc_status *status,558char *buffer,559size_t size)560{561unsigned int master;562bool read;563564if ((status->besr & SDRAM_BESR_MASK) == 0)565return 0;566567if ((status->besr & SDRAM_BESR_M0ET_MASK) == SDRAM_BESR_M0ET_NONE)568return 0;569570read = ((status->besr & SDRAM_BESR_M0RW_MASK) == SDRAM_BESR_M0RW_READ);571572master = SDRAM_BESR_M0ID_DECODE(status->besr);573574return snprintf(buffer, size,575"%s error w/ PLB master %u \"%s\"; ",576(read ? "Read" : "Write"),577master,578(((master >= SDRAM_PLB_M0ID_FIRST) &&579(master <= SDRAM_PLB_M0ID_LAST)) ?580ppc4xx_plb_masters[master] : "UNKNOWN"));581}582583/**584* ppc4xx_edac_generate_message - generate interpretted status message585* @mci: A pointer to the EDAC memory controller instance associated586* with the driver-unique message being generated.587* @status: A pointer to the ECC status structure to generate the588* message from.589* @buffer: A pointer to the buffer in which to generate the590* message.591* @size: The size, in bytes, of space available in buffer.592*593* This routine generates to the provided buffer the driver-unique594* EDAC report message from the specified ECC status.595*/596static void597ppc4xx_edac_generate_message(const struct mem_ctl_info *mci,598const struct ppc4xx_ecc_status *status,599char *buffer,600size_t size)601{602int n;603604if (buffer == NULL || size == 0)605return;606607n = ppc4xx_edac_generate_ecc_message(mci, status, buffer, size);608609if (n < 0 || n >= size)610return;611612buffer += n;613size -= n;614615ppc4xx_edac_generate_plb_message(mci, status, buffer, size);616}617618#ifdef DEBUG619/**620* ppc4xx_ecc_dump_status - dump controller ECC status registers621* @mci: A pointer to the EDAC memory controller instance622* associated with the status being dumped.623* @status: A pointer to the ECC status structure to generate the624* dump from.625*626* This routine dumps to the kernel log buffer the raw and627* interpretted specified ECC status.628*/629static void630ppc4xx_ecc_dump_status(const struct mem_ctl_info *mci,631const struct ppc4xx_ecc_status *status)632{633char message[PPC4XX_EDAC_MESSAGE_SIZE];634635ppc4xx_edac_generate_message(mci, status, message, sizeof(message));636637ppc4xx_edac_mc_printk(KERN_INFO, mci,638"\n"639"\tECCES: 0x%08x\n"640"\tWMIRQ: 0x%08x\n"641"\tBESR: 0x%08x\n"642"\tBEAR: 0x%08x%08x\n"643"\t%s\n",644status->ecces,645status->wmirq,646status->besr,647status->bearh,648status->bearl,649message);650}651#endif /* DEBUG */652653/**654* ppc4xx_ecc_get_status - get controller ECC status655* @mci: A pointer to the EDAC memory controller instance656* associated with the status being retrieved.657* @status: A pointer to the ECC status structure to populate the658* ECC status with.659*660* This routine reads and masks, as appropriate, all the relevant661* status registers that deal with ibm,sdram-4xx-ddr2 ECC errors.662* While we read all of them, for correctable errors, we only expect663* to deal with ECCES. For uncorrectable errors, we expect to deal664* with all of them.665*/666static void667ppc4xx_ecc_get_status(const struct mem_ctl_info *mci,668struct ppc4xx_ecc_status *status)669{670const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;671const dcr_host_t *dcr_host = &pdata->dcr_host;672673status->ecces = mfsdram(dcr_host, SDRAM_ECCES) & SDRAM_ECCES_MASK;674status->wmirq = mfsdram(dcr_host, SDRAM_WMIRQ) & SDRAM_WMIRQ_MASK;675status->besr = mfsdram(dcr_host, SDRAM_BESR) & SDRAM_BESR_MASK;676status->bearl = mfsdram(dcr_host, SDRAM_BEARL);677status->bearh = mfsdram(dcr_host, SDRAM_BEARH);678}679680/**681* ppc4xx_ecc_clear_status - clear controller ECC status682* @mci: A pointer to the EDAC memory controller instance683* associated with the status being cleared.684* @status: A pointer to the ECC status structure containing the685* values to write to clear the ECC status.686*687* This routine clears--by writing the masked (as appropriate) status688* values back to--the status registers that deal with689* ibm,sdram-4xx-ddr2 ECC errors.690*/691static void692ppc4xx_ecc_clear_status(const struct mem_ctl_info *mci,693const struct ppc4xx_ecc_status *status)694{695const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;696const dcr_host_t *dcr_host = &pdata->dcr_host;697698mtsdram(dcr_host, SDRAM_ECCES, status->ecces & SDRAM_ECCES_MASK);699mtsdram(dcr_host, SDRAM_WMIRQ, status->wmirq & SDRAM_WMIRQ_MASK);700mtsdram(dcr_host, SDRAM_BESR, status->besr & SDRAM_BESR_MASK);701mtsdram(dcr_host, SDRAM_BEARL, 0);702mtsdram(dcr_host, SDRAM_BEARH, 0);703}704705/**706* ppc4xx_edac_handle_ce - handle controller correctable ECC error (CE)707* @mci: A pointer to the EDAC memory controller instance708* associated with the correctable error being handled and reported.709* @status: A pointer to the ECC status structure associated with710* the correctable error being handled and reported.711*712* This routine handles an ibm,sdram-4xx-ddr2 controller ECC713* correctable error. Per the aforementioned discussion, there's not714* enough status available to use the full EDAC correctable error715* interface, so we just pass driver-unique message to the "no info"716* interface.717*/718static void719ppc4xx_edac_handle_ce(struct mem_ctl_info *mci,720const struct ppc4xx_ecc_status *status)721{722int row;723char message[PPC4XX_EDAC_MESSAGE_SIZE];724725ppc4xx_edac_generate_message(mci, status, message, sizeof(message));726727for (row = 0; row < mci->nr_csrows; row++)728if (ppc4xx_edac_check_bank_error(status, row))729edac_mc_handle_ce_no_info(mci, message);730}731732/**733* ppc4xx_edac_handle_ue - handle controller uncorrectable ECC error (UE)734* @mci: A pointer to the EDAC memory controller instance735* associated with the uncorrectable error being handled and736* reported.737* @status: A pointer to the ECC status structure associated with738* the uncorrectable error being handled and reported.739*740* This routine handles an ibm,sdram-4xx-ddr2 controller ECC741* uncorrectable error.742*/743static void744ppc4xx_edac_handle_ue(struct mem_ctl_info *mci,745const struct ppc4xx_ecc_status *status)746{747const u64 bear = ((u64)status->bearh << 32 | status->bearl);748const unsigned long page = bear >> PAGE_SHIFT;749const unsigned long offset = bear & ~PAGE_MASK;750int row;751char message[PPC4XX_EDAC_MESSAGE_SIZE];752753ppc4xx_edac_generate_message(mci, status, message, sizeof(message));754755for (row = 0; row < mci->nr_csrows; row++)756if (ppc4xx_edac_check_bank_error(status, row))757edac_mc_handle_ue(mci, page, offset, row, message);758}759760/**761* ppc4xx_edac_check - check controller for ECC errors762* @mci: A pointer to the EDAC memory controller instance763* associated with the ibm,sdram-4xx-ddr2 controller being764* checked.765*766* This routine is used to check and post ECC errors and is called by767* both the EDAC polling thread and this driver's CE and UE interrupt768* handler.769*/770static void771ppc4xx_edac_check(struct mem_ctl_info *mci)772{773#ifdef DEBUG774static unsigned int count;775#endif776struct ppc4xx_ecc_status status;777778ppc4xx_ecc_get_status(mci, &status);779780#ifdef DEBUG781if (count++ % 30 == 0)782ppc4xx_ecc_dump_status(mci, &status);783#endif784785if (status.ecces & SDRAM_ECCES_UE)786ppc4xx_edac_handle_ue(mci, &status);787788if (status.ecces & SDRAM_ECCES_CE)789ppc4xx_edac_handle_ce(mci, &status);790791ppc4xx_ecc_clear_status(mci, &status);792}793794/**795* ppc4xx_edac_isr - SEC (CE) and DED (UE) interrupt service routine796* @irq: The virtual interrupt number being serviced.797* @dev_id: A pointer to the EDAC memory controller instance798* associated with the interrupt being handled.799*800* This routine implements the interrupt handler for both correctable801* (CE) and uncorrectable (UE) ECC errors for the ibm,sdram-4xx-ddr2802* controller. It simply calls through to the same routine used during803* polling to check, report and clear the ECC status.804*805* Unconditionally returns IRQ_HANDLED.806*/807static irqreturn_t808ppc4xx_edac_isr(int irq, void *dev_id)809{810struct mem_ctl_info *mci = dev_id;811812ppc4xx_edac_check(mci);813814return IRQ_HANDLED;815}816817/**818* ppc4xx_edac_get_dtype - return the controller memory width819* @mcopt1: The 32-bit Memory Controller Option 1 register value820* currently set for the controller, from which the width821* is derived.822*823* This routine returns the EDAC device type width appropriate for the824* current controller configuration.825*826* TODO: This needs to be conditioned dynamically through feature827* flags or some such when other controller variants are supported as828* the 405EX[r] is 16-/32-bit and the others are 32-/64-bit with the829* 16- and 64-bit field definition/value/enumeration (b1) overloaded830* among them.831*832* Returns a device type width enumeration.833*/834static enum dev_type __devinit835ppc4xx_edac_get_dtype(u32 mcopt1)836{837switch (mcopt1 & SDRAM_MCOPT1_WDTH_MASK) {838case SDRAM_MCOPT1_WDTH_16:839return DEV_X2;840case SDRAM_MCOPT1_WDTH_32:841return DEV_X4;842default:843return DEV_UNKNOWN;844}845}846847/**848* ppc4xx_edac_get_mtype - return controller memory type849* @mcopt1: The 32-bit Memory Controller Option 1 register value850* currently set for the controller, from which the memory type851* is derived.852*853* This routine returns the EDAC memory type appropriate for the854* current controller configuration.855*856* Returns a memory type enumeration.857*/858static enum mem_type __devinit859ppc4xx_edac_get_mtype(u32 mcopt1)860{861bool rden = ((mcopt1 & SDRAM_MCOPT1_RDEN_MASK) == SDRAM_MCOPT1_RDEN);862863switch (mcopt1 & SDRAM_MCOPT1_DDR_TYPE_MASK) {864case SDRAM_MCOPT1_DDR2_TYPE:865return rden ? MEM_RDDR2 : MEM_DDR2;866case SDRAM_MCOPT1_DDR1_TYPE:867return rden ? MEM_RDDR : MEM_DDR;868default:869return MEM_UNKNOWN;870}871}872873/**874* ppc4xx_edac_init_csrows - initialize driver instance rows875* @mci: A pointer to the EDAC memory controller instance876* associated with the ibm,sdram-4xx-ddr2 controller for which877* the csrows (i.e. banks/ranks) are being initialized.878* @mcopt1: The 32-bit Memory Controller Option 1 register value879* currently set for the controller, from which bank width880* and memory typ information is derived.881*882* This routine initializes the virtual "chip select rows" associated883* with the EDAC memory controller instance. An ibm,sdram-4xx-ddr2884* controller bank/rank is mapped to a row.885*886* Returns 0 if OK; otherwise, -EINVAL if the memory bank size887* configuration cannot be determined.888*/889static int __devinit890ppc4xx_edac_init_csrows(struct mem_ctl_info *mci, u32 mcopt1)891{892const struct ppc4xx_edac_pdata *pdata = mci->pvt_info;893int status = 0;894enum mem_type mtype;895enum dev_type dtype;896enum edac_type edac_mode;897int row;898u32 mbxcf, size;899static u32 ppc4xx_last_page;900901/* Establish the memory type and width */902903mtype = ppc4xx_edac_get_mtype(mcopt1);904dtype = ppc4xx_edac_get_dtype(mcopt1);905906/* Establish EDAC mode */907908if (mci->edac_cap & EDAC_FLAG_SECDED)909edac_mode = EDAC_SECDED;910else if (mci->edac_cap & EDAC_FLAG_EC)911edac_mode = EDAC_EC;912else913edac_mode = EDAC_NONE;914915/*916* Initialize each chip select row structure which correspond917* 1:1 with a controller bank/rank.918*/919920for (row = 0; row < mci->nr_csrows; row++) {921struct csrow_info *csi = &mci->csrows[row];922923/*924* Get the configuration settings for this925* row/bank/rank and skip disabled banks.926*/927928mbxcf = mfsdram(&pdata->dcr_host, SDRAM_MBXCF(row));929930if ((mbxcf & SDRAM_MBCF_BE_MASK) != SDRAM_MBCF_BE_ENABLE)931continue;932933/* Map the bank configuration size setting to pages. */934935size = mbxcf & SDRAM_MBCF_SZ_MASK;936937switch (size) {938case SDRAM_MBCF_SZ_4MB:939case SDRAM_MBCF_SZ_8MB:940case SDRAM_MBCF_SZ_16MB:941case SDRAM_MBCF_SZ_32MB:942case SDRAM_MBCF_SZ_64MB:943case SDRAM_MBCF_SZ_128MB:944case SDRAM_MBCF_SZ_256MB:945case SDRAM_MBCF_SZ_512MB:946case SDRAM_MBCF_SZ_1GB:947case SDRAM_MBCF_SZ_2GB:948case SDRAM_MBCF_SZ_4GB:949case SDRAM_MBCF_SZ_8GB:950csi->nr_pages = SDRAM_MBCF_SZ_TO_PAGES(size);951break;952default:953ppc4xx_edac_mc_printk(KERN_ERR, mci,954"Unrecognized memory bank %d "955"size 0x%08x\n",956row, SDRAM_MBCF_SZ_DECODE(size));957status = -EINVAL;958goto done;959}960961csi->first_page = ppc4xx_last_page;962csi->last_page = csi->first_page + csi->nr_pages - 1;963csi->page_mask = 0;964965/*966* It's unclear exactly what grain should be set to967* here. The SDRAM_ECCES register allows resolution of968* an error down to a nibble which would potentially969* argue for a grain of '1' byte, even though we only970* know the associated address for uncorrectable971* errors. This value is not used at present for972* anything other than error reporting so getting it973* wrong should be of little consequence. Other974* possible values would be the PLB width (16), the975* page size (PAGE_SIZE) or the memory width (2 or 4).976*/977978csi->grain = 1;979980csi->mtype = mtype;981csi->dtype = dtype;982983csi->edac_mode = edac_mode;984985ppc4xx_last_page += csi->nr_pages;986}987988done:989return status;990}991992/**993* ppc4xx_edac_mc_init - initialize driver instance994* @mci: A pointer to the EDAC memory controller instance being995* initialized.996* @op: A pointer to the OpenFirmware device tree node associated997* with the controller this EDAC instance is bound to.998* @dcr_host: A pointer to the DCR data containing the DCR mapping999* for this controller instance.1000* @mcopt1: The 32-bit Memory Controller Option 1 register value1001* currently set for the controller, from which ECC capabilities1002* and scrub mode are derived.1003*1004* This routine performs initialization of the EDAC memory controller1005* instance and related driver-private data associated with the1006* ibm,sdram-4xx-ddr2 memory controller the instance is bound to.1007*1008* Returns 0 if OK; otherwise, < 0 on error.1009*/1010static int __devinit1011ppc4xx_edac_mc_init(struct mem_ctl_info *mci,1012struct platform_device *op,1013const dcr_host_t *dcr_host,1014u32 mcopt1)1015{1016int status = 0;1017const u32 memcheck = (mcopt1 & SDRAM_MCOPT1_MCHK_MASK);1018struct ppc4xx_edac_pdata *pdata = NULL;1019const struct device_node *np = op->dev.of_node;10201021if (of_match_device(ppc4xx_edac_match, &op->dev) == NULL)1022return -EINVAL;10231024/* Initial driver pointers and private data */10251026mci->dev = &op->dev;10271028dev_set_drvdata(mci->dev, mci);10291030pdata = mci->pvt_info;10311032pdata->dcr_host = *dcr_host;1033pdata->irqs.sec = NO_IRQ;1034pdata->irqs.ded = NO_IRQ;10351036/* Initialize controller capabilities and configuration */10371038mci->mtype_cap = (MEM_FLAG_DDR | MEM_FLAG_RDDR |1039MEM_FLAG_DDR2 | MEM_FLAG_RDDR2);10401041mci->edac_ctl_cap = (EDAC_FLAG_NONE |1042EDAC_FLAG_EC |1043EDAC_FLAG_SECDED);10441045mci->scrub_cap = SCRUB_NONE;1046mci->scrub_mode = SCRUB_NONE;10471048/*1049* Update the actual capabilites based on the MCOPT1[MCHK]1050* settings. Scrubbing is only useful if reporting is enabled.1051*/10521053switch (memcheck) {1054case SDRAM_MCOPT1_MCHK_CHK:1055mci->edac_cap = EDAC_FLAG_EC;1056break;1057case SDRAM_MCOPT1_MCHK_CHK_REP:1058mci->edac_cap = (EDAC_FLAG_EC | EDAC_FLAG_SECDED);1059mci->scrub_mode = SCRUB_SW_SRC;1060break;1061default:1062mci->edac_cap = EDAC_FLAG_NONE;1063break;1064}10651066/* Initialize strings */10671068mci->mod_name = PPC4XX_EDAC_MODULE_NAME;1069mci->mod_ver = PPC4XX_EDAC_MODULE_REVISION;1070mci->ctl_name = match->compatible,1071mci->dev_name = np->full_name;10721073/* Initialize callbacks */10741075mci->edac_check = ppc4xx_edac_check;1076mci->ctl_page_to_phys = NULL;10771078/* Initialize chip select rows */10791080status = ppc4xx_edac_init_csrows(mci, mcopt1);10811082if (status)1083ppc4xx_edac_mc_printk(KERN_ERR, mci,1084"Failed to initialize rows!\n");10851086return status;1087}10881089/**1090* ppc4xx_edac_register_irq - setup and register controller interrupts1091* @op: A pointer to the OpenFirmware device tree node associated1092* with the controller this EDAC instance is bound to.1093* @mci: A pointer to the EDAC memory controller instance1094* associated with the ibm,sdram-4xx-ddr2 controller for which1095* interrupts are being registered.1096*1097* This routine parses the correctable (CE) and uncorrectable error (UE)1098* interrupts from the device tree node and maps and assigns them to1099* the associated EDAC memory controller instance.1100*1101* Returns 0 if OK; otherwise, -ENODEV if the interrupts could not be1102* mapped and assigned.1103*/1104static int __devinit1105ppc4xx_edac_register_irq(struct platform_device *op, struct mem_ctl_info *mci)1106{1107int status = 0;1108int ded_irq, sec_irq;1109struct ppc4xx_edac_pdata *pdata = mci->pvt_info;1110struct device_node *np = op->dev.of_node;11111112ded_irq = irq_of_parse_and_map(np, INTMAP_ECCDED_INDEX);1113sec_irq = irq_of_parse_and_map(np, INTMAP_ECCSEC_INDEX);11141115if (ded_irq == NO_IRQ || sec_irq == NO_IRQ) {1116ppc4xx_edac_mc_printk(KERN_ERR, mci,1117"Unable to map interrupts.\n");1118status = -ENODEV;1119goto fail;1120}11211122status = request_irq(ded_irq,1123ppc4xx_edac_isr,1124IRQF_DISABLED,1125"[EDAC] MC ECCDED",1126mci);11271128if (status < 0) {1129ppc4xx_edac_mc_printk(KERN_ERR, mci,1130"Unable to request irq %d for ECC DED",1131ded_irq);1132status = -ENODEV;1133goto fail1;1134}11351136status = request_irq(sec_irq,1137ppc4xx_edac_isr,1138IRQF_DISABLED,1139"[EDAC] MC ECCSEC",1140mci);11411142if (status < 0) {1143ppc4xx_edac_mc_printk(KERN_ERR, mci,1144"Unable to request irq %d for ECC SEC",1145sec_irq);1146status = -ENODEV;1147goto fail2;1148}11491150ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCDED irq is %d\n", ded_irq);1151ppc4xx_edac_mc_printk(KERN_INFO, mci, "ECCSEC irq is %d\n", sec_irq);11521153pdata->irqs.ded = ded_irq;1154pdata->irqs.sec = sec_irq;11551156return 0;11571158fail2:1159free_irq(sec_irq, mci);11601161fail1:1162free_irq(ded_irq, mci);11631164fail:1165return status;1166}11671168/**1169* ppc4xx_edac_map_dcrs - locate and map controller registers1170* @np: A pointer to the device tree node containing the DCR1171* resources to map.1172* @dcr_host: A pointer to the DCR data to populate with the1173* DCR mapping.1174*1175* This routine attempts to locate in the device tree and map the DCR1176* register resources associated with the controller's indirect DCR1177* address and data windows.1178*1179* Returns 0 if the DCRs were successfully mapped; otherwise, < 0 on1180* error.1181*/1182static int __devinit1183ppc4xx_edac_map_dcrs(const struct device_node *np, dcr_host_t *dcr_host)1184{1185unsigned int dcr_base, dcr_len;11861187if (np == NULL || dcr_host == NULL)1188return -EINVAL;11891190/* Get the DCR resource extent and sanity check the values. */11911192dcr_base = dcr_resource_start(np, 0);1193dcr_len = dcr_resource_len(np, 0);11941195if (dcr_base == 0 || dcr_len == 0) {1196ppc4xx_edac_printk(KERN_ERR,1197"Failed to obtain DCR property.\n");1198return -ENODEV;1199}12001201if (dcr_len != SDRAM_DCR_RESOURCE_LEN) {1202ppc4xx_edac_printk(KERN_ERR,1203"Unexpected DCR length %d, expected %d.\n",1204dcr_len, SDRAM_DCR_RESOURCE_LEN);1205return -ENODEV;1206}12071208/* Attempt to map the DCR extent. */12091210*dcr_host = dcr_map(np, dcr_base, dcr_len);12111212if (!DCR_MAP_OK(*dcr_host)) {1213ppc4xx_edac_printk(KERN_INFO, "Failed to map DCRs.\n");1214return -ENODEV;1215}12161217return 0;1218}12191220/**1221* ppc4xx_edac_probe - check controller and bind driver1222* @op: A pointer to the OpenFirmware device tree node associated1223* with the controller being probed for driver binding.1224*1225* This routine probes a specific ibm,sdram-4xx-ddr2 controller1226* instance for binding with the driver.1227*1228* Returns 0 if the controller instance was successfully bound to the1229* driver; otherwise, < 0 on error.1230*/1231static int __devinit ppc4xx_edac_probe(struct platform_device *op)1232{1233int status = 0;1234u32 mcopt1, memcheck;1235dcr_host_t dcr_host;1236const struct device_node *np = op->dev.of_node;1237struct mem_ctl_info *mci = NULL;1238static int ppc4xx_edac_instance;12391240/*1241* At this point, we only support the controller realized on1242* the AMCC PPC 405EX[r]. Reject anything else.1243*/12441245if (!of_device_is_compatible(np, "ibm,sdram-405ex") &&1246!of_device_is_compatible(np, "ibm,sdram-405exr")) {1247ppc4xx_edac_printk(KERN_NOTICE,1248"Only the PPC405EX[r] is supported.\n");1249return -ENODEV;1250}12511252/*1253* Next, get the DCR property and attempt to map it so that we1254* can probe the controller.1255*/12561257status = ppc4xx_edac_map_dcrs(np, &dcr_host);12581259if (status)1260return status;12611262/*1263* First determine whether ECC is enabled at all. If not,1264* there is no useful checking or monitoring that can be done1265* for this controller.1266*/12671268mcopt1 = mfsdram(&dcr_host, SDRAM_MCOPT1);1269memcheck = (mcopt1 & SDRAM_MCOPT1_MCHK_MASK);12701271if (memcheck == SDRAM_MCOPT1_MCHK_NON) {1272ppc4xx_edac_printk(KERN_INFO, "%s: No ECC memory detected or "1273"ECC is disabled.\n", np->full_name);1274status = -ENODEV;1275goto done;1276}12771278/*1279* At this point, we know ECC is enabled, allocate an EDAC1280* controller instance and perform the appropriate1281* initialization.1282*/12831284mci = edac_mc_alloc(sizeof(struct ppc4xx_edac_pdata),1285ppc4xx_edac_nr_csrows,1286ppc4xx_edac_nr_chans,1287ppc4xx_edac_instance);12881289if (mci == NULL) {1290ppc4xx_edac_printk(KERN_ERR, "%s: "1291"Failed to allocate EDAC MC instance!\n",1292np->full_name);1293status = -ENOMEM;1294goto done;1295}12961297status = ppc4xx_edac_mc_init(mci, op, &dcr_host, mcopt1);12981299if (status) {1300ppc4xx_edac_mc_printk(KERN_ERR, mci,1301"Failed to initialize instance!\n");1302goto fail;1303}13041305/*1306* We have a valid, initialized EDAC instance bound to the1307* controller. Attempt to register it with the EDAC subsystem1308* and, if necessary, register interrupts.1309*/13101311if (edac_mc_add_mc(mci)) {1312ppc4xx_edac_mc_printk(KERN_ERR, mci,1313"Failed to add instance!\n");1314status = -ENODEV;1315goto fail;1316}13171318if (edac_op_state == EDAC_OPSTATE_INT) {1319status = ppc4xx_edac_register_irq(op, mci);13201321if (status)1322goto fail1;1323}13241325ppc4xx_edac_instance++;13261327return 0;13281329fail1:1330edac_mc_del_mc(mci->dev);13311332fail:1333edac_mc_free(mci);13341335done:1336return status;1337}13381339/**1340* ppc4xx_edac_remove - unbind driver from controller1341* @op: A pointer to the OpenFirmware device tree node associated1342* with the controller this EDAC instance is to be unbound/removed1343* from.1344*1345* This routine unbinds the EDAC memory controller instance associated1346* with the specified ibm,sdram-4xx-ddr2 controller described by the1347* OpenFirmware device tree node passed as a parameter.1348*1349* Unconditionally returns 0.1350*/1351static int1352ppc4xx_edac_remove(struct platform_device *op)1353{1354struct mem_ctl_info *mci = dev_get_drvdata(&op->dev);1355struct ppc4xx_edac_pdata *pdata = mci->pvt_info;13561357if (edac_op_state == EDAC_OPSTATE_INT) {1358free_irq(pdata->irqs.sec, mci);1359free_irq(pdata->irqs.ded, mci);1360}13611362dcr_unmap(pdata->dcr_host, SDRAM_DCR_RESOURCE_LEN);13631364edac_mc_del_mc(mci->dev);1365edac_mc_free(mci);13661367return 0;1368}13691370/**1371* ppc4xx_edac_opstate_init - initialize EDAC reporting method1372*1373* This routine ensures that the EDAC memory controller reporting1374* method is mapped to a sane value as the EDAC core defines the value1375* to EDAC_OPSTATE_INVAL by default. We don't call the global1376* opstate_init as that defaults to polling and we want interrupt as1377* the default.1378*/1379static inline void __init1380ppc4xx_edac_opstate_init(void)1381{1382switch (edac_op_state) {1383case EDAC_OPSTATE_POLL:1384case EDAC_OPSTATE_INT:1385break;1386default:1387edac_op_state = EDAC_OPSTATE_INT;1388break;1389}13901391ppc4xx_edac_printk(KERN_INFO, "Reporting type: %s\n",1392((edac_op_state == EDAC_OPSTATE_POLL) ?1393EDAC_OPSTATE_POLL_STR :1394((edac_op_state == EDAC_OPSTATE_INT) ?1395EDAC_OPSTATE_INT_STR :1396EDAC_OPSTATE_UNKNOWN_STR)));1397}13981399/**1400* ppc4xx_edac_init - driver/module insertion entry point1401*1402* This routine is the driver/module insertion entry point. It1403* initializes the EDAC memory controller reporting state and1404* registers the driver as an OpenFirmware device tree platform1405* driver.1406*/1407static int __init1408ppc4xx_edac_init(void)1409{1410ppc4xx_edac_printk(KERN_INFO, PPC4XX_EDAC_MODULE_REVISION "\n");14111412ppc4xx_edac_opstate_init();14131414return platform_driver_register(&ppc4xx_edac_driver);1415}14161417/**1418* ppc4xx_edac_exit - driver/module removal entry point1419*1420* This routine is the driver/module removal entry point. It1421* unregisters the driver as an OpenFirmware device tree platform1422* driver.1423*/1424static void __exit1425ppc4xx_edac_exit(void)1426{1427platform_driver_unregister(&ppc4xx_edac_driver);1428}14291430module_init(ppc4xx_edac_init);1431module_exit(ppc4xx_edac_exit);14321433MODULE_LICENSE("GPL v2");1434MODULE_AUTHOR("Grant Erickson <[email protected]>");1435MODULE_DESCRIPTION("EDAC MC Driver for the PPC4xx IBM DDR2 Memory Controller");1436module_param(edac_op_state, int, 0444);1437MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting State: "1438"0=" EDAC_OPSTATE_POLL_STR ", 2=" EDAC_OPSTATE_INT_STR);143914401441