2 * Copyright (c) 2008 Nuovation System Designs, LLC
3 * Grant Erickson <gerickson@nuovations.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; version 2 of the
12 #include <linux/edac.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/of_platform.h>
20 #include <linux/types.h>
24 #include "edac_module.h"
25 #include "ppc4xx_edac.h"
28 * This file implements a driver for monitoring and handling events
29 * associated with the IMB DDR2 ECC controller found in the AMCC/IBM
30 * 405EX[r], 440SP, 440SPe, 460EX, 460GT and 460SX.
32 * As realized in the 405EX[r], this controller features:
34 * - Support for registered- and non-registered DDR1 and DDR2 memory.
35 * - 32-bit or 16-bit memory interface with optional ECC.
37 * o ECC support includes:
40 * - Aligned-nibble error detect
43 * - Two (2) memory banks/ranks.
44 * - Up to 1 GiB per bank/rank in 32-bit mode and up to 512 MiB per
45 * bank/rank in 16-bit mode.
47 * As realized in the 440SP and 440SPe, this controller changes/adds:
49 * - 64-bit or 32-bit memory interface with optional ECC.
51 * o ECC support includes:
54 * - Aligned-nibble error detect
57 * - Up to 4 GiB per bank/rank in 64-bit mode and up to 2 GiB
58 * per bank/rank in 32-bit mode.
60 * As realized in the 460EX and 460GT, this controller changes/adds:
62 * - 64-bit or 32-bit memory interface with optional ECC.
64 * o ECC support includes:
67 * - Aligned-nibble error detect
70 * - Four (4) memory banks/ranks.
71 * - Up to 16 GiB per bank/rank in 64-bit mode and up to 8 GiB
72 * per bank/rank in 32-bit mode.
74 * At present, this driver has ONLY been tested against the controller
75 * realization in the 405EX[r] on the AMCC Kilauea and Haleakala
76 * boards (256 MiB w/o ECC memory soldered onto the board) and a
77 * proprietary board based on those designs (128 MiB ECC memory, also
78 * soldered onto the board).
80 * Dynamic feature detection and handling needs to be added for the
81 * other realizations of this controller listed above.
83 * Eventually, this driver will likely be adapted to the above variant
84 * realizations of this controller as well as broken apart to handle
85 * the other known ECC-capable controllers prevalent in other 4xx
88 * - IBM SDRAM (405GP, 405CR and 405EP) "ibm,sdram-4xx"
89 * - IBM DDR1 (440GP, 440GX, 440EP and 440GR) "ibm,sdram-4xx-ddr"
90 * - Denali DDR1/DDR2 (440EPX and 440GRX) "denali,sdram-4xx-ddr2"
92 * For this controller, unfortunately, correctable errors report
93 * nothing more than the beat/cycle and byte/lane the correction
94 * occurred on and the check bit group that covered the error.
96 * In contrast, uncorrectable errors also report the failing address,
97 * the bus master and the transaction direction (i.e. read or write)
99 * Regardless of whether the error is a CE or a UE, we report the
100 * following pieces of information in the driver-unique message to the
105 * - Check bit error group
109 /* Preprocessor Definitions */
111 #define EDAC_OPSTATE_INT_STR "interrupt"
112 #define EDAC_OPSTATE_POLL_STR "polled"
113 #define EDAC_OPSTATE_UNKNOWN_STR "unknown"
115 #define PPC4XX_EDAC_MODULE_NAME "ppc4xx_edac"
116 #define PPC4XX_EDAC_MODULE_REVISION "v1.0.0"
118 #define PPC4XX_EDAC_MESSAGE_SIZE 256
121 * Kernel logging without an EDAC instance
123 #define ppc4xx_edac_printk(level, fmt, arg...) \
124 edac_printk(level, "PPC4xx MC", fmt, ##arg)
127 * Kernel logging with an EDAC instance
129 #define ppc4xx_edac_mc_printk(level, mci, fmt, arg...) \
130 edac_mc_chipset_printk(mci, level, "PPC4xx", fmt, ##arg)
133 * Macros to convert bank configuration size enumerations into MiB and
136 #define SDRAM_MBCF_SZ_MiB_MIN 4
137 #define SDRAM_MBCF_SZ_TO_MiB(n) (SDRAM_MBCF_SZ_MiB_MIN \
138 << (SDRAM_MBCF_SZ_DECODE(n)))
139 #define SDRAM_MBCF_SZ_TO_PAGES(n) (SDRAM_MBCF_SZ_MiB_MIN \
140 << (20 - PAGE_SHIFT + \
141 SDRAM_MBCF_SZ_DECODE(n)))
144 * The ibm,sdram-4xx-ddr2 Device Control Registers (DCRs) are
145 * indirectly accessed and have a base and length defined by the
146 * device tree. The base can be anything; however, we expect the
147 * length to be precisely two registers, the first for the address
148 * window and the second for the data window.
150 #define SDRAM_DCR_RESOURCE_LEN 2
151 #define SDRAM_DCR_ADDR_OFFSET 0
152 #define SDRAM_DCR_DATA_OFFSET 1
155 * Device tree interrupt indices
157 #define INTMAP_ECCDED_INDEX 0 /* Double-bit Error Detect */
158 #define INTMAP_ECCSEC_INDEX 1 /* Single-bit Error Correct */
160 /* Type Definitions */
163 * PPC4xx SDRAM memory controller private instance data
165 struct ppc4xx_edac_pdata
{
166 dcr_host_t dcr_host
; /* Indirect DCR address/data window mapping */
168 int sec
; /* Single-bit correctable error IRQ assigned */
169 int ded
; /* Double-bit detectable error IRQ assigned */
174 * Various status data gathered and manipulated when checking and
175 * reporting ECC status.
177 struct ppc4xx_ecc_status
{
185 /* Function Prototypes */
187 static int ppc4xx_edac_probe(struct platform_device
*device
);
188 static int ppc4xx_edac_remove(struct platform_device
*device
);
190 /* Global Variables */
193 * Device tree node type and compatible tuples this driver can match
196 static const struct of_device_id ppc4xx_edac_match
[] = {
198 .compatible
= "ibm,sdram-4xx-ddr2"
202 MODULE_DEVICE_TABLE(of
, ppc4xx_edac_match
);
204 static struct platform_driver ppc4xx_edac_driver
= {
205 .probe
= ppc4xx_edac_probe
,
206 .remove
= ppc4xx_edac_remove
,
208 .name
= PPC4XX_EDAC_MODULE_NAME
,
209 .of_match_table
= ppc4xx_edac_match
,
214 * TODO: The row and channel parameters likely need to be dynamically
215 * set based on the aforementioned variant controller realizations.
217 static const unsigned ppc4xx_edac_nr_csrows
= 2;
218 static const unsigned ppc4xx_edac_nr_chans
= 1;
221 * Strings associated with PLB master IDs capable of being posted in
222 * SDRAM_BESR or SDRAM_WMIRQ on uncorrectable ECC errors.
224 static const char * const ppc4xx_plb_masters
[9] = {
225 [SDRAM_PLB_M0ID_ICU
] = "ICU",
226 [SDRAM_PLB_M0ID_PCIE0
] = "PCI-E 0",
227 [SDRAM_PLB_M0ID_PCIE1
] = "PCI-E 1",
228 [SDRAM_PLB_M0ID_DMA
] = "DMA",
229 [SDRAM_PLB_M0ID_DCU
] = "DCU",
230 [SDRAM_PLB_M0ID_OPB
] = "OPB",
231 [SDRAM_PLB_M0ID_MAL
] = "MAL",
232 [SDRAM_PLB_M0ID_SEC
] = "SEC",
233 [SDRAM_PLB_M0ID_AHB
] = "AHB"
237 * mfsdram - read and return controller register data
238 * @dcr_host: A pointer to the DCR mapping.
239 * @idcr_n: The indirect DCR register to read.
241 * This routine reads and returns the data associated with the
242 * controller's specified indirect DCR register.
244 * Returns the read data.
247 mfsdram(const dcr_host_t
*dcr_host
, unsigned int idcr_n
)
249 return __mfdcri(dcr_host
->base
+ SDRAM_DCR_ADDR_OFFSET
,
250 dcr_host
->base
+ SDRAM_DCR_DATA_OFFSET
,
255 * mtsdram - write controller register data
256 * @dcr_host: A pointer to the DCR mapping.
257 * @idcr_n: The indirect DCR register to write.
258 * @value: The data to write.
260 * This routine writes the provided data to the controller's specified
261 * indirect DCR register.
264 mtsdram(const dcr_host_t
*dcr_host
, unsigned int idcr_n
, u32 value
)
266 return __mtdcri(dcr_host
->base
+ SDRAM_DCR_ADDR_OFFSET
,
267 dcr_host
->base
+ SDRAM_DCR_DATA_OFFSET
,
273 * ppc4xx_edac_check_bank_error - check a bank for an ECC bank error
274 * @status: A pointer to the ECC status structure to check for an
276 * @bank: The bank to check for an ECC error.
278 * This routine determines whether the specified bank has an ECC
281 * Returns true if the specified bank has an ECC error; otherwise,
285 ppc4xx_edac_check_bank_error(const struct ppc4xx_ecc_status
*status
,
290 return status
->ecces
& SDRAM_ECCES_BK0ER
;
292 return status
->ecces
& SDRAM_ECCES_BK1ER
;
299 * ppc4xx_edac_generate_bank_message - generate interpretted bank status message
300 * @mci: A pointer to the EDAC memory controller instance associated
301 * with the bank message being generated.
302 * @status: A pointer to the ECC status structure to generate the
304 * @buffer: A pointer to the buffer in which to generate the
306 * @size: The size, in bytes, of space available in buffer.
308 * This routine generates to the provided buffer the portion of the
309 * driver-unique report message associated with the ECCESS[BKNER]
310 * field of the specified ECC status.
312 * Returns the number of characters generated on success; otherwise, <
316 ppc4xx_edac_generate_bank_message(const struct mem_ctl_info
*mci
,
317 const struct ppc4xx_ecc_status
*status
,
322 unsigned int row
, rows
;
324 n
= snprintf(buffer
, size
, "%s: Banks: ", mci
->dev_name
);
326 if (n
< 0 || n
>= size
)
333 for (rows
= 0, row
= 0; row
< mci
->nr_csrows
; row
++) {
334 if (ppc4xx_edac_check_bank_error(status
, row
)) {
335 n
= snprintf(buffer
, size
, "%s%u",
336 (rows
++ ? ", " : ""), row
);
338 if (n
< 0 || n
>= size
)
347 n
= snprintf(buffer
, size
, "%s; ", rows
? "" : "None");
349 if (n
< 0 || n
>= size
)
361 * ppc4xx_edac_generate_checkbit_message - generate interpretted checkbit message
362 * @mci: A pointer to the EDAC memory controller instance associated
363 * with the checkbit message being generated.
364 * @status: A pointer to the ECC status structure to generate the
366 * @buffer: A pointer to the buffer in which to generate the
368 * @size: The size, in bytes, of space available in buffer.
370 * This routine generates to the provided buffer the portion of the
371 * driver-unique report message associated with the ECCESS[CKBER]
372 * field of the specified ECC status.
374 * Returns the number of characters generated on success; otherwise, <
378 ppc4xx_edac_generate_checkbit_message(const struct mem_ctl_info
*mci
,
379 const struct ppc4xx_ecc_status
*status
,
383 const struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
384 const char *ckber
= NULL
;
386 switch (status
->ecces
& SDRAM_ECCES_CKBER_MASK
) {
387 case SDRAM_ECCES_CKBER_NONE
:
390 case SDRAM_ECCES_CKBER_32_ECC_0_3
:
393 case SDRAM_ECCES_CKBER_32_ECC_4_8
:
394 switch (mfsdram(&pdata
->dcr_host
, SDRAM_MCOPT1
) &
395 SDRAM_MCOPT1_WDTH_MASK
) {
396 case SDRAM_MCOPT1_WDTH_16
:
399 case SDRAM_MCOPT1_WDTH_32
:
407 case SDRAM_ECCES_CKBER_32_ECC_0_8
:
415 return snprintf(buffer
, size
, "Checkbit Error: %s", ckber
);
419 * ppc4xx_edac_generate_lane_message - generate interpretted byte lane message
420 * @mci: A pointer to the EDAC memory controller instance associated
421 * with the byte lane message being generated.
422 * @status: A pointer to the ECC status structure to generate the
424 * @buffer: A pointer to the buffer in which to generate the
426 * @size: The size, in bytes, of space available in buffer.
428 * This routine generates to the provided buffer the portion of the
429 * driver-unique report message associated with the ECCESS[BNCE]
430 * field of the specified ECC status.
432 * Returns the number of characters generated on success; otherwise, <
436 ppc4xx_edac_generate_lane_message(const struct mem_ctl_info
*mci
,
437 const struct ppc4xx_ecc_status
*status
,
442 unsigned int lane
, lanes
;
443 const unsigned int first_lane
= 0;
444 const unsigned int lane_count
= 16;
446 n
= snprintf(buffer
, size
, "; Byte Lane Errors: ");
448 if (n
< 0 || n
>= size
)
455 for (lanes
= 0, lane
= first_lane
; lane
< lane_count
; lane
++) {
456 if ((status
->ecces
& SDRAM_ECCES_BNCE_ENCODE(lane
)) != 0) {
457 n
= snprintf(buffer
, size
,
459 (lanes
++ ? ", " : ""), lane
);
461 if (n
< 0 || n
>= size
)
470 n
= snprintf(buffer
, size
, "%s; ", lanes
? "" : "None");
472 if (n
< 0 || n
>= size
)
484 * ppc4xx_edac_generate_ecc_message - generate interpretted ECC status message
485 * @mci: A pointer to the EDAC memory controller instance associated
486 * with the ECCES message being generated.
487 * @status: A pointer to the ECC status structure to generate the
489 * @buffer: A pointer to the buffer in which to generate the
491 * @size: The size, in bytes, of space available in buffer.
493 * This routine generates to the provided buffer the portion of the
494 * driver-unique report message associated with the ECCESS register of
495 * the specified ECC status.
497 * Returns the number of characters generated on success; otherwise, <
501 ppc4xx_edac_generate_ecc_message(const struct mem_ctl_info
*mci
,
502 const struct ppc4xx_ecc_status
*status
,
508 n
= ppc4xx_edac_generate_bank_message(mci
, status
, buffer
, size
);
510 if (n
< 0 || n
>= size
)
517 n
= ppc4xx_edac_generate_checkbit_message(mci
, status
, buffer
, size
);
519 if (n
< 0 || n
>= size
)
526 n
= ppc4xx_edac_generate_lane_message(mci
, status
, buffer
, size
);
528 if (n
< 0 || n
>= size
)
540 * ppc4xx_edac_generate_plb_message - generate interpretted PLB status message
541 * @mci: A pointer to the EDAC memory controller instance associated
542 * with the PLB message being generated.
543 * @status: A pointer to the ECC status structure to generate the
545 * @buffer: A pointer to the buffer in which to generate the
547 * @size: The size, in bytes, of space available in buffer.
549 * This routine generates to the provided buffer the portion of the
550 * driver-unique report message associated with the PLB-related BESR
551 * and/or WMIRQ registers of the specified ECC status.
553 * Returns the number of characters generated on success; otherwise, <
557 ppc4xx_edac_generate_plb_message(const struct mem_ctl_info
*mci
,
558 const struct ppc4xx_ecc_status
*status
,
565 if ((status
->besr
& SDRAM_BESR_MASK
) == 0)
568 if ((status
->besr
& SDRAM_BESR_M0ET_MASK
) == SDRAM_BESR_M0ET_NONE
)
571 read
= ((status
->besr
& SDRAM_BESR_M0RW_MASK
) == SDRAM_BESR_M0RW_READ
);
573 master
= SDRAM_BESR_M0ID_DECODE(status
->besr
);
575 return snprintf(buffer
, size
,
576 "%s error w/ PLB master %u \"%s\"; ",
577 (read
? "Read" : "Write"),
579 (((master
>= SDRAM_PLB_M0ID_FIRST
) &&
580 (master
<= SDRAM_PLB_M0ID_LAST
)) ?
581 ppc4xx_plb_masters
[master
] : "UNKNOWN"));
585 * ppc4xx_edac_generate_message - generate interpretted status message
586 * @mci: A pointer to the EDAC memory controller instance associated
587 * with the driver-unique message being generated.
588 * @status: A pointer to the ECC status structure to generate the
590 * @buffer: A pointer to the buffer in which to generate the
592 * @size: The size, in bytes, of space available in buffer.
594 * This routine generates to the provided buffer the driver-unique
595 * EDAC report message from the specified ECC status.
598 ppc4xx_edac_generate_message(const struct mem_ctl_info
*mci
,
599 const struct ppc4xx_ecc_status
*status
,
605 if (buffer
== NULL
|| size
== 0)
608 n
= ppc4xx_edac_generate_ecc_message(mci
, status
, buffer
, size
);
610 if (n
< 0 || n
>= size
)
616 ppc4xx_edac_generate_plb_message(mci
, status
, buffer
, size
);
621 * ppc4xx_ecc_dump_status - dump controller ECC status registers
622 * @mci: A pointer to the EDAC memory controller instance
623 * associated with the status being dumped.
624 * @status: A pointer to the ECC status structure to generate the
627 * This routine dumps to the kernel log buffer the raw and
628 * interpretted specified ECC status.
631 ppc4xx_ecc_dump_status(const struct mem_ctl_info
*mci
,
632 const struct ppc4xx_ecc_status
*status
)
634 char message
[PPC4XX_EDAC_MESSAGE_SIZE
];
636 ppc4xx_edac_generate_message(mci
, status
, message
, sizeof(message
));
638 ppc4xx_edac_mc_printk(KERN_INFO
, mci
,
643 "\tBEAR: 0x%08x%08x\n"
655 * ppc4xx_ecc_get_status - get controller ECC status
656 * @mci: A pointer to the EDAC memory controller instance
657 * associated with the status being retrieved.
658 * @status: A pointer to the ECC status structure to populate the
661 * This routine reads and masks, as appropriate, all the relevant
662 * status registers that deal with ibm,sdram-4xx-ddr2 ECC errors.
663 * While we read all of them, for correctable errors, we only expect
664 * to deal with ECCES. For uncorrectable errors, we expect to deal
668 ppc4xx_ecc_get_status(const struct mem_ctl_info
*mci
,
669 struct ppc4xx_ecc_status
*status
)
671 const struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
672 const dcr_host_t
*dcr_host
= &pdata
->dcr_host
;
674 status
->ecces
= mfsdram(dcr_host
, SDRAM_ECCES
) & SDRAM_ECCES_MASK
;
675 status
->wmirq
= mfsdram(dcr_host
, SDRAM_WMIRQ
) & SDRAM_WMIRQ_MASK
;
676 status
->besr
= mfsdram(dcr_host
, SDRAM_BESR
) & SDRAM_BESR_MASK
;
677 status
->bearl
= mfsdram(dcr_host
, SDRAM_BEARL
);
678 status
->bearh
= mfsdram(dcr_host
, SDRAM_BEARH
);
682 * ppc4xx_ecc_clear_status - clear controller ECC status
683 * @mci: A pointer to the EDAC memory controller instance
684 * associated with the status being cleared.
685 * @status: A pointer to the ECC status structure containing the
686 * values to write to clear the ECC status.
688 * This routine clears--by writing the masked (as appropriate) status
689 * values back to--the status registers that deal with
690 * ibm,sdram-4xx-ddr2 ECC errors.
693 ppc4xx_ecc_clear_status(const struct mem_ctl_info
*mci
,
694 const struct ppc4xx_ecc_status
*status
)
696 const struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
697 const dcr_host_t
*dcr_host
= &pdata
->dcr_host
;
699 mtsdram(dcr_host
, SDRAM_ECCES
, status
->ecces
& SDRAM_ECCES_MASK
);
700 mtsdram(dcr_host
, SDRAM_WMIRQ
, status
->wmirq
& SDRAM_WMIRQ_MASK
);
701 mtsdram(dcr_host
, SDRAM_BESR
, status
->besr
& SDRAM_BESR_MASK
);
702 mtsdram(dcr_host
, SDRAM_BEARL
, 0);
703 mtsdram(dcr_host
, SDRAM_BEARH
, 0);
707 * ppc4xx_edac_handle_ce - handle controller correctable ECC error (CE)
708 * @mci: A pointer to the EDAC memory controller instance
709 * associated with the correctable error being handled and reported.
710 * @status: A pointer to the ECC status structure associated with
711 * the correctable error being handled and reported.
713 * This routine handles an ibm,sdram-4xx-ddr2 controller ECC
714 * correctable error. Per the aforementioned discussion, there's not
715 * enough status available to use the full EDAC correctable error
716 * interface, so we just pass driver-unique message to the "no info"
720 ppc4xx_edac_handle_ce(struct mem_ctl_info
*mci
,
721 const struct ppc4xx_ecc_status
*status
)
724 char message
[PPC4XX_EDAC_MESSAGE_SIZE
];
726 ppc4xx_edac_generate_message(mci
, status
, message
, sizeof(message
));
728 for (row
= 0; row
< mci
->nr_csrows
; row
++)
729 if (ppc4xx_edac_check_bank_error(status
, row
))
730 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, 1,
737 * ppc4xx_edac_handle_ue - handle controller uncorrectable ECC error (UE)
738 * @mci: A pointer to the EDAC memory controller instance
739 * associated with the uncorrectable error being handled and
741 * @status: A pointer to the ECC status structure associated with
742 * the uncorrectable error being handled and reported.
744 * This routine handles an ibm,sdram-4xx-ddr2 controller ECC
745 * uncorrectable error.
748 ppc4xx_edac_handle_ue(struct mem_ctl_info
*mci
,
749 const struct ppc4xx_ecc_status
*status
)
751 const u64 bear
= ((u64
)status
->bearh
<< 32 | status
->bearl
);
752 const unsigned long page
= bear
>> PAGE_SHIFT
;
753 const unsigned long offset
= bear
& ~PAGE_MASK
;
755 char message
[PPC4XX_EDAC_MESSAGE_SIZE
];
757 ppc4xx_edac_generate_message(mci
, status
, message
, sizeof(message
));
759 for (row
= 0; row
< mci
->nr_csrows
; row
++)
760 if (ppc4xx_edac_check_bank_error(status
, row
))
761 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED
, mci
, 1,
768 * ppc4xx_edac_check - check controller for ECC errors
769 * @mci: A pointer to the EDAC memory controller instance
770 * associated with the ibm,sdram-4xx-ddr2 controller being
773 * This routine is used to check and post ECC errors and is called by
774 * both the EDAC polling thread and this driver's CE and UE interrupt
778 ppc4xx_edac_check(struct mem_ctl_info
*mci
)
781 static unsigned int count
;
783 struct ppc4xx_ecc_status status
;
785 ppc4xx_ecc_get_status(mci
, &status
);
788 if (count
++ % 30 == 0)
789 ppc4xx_ecc_dump_status(mci
, &status
);
792 if (status
.ecces
& SDRAM_ECCES_UE
)
793 ppc4xx_edac_handle_ue(mci
, &status
);
795 if (status
.ecces
& SDRAM_ECCES_CE
)
796 ppc4xx_edac_handle_ce(mci
, &status
);
798 ppc4xx_ecc_clear_status(mci
, &status
);
802 * ppc4xx_edac_isr - SEC (CE) and DED (UE) interrupt service routine
803 * @irq: The virtual interrupt number being serviced.
804 * @dev_id: A pointer to the EDAC memory controller instance
805 * associated with the interrupt being handled.
807 * This routine implements the interrupt handler for both correctable
808 * (CE) and uncorrectable (UE) ECC errors for the ibm,sdram-4xx-ddr2
809 * controller. It simply calls through to the same routine used during
810 * polling to check, report and clear the ECC status.
812 * Unconditionally returns IRQ_HANDLED.
815 ppc4xx_edac_isr(int irq
, void *dev_id
)
817 struct mem_ctl_info
*mci
= dev_id
;
819 ppc4xx_edac_check(mci
);
825 * ppc4xx_edac_get_dtype - return the controller memory width
826 * @mcopt1: The 32-bit Memory Controller Option 1 register value
827 * currently set for the controller, from which the width
830 * This routine returns the EDAC device type width appropriate for the
831 * current controller configuration.
833 * TODO: This needs to be conditioned dynamically through feature
834 * flags or some such when other controller variants are supported as
835 * the 405EX[r] is 16-/32-bit and the others are 32-/64-bit with the
836 * 16- and 64-bit field definition/value/enumeration (b1) overloaded
839 * Returns a device type width enumeration.
841 static enum dev_type
ppc4xx_edac_get_dtype(u32 mcopt1
)
843 switch (mcopt1
& SDRAM_MCOPT1_WDTH_MASK
) {
844 case SDRAM_MCOPT1_WDTH_16
:
846 case SDRAM_MCOPT1_WDTH_32
:
854 * ppc4xx_edac_get_mtype - return controller memory type
855 * @mcopt1: The 32-bit Memory Controller Option 1 register value
856 * currently set for the controller, from which the memory type
859 * This routine returns the EDAC memory type appropriate for the
860 * current controller configuration.
862 * Returns a memory type enumeration.
864 static enum mem_type
ppc4xx_edac_get_mtype(u32 mcopt1
)
866 bool rden
= ((mcopt1
& SDRAM_MCOPT1_RDEN_MASK
) == SDRAM_MCOPT1_RDEN
);
868 switch (mcopt1
& SDRAM_MCOPT1_DDR_TYPE_MASK
) {
869 case SDRAM_MCOPT1_DDR2_TYPE
:
870 return rden
? MEM_RDDR2
: MEM_DDR2
;
871 case SDRAM_MCOPT1_DDR1_TYPE
:
872 return rden
? MEM_RDDR
: MEM_DDR
;
879 * ppc4xx_edac_init_csrows - initialize driver instance rows
880 * @mci: A pointer to the EDAC memory controller instance
881 * associated with the ibm,sdram-4xx-ddr2 controller for which
882 * the csrows (i.e. banks/ranks) are being initialized.
883 * @mcopt1: The 32-bit Memory Controller Option 1 register value
884 * currently set for the controller, from which bank width
885 * and memory typ information is derived.
887 * This routine initializes the virtual "chip select rows" associated
888 * with the EDAC memory controller instance. An ibm,sdram-4xx-ddr2
889 * controller bank/rank is mapped to a row.
891 * Returns 0 if OK; otherwise, -EINVAL if the memory bank size
892 * configuration cannot be determined.
894 static int ppc4xx_edac_init_csrows(struct mem_ctl_info
*mci
, u32 mcopt1
)
896 const struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
900 enum edac_type edac_mode
;
902 u32 mbxcf
, size
, nr_pages
;
904 /* Establish the memory type and width */
906 mtype
= ppc4xx_edac_get_mtype(mcopt1
);
907 dtype
= ppc4xx_edac_get_dtype(mcopt1
);
909 /* Establish EDAC mode */
911 if (mci
->edac_cap
& EDAC_FLAG_SECDED
)
912 edac_mode
= EDAC_SECDED
;
913 else if (mci
->edac_cap
& EDAC_FLAG_EC
)
916 edac_mode
= EDAC_NONE
;
919 * Initialize each chip select row structure which correspond
920 * 1:1 with a controller bank/rank.
923 for (row
= 0; row
< mci
->nr_csrows
; row
++) {
924 struct csrow_info
*csi
= mci
->csrows
[row
];
927 * Get the configuration settings for this
928 * row/bank/rank and skip disabled banks.
931 mbxcf
= mfsdram(&pdata
->dcr_host
, SDRAM_MBXCF(row
));
933 if ((mbxcf
& SDRAM_MBCF_BE_MASK
) != SDRAM_MBCF_BE_ENABLE
)
936 /* Map the bank configuration size setting to pages. */
938 size
= mbxcf
& SDRAM_MBCF_SZ_MASK
;
941 case SDRAM_MBCF_SZ_4MB
:
942 case SDRAM_MBCF_SZ_8MB
:
943 case SDRAM_MBCF_SZ_16MB
:
944 case SDRAM_MBCF_SZ_32MB
:
945 case SDRAM_MBCF_SZ_64MB
:
946 case SDRAM_MBCF_SZ_128MB
:
947 case SDRAM_MBCF_SZ_256MB
:
948 case SDRAM_MBCF_SZ_512MB
:
949 case SDRAM_MBCF_SZ_1GB
:
950 case SDRAM_MBCF_SZ_2GB
:
951 case SDRAM_MBCF_SZ_4GB
:
952 case SDRAM_MBCF_SZ_8GB
:
953 nr_pages
= SDRAM_MBCF_SZ_TO_PAGES(size
);
956 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
957 "Unrecognized memory bank %d "
959 row
, SDRAM_MBCF_SZ_DECODE(size
));
965 * It's unclear exactly what grain should be set to
966 * here. The SDRAM_ECCES register allows resolution of
967 * an error down to a nibble which would potentially
968 * argue for a grain of '1' byte, even though we only
969 * know the associated address for uncorrectable
970 * errors. This value is not used at present for
971 * anything other than error reporting so getting it
972 * wrong should be of little consequence. Other
973 * possible values would be the PLB width (16), the
974 * page size (PAGE_SIZE) or the memory width (2 or 4).
976 for (j
= 0; j
< csi
->nr_channels
; j
++) {
977 struct dimm_info
*dimm
= csi
->channels
[j
]->dimm
;
979 dimm
->nr_pages
= nr_pages
/ csi
->nr_channels
;
985 dimm
->edac_mode
= edac_mode
;
994 * ppc4xx_edac_mc_init - initialize driver instance
995 * @mci: A pointer to the EDAC memory controller instance being
997 * @op: A pointer to the OpenFirmware device tree node associated
998 * with the controller this EDAC instance is bound to.
999 * @dcr_host: A pointer to the DCR data containing the DCR mapping
1000 * for this controller instance.
1001 * @mcopt1: The 32-bit Memory Controller Option 1 register value
1002 * currently set for the controller, from which ECC capabilities
1003 * and scrub mode are derived.
1005 * This routine performs initialization of the EDAC memory controller
1006 * instance and related driver-private data associated with the
1007 * ibm,sdram-4xx-ddr2 memory controller the instance is bound to.
1009 * Returns 0 if OK; otherwise, < 0 on error.
1011 static int ppc4xx_edac_mc_init(struct mem_ctl_info
*mci
,
1012 struct platform_device
*op
,
1013 const dcr_host_t
*dcr_host
, u32 mcopt1
)
1016 const u32 memcheck
= (mcopt1
& SDRAM_MCOPT1_MCHK_MASK
);
1017 struct ppc4xx_edac_pdata
*pdata
= NULL
;
1018 const struct device_node
*np
= op
->dev
.of_node
;
1020 if (of_match_device(ppc4xx_edac_match
, &op
->dev
) == NULL
)
1023 /* Initial driver pointers and private data */
1025 mci
->pdev
= &op
->dev
;
1027 dev_set_drvdata(mci
->pdev
, mci
);
1029 pdata
= mci
->pvt_info
;
1031 pdata
->dcr_host
= *dcr_host
;
1033 /* Initialize controller capabilities and configuration */
1035 mci
->mtype_cap
= (MEM_FLAG_DDR
| MEM_FLAG_RDDR
|
1036 MEM_FLAG_DDR2
| MEM_FLAG_RDDR2
);
1038 mci
->edac_ctl_cap
= (EDAC_FLAG_NONE
|
1042 mci
->scrub_cap
= SCRUB_NONE
;
1043 mci
->scrub_mode
= SCRUB_NONE
;
1046 * Update the actual capabilites based on the MCOPT1[MCHK]
1047 * settings. Scrubbing is only useful if reporting is enabled.
1051 case SDRAM_MCOPT1_MCHK_CHK
:
1052 mci
->edac_cap
= EDAC_FLAG_EC
;
1054 case SDRAM_MCOPT1_MCHK_CHK_REP
:
1055 mci
->edac_cap
= (EDAC_FLAG_EC
| EDAC_FLAG_SECDED
);
1056 mci
->scrub_mode
= SCRUB_SW_SRC
;
1059 mci
->edac_cap
= EDAC_FLAG_NONE
;
1063 /* Initialize strings */
1065 mci
->mod_name
= PPC4XX_EDAC_MODULE_NAME
;
1066 mci
->mod_ver
= PPC4XX_EDAC_MODULE_REVISION
;
1067 mci
->ctl_name
= ppc4xx_edac_match
->compatible
,
1068 mci
->dev_name
= np
->full_name
;
1070 /* Initialize callbacks */
1072 mci
->edac_check
= ppc4xx_edac_check
;
1073 mci
->ctl_page_to_phys
= NULL
;
1075 /* Initialize chip select rows */
1077 status
= ppc4xx_edac_init_csrows(mci
, mcopt1
);
1080 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1081 "Failed to initialize rows!\n");
1087 * ppc4xx_edac_register_irq - setup and register controller interrupts
1088 * @op: A pointer to the OpenFirmware device tree node associated
1089 * with the controller this EDAC instance is bound to.
1090 * @mci: A pointer to the EDAC memory controller instance
1091 * associated with the ibm,sdram-4xx-ddr2 controller for which
1092 * interrupts are being registered.
1094 * This routine parses the correctable (CE) and uncorrectable error (UE)
1095 * interrupts from the device tree node and maps and assigns them to
1096 * the associated EDAC memory controller instance.
1098 * Returns 0 if OK; otherwise, -ENODEV if the interrupts could not be
1099 * mapped and assigned.
1101 static int ppc4xx_edac_register_irq(struct platform_device
*op
,
1102 struct mem_ctl_info
*mci
)
1105 int ded_irq
, sec_irq
;
1106 struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
1107 struct device_node
*np
= op
->dev
.of_node
;
1109 ded_irq
= irq_of_parse_and_map(np
, INTMAP_ECCDED_INDEX
);
1110 sec_irq
= irq_of_parse_and_map(np
, INTMAP_ECCSEC_INDEX
);
1112 if (!ded_irq
|| !sec_irq
) {
1113 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1114 "Unable to map interrupts.\n");
1119 status
= request_irq(ded_irq
,
1126 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1127 "Unable to request irq %d for ECC DED",
1133 status
= request_irq(sec_irq
,
1140 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1141 "Unable to request irq %d for ECC SEC",
1147 ppc4xx_edac_mc_printk(KERN_INFO
, mci
, "ECCDED irq is %d\n", ded_irq
);
1148 ppc4xx_edac_mc_printk(KERN_INFO
, mci
, "ECCSEC irq is %d\n", sec_irq
);
1150 pdata
->irqs
.ded
= ded_irq
;
1151 pdata
->irqs
.sec
= sec_irq
;
1156 free_irq(sec_irq
, mci
);
1159 free_irq(ded_irq
, mci
);
1166 * ppc4xx_edac_map_dcrs - locate and map controller registers
1167 * @np: A pointer to the device tree node containing the DCR
1169 * @dcr_host: A pointer to the DCR data to populate with the
1172 * This routine attempts to locate in the device tree and map the DCR
1173 * register resources associated with the controller's indirect DCR
1174 * address and data windows.
1176 * Returns 0 if the DCRs were successfully mapped; otherwise, < 0 on
1179 static int ppc4xx_edac_map_dcrs(const struct device_node
*np
,
1180 dcr_host_t
*dcr_host
)
1182 unsigned int dcr_base
, dcr_len
;
1184 if (np
== NULL
|| dcr_host
== NULL
)
1187 /* Get the DCR resource extent and sanity check the values. */
1189 dcr_base
= dcr_resource_start(np
, 0);
1190 dcr_len
= dcr_resource_len(np
, 0);
1192 if (dcr_base
== 0 || dcr_len
== 0) {
1193 ppc4xx_edac_printk(KERN_ERR
,
1194 "Failed to obtain DCR property.\n");
1198 if (dcr_len
!= SDRAM_DCR_RESOURCE_LEN
) {
1199 ppc4xx_edac_printk(KERN_ERR
,
1200 "Unexpected DCR length %d, expected %d.\n",
1201 dcr_len
, SDRAM_DCR_RESOURCE_LEN
);
1205 /* Attempt to map the DCR extent. */
1207 *dcr_host
= dcr_map(np
, dcr_base
, dcr_len
);
1209 if (!DCR_MAP_OK(*dcr_host
)) {
1210 ppc4xx_edac_printk(KERN_INFO
, "Failed to map DCRs.\n");
1218 * ppc4xx_edac_probe - check controller and bind driver
1219 * @op: A pointer to the OpenFirmware device tree node associated
1220 * with the controller being probed for driver binding.
1222 * This routine probes a specific ibm,sdram-4xx-ddr2 controller
1223 * instance for binding with the driver.
1225 * Returns 0 if the controller instance was successfully bound to the
1226 * driver; otherwise, < 0 on error.
1228 static int ppc4xx_edac_probe(struct platform_device
*op
)
1231 u32 mcopt1
, memcheck
;
1232 dcr_host_t dcr_host
;
1233 const struct device_node
*np
= op
->dev
.of_node
;
1234 struct mem_ctl_info
*mci
= NULL
;
1235 struct edac_mc_layer layers
[2];
1236 static int ppc4xx_edac_instance
;
1239 * At this point, we only support the controller realized on
1240 * the AMCC PPC 405EX[r]. Reject anything else.
1243 if (!of_device_is_compatible(np
, "ibm,sdram-405ex") &&
1244 !of_device_is_compatible(np
, "ibm,sdram-405exr")) {
1245 ppc4xx_edac_printk(KERN_NOTICE
,
1246 "Only the PPC405EX[r] is supported.\n");
1251 * Next, get the DCR property and attempt to map it so that we
1252 * can probe the controller.
1255 status
= ppc4xx_edac_map_dcrs(np
, &dcr_host
);
1261 * First determine whether ECC is enabled at all. If not,
1262 * there is no useful checking or monitoring that can be done
1263 * for this controller.
1266 mcopt1
= mfsdram(&dcr_host
, SDRAM_MCOPT1
);
1267 memcheck
= (mcopt1
& SDRAM_MCOPT1_MCHK_MASK
);
1269 if (memcheck
== SDRAM_MCOPT1_MCHK_NON
) {
1270 ppc4xx_edac_printk(KERN_INFO
, "%s: No ECC memory detected or "
1271 "ECC is disabled.\n", np
->full_name
);
1277 * At this point, we know ECC is enabled, allocate an EDAC
1278 * controller instance and perform the appropriate
1281 layers
[0].type
= EDAC_MC_LAYER_CHIP_SELECT
;
1282 layers
[0].size
= ppc4xx_edac_nr_csrows
;
1283 layers
[0].is_virt_csrow
= true;
1284 layers
[1].type
= EDAC_MC_LAYER_CHANNEL
;
1285 layers
[1].size
= ppc4xx_edac_nr_chans
;
1286 layers
[1].is_virt_csrow
= false;
1287 mci
= edac_mc_alloc(ppc4xx_edac_instance
, ARRAY_SIZE(layers
), layers
,
1288 sizeof(struct ppc4xx_edac_pdata
));
1290 ppc4xx_edac_printk(KERN_ERR
, "%s: "
1291 "Failed to allocate EDAC MC instance!\n",
1297 status
= ppc4xx_edac_mc_init(mci
, op
, &dcr_host
, mcopt1
);
1300 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1301 "Failed to initialize instance!\n");
1306 * We have a valid, initialized EDAC instance bound to the
1307 * controller. Attempt to register it with the EDAC subsystem
1308 * and, if necessary, register interrupts.
1311 if (edac_mc_add_mc(mci
)) {
1312 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1313 "Failed to add instance!\n");
1318 if (edac_op_state
== EDAC_OPSTATE_INT
) {
1319 status
= ppc4xx_edac_register_irq(op
, mci
);
1325 ppc4xx_edac_instance
++;
1330 edac_mc_del_mc(mci
->pdev
);
1340 * ppc4xx_edac_remove - unbind driver from controller
1341 * @op: A pointer to the OpenFirmware device tree node associated
1342 * with the controller this EDAC instance is to be unbound/removed
1345 * This routine unbinds the EDAC memory controller instance associated
1346 * with the specified ibm,sdram-4xx-ddr2 controller described by the
1347 * OpenFirmware device tree node passed as a parameter.
1349 * Unconditionally returns 0.
1352 ppc4xx_edac_remove(struct platform_device
*op
)
1354 struct mem_ctl_info
*mci
= dev_get_drvdata(&op
->dev
);
1355 struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
1357 if (edac_op_state
== EDAC_OPSTATE_INT
) {
1358 free_irq(pdata
->irqs
.sec
, mci
);
1359 free_irq(pdata
->irqs
.ded
, mci
);
1362 dcr_unmap(pdata
->dcr_host
, SDRAM_DCR_RESOURCE_LEN
);
1364 edac_mc_del_mc(mci
->pdev
);
1371 * ppc4xx_edac_opstate_init - initialize EDAC reporting method
1373 * This routine ensures that the EDAC memory controller reporting
1374 * method is mapped to a sane value as the EDAC core defines the value
1375 * to EDAC_OPSTATE_INVAL by default. We don't call the global
1376 * opstate_init as that defaults to polling and we want interrupt as
1379 static inline void __init
1380 ppc4xx_edac_opstate_init(void)
1382 switch (edac_op_state
) {
1383 case EDAC_OPSTATE_POLL
:
1384 case EDAC_OPSTATE_INT
:
1387 edac_op_state
= EDAC_OPSTATE_INT
;
1391 ppc4xx_edac_printk(KERN_INFO
, "Reporting type: %s\n",
1392 ((edac_op_state
== EDAC_OPSTATE_POLL
) ?
1393 EDAC_OPSTATE_POLL_STR
:
1394 ((edac_op_state
== EDAC_OPSTATE_INT
) ?
1395 EDAC_OPSTATE_INT_STR
:
1396 EDAC_OPSTATE_UNKNOWN_STR
)));
1400 * ppc4xx_edac_init - driver/module insertion entry point
1402 * This routine is the driver/module insertion entry point. It
1403 * initializes the EDAC memory controller reporting state and
1404 * registers the driver as an OpenFirmware device tree platform
1408 ppc4xx_edac_init(void)
1410 ppc4xx_edac_printk(KERN_INFO
, PPC4XX_EDAC_MODULE_REVISION
"\n");
1412 ppc4xx_edac_opstate_init();
1414 return platform_driver_register(&ppc4xx_edac_driver
);
1418 * ppc4xx_edac_exit - driver/module removal entry point
1420 * This routine is the driver/module removal entry point. It
1421 * unregisters the driver as an OpenFirmware device tree platform
1425 ppc4xx_edac_exit(void)
1427 platform_driver_unregister(&ppc4xx_edac_driver
);
1430 module_init(ppc4xx_edac_init
);
1431 module_exit(ppc4xx_edac_exit
);
1433 MODULE_LICENSE("GPL v2");
1434 MODULE_AUTHOR("Grant Erickson <gerickson@nuovations.com>");
1435 MODULE_DESCRIPTION("EDAC MC Driver for the PPC4xx IBM DDR2 Memory Controller");
1436 module_param(edac_op_state
, int, 0444);
1437 MODULE_PARM_DESC(edac_op_state
, "EDAC Error Reporting State: "
1438 "0=" EDAC_OPSTATE_POLL_STR
", 2=" EDAC_OPSTATE_INT_STR
);