1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008 Nuovation System Designs, LLC
4 * Grant Erickson <gerickson@nuovations.com>
7 #include <linux/edac.h>
8 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/of_platform.h>
15 #include <linux/types.h>
19 #include "edac_module.h"
20 #include "ppc4xx_edac.h"
23 * This file implements a driver for monitoring and handling events
24 * associated with the IMB DDR2 ECC controller found in the AMCC/IBM
25 * 405EX[r], 440SP, 440SPe, 460EX, 460GT and 460SX.
27 * As realized in the 405EX[r], this controller features:
29 * - Support for registered- and non-registered DDR1 and DDR2 memory.
30 * - 32-bit or 16-bit memory interface with optional ECC.
32 * o ECC support includes:
35 * - Aligned-nibble error detect
38 * - Two (2) memory banks/ranks.
39 * - Up to 1 GiB per bank/rank in 32-bit mode and up to 512 MiB per
40 * bank/rank in 16-bit mode.
42 * As realized in the 440SP and 440SPe, this controller changes/adds:
44 * - 64-bit or 32-bit memory interface with optional ECC.
46 * o ECC support includes:
49 * - Aligned-nibble error detect
52 * - Up to 4 GiB per bank/rank in 64-bit mode and up to 2 GiB
53 * per bank/rank in 32-bit mode.
55 * As realized in the 460EX and 460GT, this controller changes/adds:
57 * - 64-bit or 32-bit memory interface with optional ECC.
59 * o ECC support includes:
62 * - Aligned-nibble error detect
65 * - Four (4) memory banks/ranks.
66 * - Up to 16 GiB per bank/rank in 64-bit mode and up to 8 GiB
67 * per bank/rank in 32-bit mode.
69 * At present, this driver has ONLY been tested against the controller
70 * realization in the 405EX[r] on the AMCC Kilauea and Haleakala
71 * boards (256 MiB w/o ECC memory soldered onto the board) and a
72 * proprietary board based on those designs (128 MiB ECC memory, also
73 * soldered onto the board).
75 * Dynamic feature detection and handling needs to be added for the
76 * other realizations of this controller listed above.
78 * Eventually, this driver will likely be adapted to the above variant
79 * realizations of this controller as well as broken apart to handle
80 * the other known ECC-capable controllers prevalent in other 4xx
83 * - IBM SDRAM (405GP, 405CR and 405EP) "ibm,sdram-4xx"
84 * - IBM DDR1 (440GP, 440GX, 440EP and 440GR) "ibm,sdram-4xx-ddr"
85 * - Denali DDR1/DDR2 (440EPX and 440GRX) "denali,sdram-4xx-ddr2"
87 * For this controller, unfortunately, correctable errors report
88 * nothing more than the beat/cycle and byte/lane the correction
89 * occurred on and the check bit group that covered the error.
91 * In contrast, uncorrectable errors also report the failing address,
92 * the bus master and the transaction direction (i.e. read or write)
94 * Regardless of whether the error is a CE or a UE, we report the
95 * following pieces of information in the driver-unique message to the
100 * - Check bit error group
104 /* Preprocessor Definitions */
106 #define EDAC_OPSTATE_INT_STR "interrupt"
107 #define EDAC_OPSTATE_POLL_STR "polled"
108 #define EDAC_OPSTATE_UNKNOWN_STR "unknown"
110 #define PPC4XX_EDAC_MODULE_NAME "ppc4xx_edac"
111 #define PPC4XX_EDAC_MODULE_REVISION "v1.0.0"
113 #define PPC4XX_EDAC_MESSAGE_SIZE 256
116 * Kernel logging without an EDAC instance
118 #define ppc4xx_edac_printk(level, fmt, arg...) \
119 edac_printk(level, "PPC4xx MC", fmt, ##arg)
122 * Kernel logging with an EDAC instance
124 #define ppc4xx_edac_mc_printk(level, mci, fmt, arg...) \
125 edac_mc_chipset_printk(mci, level, "PPC4xx", fmt, ##arg)
128 * Macros to convert bank configuration size enumerations into MiB and
131 #define SDRAM_MBCF_SZ_MiB_MIN 4
132 #define SDRAM_MBCF_SZ_TO_MiB(n) (SDRAM_MBCF_SZ_MiB_MIN \
133 << (SDRAM_MBCF_SZ_DECODE(n)))
134 #define SDRAM_MBCF_SZ_TO_PAGES(n) (SDRAM_MBCF_SZ_MiB_MIN \
135 << (20 - PAGE_SHIFT + \
136 SDRAM_MBCF_SZ_DECODE(n)))
139 * The ibm,sdram-4xx-ddr2 Device Control Registers (DCRs) are
140 * indirectly accessed and have a base and length defined by the
141 * device tree. The base can be anything; however, we expect the
142 * length to be precisely two registers, the first for the address
143 * window and the second for the data window.
145 #define SDRAM_DCR_RESOURCE_LEN 2
146 #define SDRAM_DCR_ADDR_OFFSET 0
147 #define SDRAM_DCR_DATA_OFFSET 1
150 * Device tree interrupt indices
152 #define INTMAP_ECCDED_INDEX 0 /* Double-bit Error Detect */
153 #define INTMAP_ECCSEC_INDEX 1 /* Single-bit Error Correct */
155 /* Type Definitions */
158 * PPC4xx SDRAM memory controller private instance data
160 struct ppc4xx_edac_pdata
{
161 dcr_host_t dcr_host
; /* Indirect DCR address/data window mapping */
163 int sec
; /* Single-bit correctable error IRQ assigned */
164 int ded
; /* Double-bit detectable error IRQ assigned */
169 * Various status data gathered and manipulated when checking and
170 * reporting ECC status.
172 struct ppc4xx_ecc_status
{
180 /* Function Prototypes */
182 static int ppc4xx_edac_probe(struct platform_device
*device
);
183 static int ppc4xx_edac_remove(struct platform_device
*device
);
185 /* Global Variables */
188 * Device tree node type and compatible tuples this driver can match
191 static const struct of_device_id ppc4xx_edac_match
[] = {
193 .compatible
= "ibm,sdram-4xx-ddr2"
197 MODULE_DEVICE_TABLE(of
, ppc4xx_edac_match
);
199 static struct platform_driver ppc4xx_edac_driver
= {
200 .probe
= ppc4xx_edac_probe
,
201 .remove
= ppc4xx_edac_remove
,
203 .name
= PPC4XX_EDAC_MODULE_NAME
,
204 .of_match_table
= ppc4xx_edac_match
,
209 * TODO: The row and channel parameters likely need to be dynamically
210 * set based on the aforementioned variant controller realizations.
212 static const unsigned ppc4xx_edac_nr_csrows
= 2;
213 static const unsigned ppc4xx_edac_nr_chans
= 1;
216 * Strings associated with PLB master IDs capable of being posted in
217 * SDRAM_BESR or SDRAM_WMIRQ on uncorrectable ECC errors.
219 static const char * const ppc4xx_plb_masters
[9] = {
220 [SDRAM_PLB_M0ID_ICU
] = "ICU",
221 [SDRAM_PLB_M0ID_PCIE0
] = "PCI-E 0",
222 [SDRAM_PLB_M0ID_PCIE1
] = "PCI-E 1",
223 [SDRAM_PLB_M0ID_DMA
] = "DMA",
224 [SDRAM_PLB_M0ID_DCU
] = "DCU",
225 [SDRAM_PLB_M0ID_OPB
] = "OPB",
226 [SDRAM_PLB_M0ID_MAL
] = "MAL",
227 [SDRAM_PLB_M0ID_SEC
] = "SEC",
228 [SDRAM_PLB_M0ID_AHB
] = "AHB"
232 * mfsdram - read and return controller register data
233 * @dcr_host: A pointer to the DCR mapping.
234 * @idcr_n: The indirect DCR register to read.
236 * This routine reads and returns the data associated with the
237 * controller's specified indirect DCR register.
239 * Returns the read data.
242 mfsdram(const dcr_host_t
*dcr_host
, unsigned int idcr_n
)
244 return __mfdcri(dcr_host
->base
+ SDRAM_DCR_ADDR_OFFSET
,
245 dcr_host
->base
+ SDRAM_DCR_DATA_OFFSET
,
250 * mtsdram - write controller register data
251 * @dcr_host: A pointer to the DCR mapping.
252 * @idcr_n: The indirect DCR register to write.
253 * @value: The data to write.
255 * This routine writes the provided data to the controller's specified
256 * indirect DCR register.
259 mtsdram(const dcr_host_t
*dcr_host
, unsigned int idcr_n
, u32 value
)
261 return __mtdcri(dcr_host
->base
+ SDRAM_DCR_ADDR_OFFSET
,
262 dcr_host
->base
+ SDRAM_DCR_DATA_OFFSET
,
268 * ppc4xx_edac_check_bank_error - check a bank for an ECC bank error
269 * @status: A pointer to the ECC status structure to check for an
271 * @bank: The bank to check for an ECC error.
273 * This routine determines whether the specified bank has an ECC
276 * Returns true if the specified bank has an ECC error; otherwise,
280 ppc4xx_edac_check_bank_error(const struct ppc4xx_ecc_status
*status
,
285 return status
->ecces
& SDRAM_ECCES_BK0ER
;
287 return status
->ecces
& SDRAM_ECCES_BK1ER
;
294 * ppc4xx_edac_generate_bank_message - generate interpretted bank status message
295 * @mci: A pointer to the EDAC memory controller instance associated
296 * with the bank message being generated.
297 * @status: A pointer to the ECC status structure to generate the
299 * @buffer: A pointer to the buffer in which to generate the
301 * @size: The size, in bytes, of space available in buffer.
303 * This routine generates to the provided buffer the portion of the
304 * driver-unique report message associated with the ECCESS[BKNER]
305 * field of the specified ECC status.
307 * Returns the number of characters generated on success; otherwise, <
311 ppc4xx_edac_generate_bank_message(const struct mem_ctl_info
*mci
,
312 const struct ppc4xx_ecc_status
*status
,
317 unsigned int row
, rows
;
319 n
= snprintf(buffer
, size
, "%s: Banks: ", mci
->dev_name
);
321 if (n
< 0 || n
>= size
)
328 for (rows
= 0, row
= 0; row
< mci
->nr_csrows
; row
++) {
329 if (ppc4xx_edac_check_bank_error(status
, row
)) {
330 n
= snprintf(buffer
, size
, "%s%u",
331 (rows
++ ? ", " : ""), row
);
333 if (n
< 0 || n
>= size
)
342 n
= snprintf(buffer
, size
, "%s; ", rows
? "" : "None");
344 if (n
< 0 || n
>= size
)
356 * ppc4xx_edac_generate_checkbit_message - generate interpretted checkbit message
357 * @mci: A pointer to the EDAC memory controller instance associated
358 * with the checkbit message being generated.
359 * @status: A pointer to the ECC status structure to generate the
361 * @buffer: A pointer to the buffer in which to generate the
363 * @size: The size, in bytes, of space available in buffer.
365 * This routine generates to the provided buffer the portion of the
366 * driver-unique report message associated with the ECCESS[CKBER]
367 * field of the specified ECC status.
369 * Returns the number of characters generated on success; otherwise, <
373 ppc4xx_edac_generate_checkbit_message(const struct mem_ctl_info
*mci
,
374 const struct ppc4xx_ecc_status
*status
,
378 const struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
379 const char *ckber
= NULL
;
381 switch (status
->ecces
& SDRAM_ECCES_CKBER_MASK
) {
382 case SDRAM_ECCES_CKBER_NONE
:
385 case SDRAM_ECCES_CKBER_32_ECC_0_3
:
388 case SDRAM_ECCES_CKBER_32_ECC_4_8
:
389 switch (mfsdram(&pdata
->dcr_host
, SDRAM_MCOPT1
) &
390 SDRAM_MCOPT1_WDTH_MASK
) {
391 case SDRAM_MCOPT1_WDTH_16
:
394 case SDRAM_MCOPT1_WDTH_32
:
402 case SDRAM_ECCES_CKBER_32_ECC_0_8
:
410 return snprintf(buffer
, size
, "Checkbit Error: %s", ckber
);
414 * ppc4xx_edac_generate_lane_message - generate interpretted byte lane message
415 * @mci: A pointer to the EDAC memory controller instance associated
416 * with the byte lane message being generated.
417 * @status: A pointer to the ECC status structure to generate the
419 * @buffer: A pointer to the buffer in which to generate the
421 * @size: The size, in bytes, of space available in buffer.
423 * This routine generates to the provided buffer the portion of the
424 * driver-unique report message associated with the ECCESS[BNCE]
425 * field of the specified ECC status.
427 * Returns the number of characters generated on success; otherwise, <
431 ppc4xx_edac_generate_lane_message(const struct mem_ctl_info
*mci
,
432 const struct ppc4xx_ecc_status
*status
,
437 unsigned int lane
, lanes
;
438 const unsigned int first_lane
= 0;
439 const unsigned int lane_count
= 16;
441 n
= snprintf(buffer
, size
, "; Byte Lane Errors: ");
443 if (n
< 0 || n
>= size
)
450 for (lanes
= 0, lane
= first_lane
; lane
< lane_count
; lane
++) {
451 if ((status
->ecces
& SDRAM_ECCES_BNCE_ENCODE(lane
)) != 0) {
452 n
= snprintf(buffer
, size
,
454 (lanes
++ ? ", " : ""), lane
);
456 if (n
< 0 || n
>= size
)
465 n
= snprintf(buffer
, size
, "%s; ", lanes
? "" : "None");
467 if (n
< 0 || n
>= size
)
479 * ppc4xx_edac_generate_ecc_message - generate interpretted ECC status message
480 * @mci: A pointer to the EDAC memory controller instance associated
481 * with the ECCES message being generated.
482 * @status: A pointer to the ECC status structure to generate the
484 * @buffer: A pointer to the buffer in which to generate the
486 * @size: The size, in bytes, of space available in buffer.
488 * This routine generates to the provided buffer the portion of the
489 * driver-unique report message associated with the ECCESS register of
490 * the specified ECC status.
492 * Returns the number of characters generated on success; otherwise, <
496 ppc4xx_edac_generate_ecc_message(const struct mem_ctl_info
*mci
,
497 const struct ppc4xx_ecc_status
*status
,
503 n
= ppc4xx_edac_generate_bank_message(mci
, status
, buffer
, size
);
505 if (n
< 0 || n
>= size
)
512 n
= ppc4xx_edac_generate_checkbit_message(mci
, status
, buffer
, size
);
514 if (n
< 0 || n
>= size
)
521 n
= ppc4xx_edac_generate_lane_message(mci
, status
, buffer
, size
);
523 if (n
< 0 || n
>= size
)
535 * ppc4xx_edac_generate_plb_message - generate interpretted PLB status message
536 * @mci: A pointer to the EDAC memory controller instance associated
537 * with the PLB message being generated.
538 * @status: A pointer to the ECC status structure to generate the
540 * @buffer: A pointer to the buffer in which to generate the
542 * @size: The size, in bytes, of space available in buffer.
544 * This routine generates to the provided buffer the portion of the
545 * driver-unique report message associated with the PLB-related BESR
546 * and/or WMIRQ registers of the specified ECC status.
548 * Returns the number of characters generated on success; otherwise, <
552 ppc4xx_edac_generate_plb_message(const struct mem_ctl_info
*mci
,
553 const struct ppc4xx_ecc_status
*status
,
560 if ((status
->besr
& SDRAM_BESR_MASK
) == 0)
563 if ((status
->besr
& SDRAM_BESR_M0ET_MASK
) == SDRAM_BESR_M0ET_NONE
)
566 read
= ((status
->besr
& SDRAM_BESR_M0RW_MASK
) == SDRAM_BESR_M0RW_READ
);
568 master
= SDRAM_BESR_M0ID_DECODE(status
->besr
);
570 return snprintf(buffer
, size
,
571 "%s error w/ PLB master %u \"%s\"; ",
572 (read
? "Read" : "Write"),
574 (((master
>= SDRAM_PLB_M0ID_FIRST
) &&
575 (master
<= SDRAM_PLB_M0ID_LAST
)) ?
576 ppc4xx_plb_masters
[master
] : "UNKNOWN"));
580 * ppc4xx_edac_generate_message - generate interpretted status message
581 * @mci: A pointer to the EDAC memory controller instance associated
582 * with the driver-unique message being generated.
583 * @status: A pointer to the ECC status structure to generate the
585 * @buffer: A pointer to the buffer in which to generate the
587 * @size: The size, in bytes, of space available in buffer.
589 * This routine generates to the provided buffer the driver-unique
590 * EDAC report message from the specified ECC status.
593 ppc4xx_edac_generate_message(const struct mem_ctl_info
*mci
,
594 const struct ppc4xx_ecc_status
*status
,
600 if (buffer
== NULL
|| size
== 0)
603 n
= ppc4xx_edac_generate_ecc_message(mci
, status
, buffer
, size
);
605 if (n
< 0 || n
>= size
)
611 ppc4xx_edac_generate_plb_message(mci
, status
, buffer
, size
);
616 * ppc4xx_ecc_dump_status - dump controller ECC status registers
617 * @mci: A pointer to the EDAC memory controller instance
618 * associated with the status being dumped.
619 * @status: A pointer to the ECC status structure to generate the
622 * This routine dumps to the kernel log buffer the raw and
623 * interpretted specified ECC status.
626 ppc4xx_ecc_dump_status(const struct mem_ctl_info
*mci
,
627 const struct ppc4xx_ecc_status
*status
)
629 char message
[PPC4XX_EDAC_MESSAGE_SIZE
];
631 ppc4xx_edac_generate_message(mci
, status
, message
, sizeof(message
));
633 ppc4xx_edac_mc_printk(KERN_INFO
, mci
,
638 "\tBEAR: 0x%08x%08x\n"
650 * ppc4xx_ecc_get_status - get controller ECC status
651 * @mci: A pointer to the EDAC memory controller instance
652 * associated with the status being retrieved.
653 * @status: A pointer to the ECC status structure to populate the
656 * This routine reads and masks, as appropriate, all the relevant
657 * status registers that deal with ibm,sdram-4xx-ddr2 ECC errors.
658 * While we read all of them, for correctable errors, we only expect
659 * to deal with ECCES. For uncorrectable errors, we expect to deal
663 ppc4xx_ecc_get_status(const struct mem_ctl_info
*mci
,
664 struct ppc4xx_ecc_status
*status
)
666 const struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
667 const dcr_host_t
*dcr_host
= &pdata
->dcr_host
;
669 status
->ecces
= mfsdram(dcr_host
, SDRAM_ECCES
) & SDRAM_ECCES_MASK
;
670 status
->wmirq
= mfsdram(dcr_host
, SDRAM_WMIRQ
) & SDRAM_WMIRQ_MASK
;
671 status
->besr
= mfsdram(dcr_host
, SDRAM_BESR
) & SDRAM_BESR_MASK
;
672 status
->bearl
= mfsdram(dcr_host
, SDRAM_BEARL
);
673 status
->bearh
= mfsdram(dcr_host
, SDRAM_BEARH
);
677 * ppc4xx_ecc_clear_status - clear controller ECC status
678 * @mci: A pointer to the EDAC memory controller instance
679 * associated with the status being cleared.
680 * @status: A pointer to the ECC status structure containing the
681 * values to write to clear the ECC status.
683 * This routine clears--by writing the masked (as appropriate) status
684 * values back to--the status registers that deal with
685 * ibm,sdram-4xx-ddr2 ECC errors.
688 ppc4xx_ecc_clear_status(const struct mem_ctl_info
*mci
,
689 const struct ppc4xx_ecc_status
*status
)
691 const struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
692 const dcr_host_t
*dcr_host
= &pdata
->dcr_host
;
694 mtsdram(dcr_host
, SDRAM_ECCES
, status
->ecces
& SDRAM_ECCES_MASK
);
695 mtsdram(dcr_host
, SDRAM_WMIRQ
, status
->wmirq
& SDRAM_WMIRQ_MASK
);
696 mtsdram(dcr_host
, SDRAM_BESR
, status
->besr
& SDRAM_BESR_MASK
);
697 mtsdram(dcr_host
, SDRAM_BEARL
, 0);
698 mtsdram(dcr_host
, SDRAM_BEARH
, 0);
702 * ppc4xx_edac_handle_ce - handle controller correctable ECC error (CE)
703 * @mci: A pointer to the EDAC memory controller instance
704 * associated with the correctable error being handled and reported.
705 * @status: A pointer to the ECC status structure associated with
706 * the correctable error being handled and reported.
708 * This routine handles an ibm,sdram-4xx-ddr2 controller ECC
709 * correctable error. Per the aforementioned discussion, there's not
710 * enough status available to use the full EDAC correctable error
711 * interface, so we just pass driver-unique message to the "no info"
715 ppc4xx_edac_handle_ce(struct mem_ctl_info
*mci
,
716 const struct ppc4xx_ecc_status
*status
)
719 char message
[PPC4XX_EDAC_MESSAGE_SIZE
];
721 ppc4xx_edac_generate_message(mci
, status
, message
, sizeof(message
));
723 for (row
= 0; row
< mci
->nr_csrows
; row
++)
724 if (ppc4xx_edac_check_bank_error(status
, row
))
725 edac_mc_handle_error(HW_EVENT_ERR_CORRECTED
, mci
, 1,
732 * ppc4xx_edac_handle_ue - handle controller uncorrectable ECC error (UE)
733 * @mci: A pointer to the EDAC memory controller instance
734 * associated with the uncorrectable error being handled and
736 * @status: A pointer to the ECC status structure associated with
737 * the uncorrectable error being handled and reported.
739 * This routine handles an ibm,sdram-4xx-ddr2 controller ECC
740 * uncorrectable error.
743 ppc4xx_edac_handle_ue(struct mem_ctl_info
*mci
,
744 const struct ppc4xx_ecc_status
*status
)
746 const u64 bear
= ((u64
)status
->bearh
<< 32 | status
->bearl
);
747 const unsigned long page
= bear
>> PAGE_SHIFT
;
748 const unsigned long offset
= bear
& ~PAGE_MASK
;
750 char message
[PPC4XX_EDAC_MESSAGE_SIZE
];
752 ppc4xx_edac_generate_message(mci
, status
, message
, sizeof(message
));
754 for (row
= 0; row
< mci
->nr_csrows
; row
++)
755 if (ppc4xx_edac_check_bank_error(status
, row
))
756 edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED
, mci
, 1,
763 * ppc4xx_edac_check - check controller for ECC errors
764 * @mci: A pointer to the EDAC memory controller instance
765 * associated with the ibm,sdram-4xx-ddr2 controller being
768 * This routine is used to check and post ECC errors and is called by
769 * both the EDAC polling thread and this driver's CE and UE interrupt
773 ppc4xx_edac_check(struct mem_ctl_info
*mci
)
776 static unsigned int count
;
778 struct ppc4xx_ecc_status status
;
780 ppc4xx_ecc_get_status(mci
, &status
);
783 if (count
++ % 30 == 0)
784 ppc4xx_ecc_dump_status(mci
, &status
);
787 if (status
.ecces
& SDRAM_ECCES_UE
)
788 ppc4xx_edac_handle_ue(mci
, &status
);
790 if (status
.ecces
& SDRAM_ECCES_CE
)
791 ppc4xx_edac_handle_ce(mci
, &status
);
793 ppc4xx_ecc_clear_status(mci
, &status
);
797 * ppc4xx_edac_isr - SEC (CE) and DED (UE) interrupt service routine
798 * @irq: The virtual interrupt number being serviced.
799 * @dev_id: A pointer to the EDAC memory controller instance
800 * associated with the interrupt being handled.
802 * This routine implements the interrupt handler for both correctable
803 * (CE) and uncorrectable (UE) ECC errors for the ibm,sdram-4xx-ddr2
804 * controller. It simply calls through to the same routine used during
805 * polling to check, report and clear the ECC status.
807 * Unconditionally returns IRQ_HANDLED.
810 ppc4xx_edac_isr(int irq
, void *dev_id
)
812 struct mem_ctl_info
*mci
= dev_id
;
814 ppc4xx_edac_check(mci
);
820 * ppc4xx_edac_get_dtype - return the controller memory width
821 * @mcopt1: The 32-bit Memory Controller Option 1 register value
822 * currently set for the controller, from which the width
825 * This routine returns the EDAC device type width appropriate for the
826 * current controller configuration.
828 * TODO: This needs to be conditioned dynamically through feature
829 * flags or some such when other controller variants are supported as
830 * the 405EX[r] is 16-/32-bit and the others are 32-/64-bit with the
831 * 16- and 64-bit field definition/value/enumeration (b1) overloaded
834 * Returns a device type width enumeration.
836 static enum dev_type
ppc4xx_edac_get_dtype(u32 mcopt1
)
838 switch (mcopt1
& SDRAM_MCOPT1_WDTH_MASK
) {
839 case SDRAM_MCOPT1_WDTH_16
:
841 case SDRAM_MCOPT1_WDTH_32
:
849 * ppc4xx_edac_get_mtype - return controller memory type
850 * @mcopt1: The 32-bit Memory Controller Option 1 register value
851 * currently set for the controller, from which the memory type
854 * This routine returns the EDAC memory type appropriate for the
855 * current controller configuration.
857 * Returns a memory type enumeration.
859 static enum mem_type
ppc4xx_edac_get_mtype(u32 mcopt1
)
861 bool rden
= ((mcopt1
& SDRAM_MCOPT1_RDEN_MASK
) == SDRAM_MCOPT1_RDEN
);
863 switch (mcopt1
& SDRAM_MCOPT1_DDR_TYPE_MASK
) {
864 case SDRAM_MCOPT1_DDR2_TYPE
:
865 return rden
? MEM_RDDR2
: MEM_DDR2
;
866 case SDRAM_MCOPT1_DDR1_TYPE
:
867 return rden
? MEM_RDDR
: MEM_DDR
;
874 * ppc4xx_edac_init_csrows - initialize driver instance rows
875 * @mci: A pointer to the EDAC memory controller instance
876 * associated with the ibm,sdram-4xx-ddr2 controller for which
877 * the csrows (i.e. banks/ranks) are being initialized.
878 * @mcopt1: The 32-bit Memory Controller Option 1 register value
879 * currently set for the controller, from which bank width
880 * and memory typ information is derived.
882 * This routine initializes the virtual "chip select rows" associated
883 * with the EDAC memory controller instance. An ibm,sdram-4xx-ddr2
884 * controller bank/rank is mapped to a row.
886 * Returns 0 if OK; otherwise, -EINVAL if the memory bank size
887 * configuration cannot be determined.
889 static int ppc4xx_edac_init_csrows(struct mem_ctl_info
*mci
, u32 mcopt1
)
891 const struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
895 enum edac_type edac_mode
;
897 u32 mbxcf
, size
, nr_pages
;
899 /* Establish the memory type and width */
901 mtype
= ppc4xx_edac_get_mtype(mcopt1
);
902 dtype
= ppc4xx_edac_get_dtype(mcopt1
);
904 /* Establish EDAC mode */
906 if (mci
->edac_cap
& EDAC_FLAG_SECDED
)
907 edac_mode
= EDAC_SECDED
;
908 else if (mci
->edac_cap
& EDAC_FLAG_EC
)
911 edac_mode
= EDAC_NONE
;
914 * Initialize each chip select row structure which correspond
915 * 1:1 with a controller bank/rank.
918 for (row
= 0; row
< mci
->nr_csrows
; row
++) {
919 struct csrow_info
*csi
= mci
->csrows
[row
];
922 * Get the configuration settings for this
923 * row/bank/rank and skip disabled banks.
926 mbxcf
= mfsdram(&pdata
->dcr_host
, SDRAM_MBXCF(row
));
928 if ((mbxcf
& SDRAM_MBCF_BE_MASK
) != SDRAM_MBCF_BE_ENABLE
)
931 /* Map the bank configuration size setting to pages. */
933 size
= mbxcf
& SDRAM_MBCF_SZ_MASK
;
936 case SDRAM_MBCF_SZ_4MB
:
937 case SDRAM_MBCF_SZ_8MB
:
938 case SDRAM_MBCF_SZ_16MB
:
939 case SDRAM_MBCF_SZ_32MB
:
940 case SDRAM_MBCF_SZ_64MB
:
941 case SDRAM_MBCF_SZ_128MB
:
942 case SDRAM_MBCF_SZ_256MB
:
943 case SDRAM_MBCF_SZ_512MB
:
944 case SDRAM_MBCF_SZ_1GB
:
945 case SDRAM_MBCF_SZ_2GB
:
946 case SDRAM_MBCF_SZ_4GB
:
947 case SDRAM_MBCF_SZ_8GB
:
948 nr_pages
= SDRAM_MBCF_SZ_TO_PAGES(size
);
951 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
952 "Unrecognized memory bank %d "
954 row
, SDRAM_MBCF_SZ_DECODE(size
));
960 * It's unclear exactly what grain should be set to
961 * here. The SDRAM_ECCES register allows resolution of
962 * an error down to a nibble which would potentially
963 * argue for a grain of '1' byte, even though we only
964 * know the associated address for uncorrectable
965 * errors. This value is not used at present for
966 * anything other than error reporting so getting it
967 * wrong should be of little consequence. Other
968 * possible values would be the PLB width (16), the
969 * page size (PAGE_SIZE) or the memory width (2 or 4).
971 for (j
= 0; j
< csi
->nr_channels
; j
++) {
972 struct dimm_info
*dimm
= csi
->channels
[j
]->dimm
;
974 dimm
->nr_pages
= nr_pages
/ csi
->nr_channels
;
980 dimm
->edac_mode
= edac_mode
;
989 * ppc4xx_edac_mc_init - initialize driver instance
990 * @mci: A pointer to the EDAC memory controller instance being
992 * @op: A pointer to the OpenFirmware device tree node associated
993 * with the controller this EDAC instance is bound to.
994 * @dcr_host: A pointer to the DCR data containing the DCR mapping
995 * for this controller instance.
996 * @mcopt1: The 32-bit Memory Controller Option 1 register value
997 * currently set for the controller, from which ECC capabilities
998 * and scrub mode are derived.
1000 * This routine performs initialization of the EDAC memory controller
1001 * instance and related driver-private data associated with the
1002 * ibm,sdram-4xx-ddr2 memory controller the instance is bound to.
1004 * Returns 0 if OK; otherwise, < 0 on error.
1006 static int ppc4xx_edac_mc_init(struct mem_ctl_info
*mci
,
1007 struct platform_device
*op
,
1008 const dcr_host_t
*dcr_host
, u32 mcopt1
)
1011 const u32 memcheck
= (mcopt1
& SDRAM_MCOPT1_MCHK_MASK
);
1012 struct ppc4xx_edac_pdata
*pdata
= NULL
;
1013 const struct device_node
*np
= op
->dev
.of_node
;
1015 if (of_match_device(ppc4xx_edac_match
, &op
->dev
) == NULL
)
1018 /* Initial driver pointers and private data */
1020 mci
->pdev
= &op
->dev
;
1022 dev_set_drvdata(mci
->pdev
, mci
);
1024 pdata
= mci
->pvt_info
;
1026 pdata
->dcr_host
= *dcr_host
;
1028 /* Initialize controller capabilities and configuration */
1030 mci
->mtype_cap
= (MEM_FLAG_DDR
| MEM_FLAG_RDDR
|
1031 MEM_FLAG_DDR2
| MEM_FLAG_RDDR2
);
1033 mci
->edac_ctl_cap
= (EDAC_FLAG_NONE
|
1037 mci
->scrub_cap
= SCRUB_NONE
;
1038 mci
->scrub_mode
= SCRUB_NONE
;
1041 * Update the actual capabilites based on the MCOPT1[MCHK]
1042 * settings. Scrubbing is only useful if reporting is enabled.
1046 case SDRAM_MCOPT1_MCHK_CHK
:
1047 mci
->edac_cap
= EDAC_FLAG_EC
;
1049 case SDRAM_MCOPT1_MCHK_CHK_REP
:
1050 mci
->edac_cap
= (EDAC_FLAG_EC
| EDAC_FLAG_SECDED
);
1051 mci
->scrub_mode
= SCRUB_SW_SRC
;
1054 mci
->edac_cap
= EDAC_FLAG_NONE
;
1058 /* Initialize strings */
1060 mci
->mod_name
= PPC4XX_EDAC_MODULE_NAME
;
1061 mci
->ctl_name
= ppc4xx_edac_match
->compatible
,
1062 mci
->dev_name
= np
->full_name
;
1064 /* Initialize callbacks */
1066 mci
->edac_check
= ppc4xx_edac_check
;
1067 mci
->ctl_page_to_phys
= NULL
;
1069 /* Initialize chip select rows */
1071 status
= ppc4xx_edac_init_csrows(mci
, mcopt1
);
1074 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1075 "Failed to initialize rows!\n");
1081 * ppc4xx_edac_register_irq - setup and register controller interrupts
1082 * @op: A pointer to the OpenFirmware device tree node associated
1083 * with the controller this EDAC instance is bound to.
1084 * @mci: A pointer to the EDAC memory controller instance
1085 * associated with the ibm,sdram-4xx-ddr2 controller for which
1086 * interrupts are being registered.
1088 * This routine parses the correctable (CE) and uncorrectable error (UE)
1089 * interrupts from the device tree node and maps and assigns them to
1090 * the associated EDAC memory controller instance.
1092 * Returns 0 if OK; otherwise, -ENODEV if the interrupts could not be
1093 * mapped and assigned.
1095 static int ppc4xx_edac_register_irq(struct platform_device
*op
,
1096 struct mem_ctl_info
*mci
)
1099 int ded_irq
, sec_irq
;
1100 struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
1101 struct device_node
*np
= op
->dev
.of_node
;
1103 ded_irq
= irq_of_parse_and_map(np
, INTMAP_ECCDED_INDEX
);
1104 sec_irq
= irq_of_parse_and_map(np
, INTMAP_ECCSEC_INDEX
);
1106 if (!ded_irq
|| !sec_irq
) {
1107 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1108 "Unable to map interrupts.\n");
1113 status
= request_irq(ded_irq
,
1120 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1121 "Unable to request irq %d for ECC DED",
1127 status
= request_irq(sec_irq
,
1134 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1135 "Unable to request irq %d for ECC SEC",
1141 ppc4xx_edac_mc_printk(KERN_INFO
, mci
, "ECCDED irq is %d\n", ded_irq
);
1142 ppc4xx_edac_mc_printk(KERN_INFO
, mci
, "ECCSEC irq is %d\n", sec_irq
);
1144 pdata
->irqs
.ded
= ded_irq
;
1145 pdata
->irqs
.sec
= sec_irq
;
1150 free_irq(sec_irq
, mci
);
1153 free_irq(ded_irq
, mci
);
1160 * ppc4xx_edac_map_dcrs - locate and map controller registers
1161 * @np: A pointer to the device tree node containing the DCR
1163 * @dcr_host: A pointer to the DCR data to populate with the
1166 * This routine attempts to locate in the device tree and map the DCR
1167 * register resources associated with the controller's indirect DCR
1168 * address and data windows.
1170 * Returns 0 if the DCRs were successfully mapped; otherwise, < 0 on
1173 static int ppc4xx_edac_map_dcrs(const struct device_node
*np
,
1174 dcr_host_t
*dcr_host
)
1176 unsigned int dcr_base
, dcr_len
;
1178 if (np
== NULL
|| dcr_host
== NULL
)
1181 /* Get the DCR resource extent and sanity check the values. */
1183 dcr_base
= dcr_resource_start(np
, 0);
1184 dcr_len
= dcr_resource_len(np
, 0);
1186 if (dcr_base
== 0 || dcr_len
== 0) {
1187 ppc4xx_edac_printk(KERN_ERR
,
1188 "Failed to obtain DCR property.\n");
1192 if (dcr_len
!= SDRAM_DCR_RESOURCE_LEN
) {
1193 ppc4xx_edac_printk(KERN_ERR
,
1194 "Unexpected DCR length %d, expected %d.\n",
1195 dcr_len
, SDRAM_DCR_RESOURCE_LEN
);
1199 /* Attempt to map the DCR extent. */
1201 *dcr_host
= dcr_map(np
, dcr_base
, dcr_len
);
1203 if (!DCR_MAP_OK(*dcr_host
)) {
1204 ppc4xx_edac_printk(KERN_INFO
, "Failed to map DCRs.\n");
1212 * ppc4xx_edac_probe - check controller and bind driver
1213 * @op: A pointer to the OpenFirmware device tree node associated
1214 * with the controller being probed for driver binding.
1216 * This routine probes a specific ibm,sdram-4xx-ddr2 controller
1217 * instance for binding with the driver.
1219 * Returns 0 if the controller instance was successfully bound to the
1220 * driver; otherwise, < 0 on error.
1222 static int ppc4xx_edac_probe(struct platform_device
*op
)
1225 u32 mcopt1
, memcheck
;
1226 dcr_host_t dcr_host
;
1227 const struct device_node
*np
= op
->dev
.of_node
;
1228 struct mem_ctl_info
*mci
= NULL
;
1229 struct edac_mc_layer layers
[2];
1230 static int ppc4xx_edac_instance
;
1233 * At this point, we only support the controller realized on
1234 * the AMCC PPC 405EX[r]. Reject anything else.
1237 if (!of_device_is_compatible(np
, "ibm,sdram-405ex") &&
1238 !of_device_is_compatible(np
, "ibm,sdram-405exr")) {
1239 ppc4xx_edac_printk(KERN_NOTICE
,
1240 "Only the PPC405EX[r] is supported.\n");
1245 * Next, get the DCR property and attempt to map it so that we
1246 * can probe the controller.
1249 status
= ppc4xx_edac_map_dcrs(np
, &dcr_host
);
1255 * First determine whether ECC is enabled at all. If not,
1256 * there is no useful checking or monitoring that can be done
1257 * for this controller.
1260 mcopt1
= mfsdram(&dcr_host
, SDRAM_MCOPT1
);
1261 memcheck
= (mcopt1
& SDRAM_MCOPT1_MCHK_MASK
);
1263 if (memcheck
== SDRAM_MCOPT1_MCHK_NON
) {
1264 ppc4xx_edac_printk(KERN_INFO
, "%pOF: No ECC memory detected or "
1265 "ECC is disabled.\n", np
);
1271 * At this point, we know ECC is enabled, allocate an EDAC
1272 * controller instance and perform the appropriate
1275 layers
[0].type
= EDAC_MC_LAYER_CHIP_SELECT
;
1276 layers
[0].size
= ppc4xx_edac_nr_csrows
;
1277 layers
[0].is_virt_csrow
= true;
1278 layers
[1].type
= EDAC_MC_LAYER_CHANNEL
;
1279 layers
[1].size
= ppc4xx_edac_nr_chans
;
1280 layers
[1].is_virt_csrow
= false;
1281 mci
= edac_mc_alloc(ppc4xx_edac_instance
, ARRAY_SIZE(layers
), layers
,
1282 sizeof(struct ppc4xx_edac_pdata
));
1284 ppc4xx_edac_printk(KERN_ERR
, "%pOF: "
1285 "Failed to allocate EDAC MC instance!\n",
1291 status
= ppc4xx_edac_mc_init(mci
, op
, &dcr_host
, mcopt1
);
1294 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1295 "Failed to initialize instance!\n");
1300 * We have a valid, initialized EDAC instance bound to the
1301 * controller. Attempt to register it with the EDAC subsystem
1302 * and, if necessary, register interrupts.
1305 if (edac_mc_add_mc(mci
)) {
1306 ppc4xx_edac_mc_printk(KERN_ERR
, mci
,
1307 "Failed to add instance!\n");
1312 if (edac_op_state
== EDAC_OPSTATE_INT
) {
1313 status
= ppc4xx_edac_register_irq(op
, mci
);
1319 ppc4xx_edac_instance
++;
1324 edac_mc_del_mc(mci
->pdev
);
1334 * ppc4xx_edac_remove - unbind driver from controller
1335 * @op: A pointer to the OpenFirmware device tree node associated
1336 * with the controller this EDAC instance is to be unbound/removed
1339 * This routine unbinds the EDAC memory controller instance associated
1340 * with the specified ibm,sdram-4xx-ddr2 controller described by the
1341 * OpenFirmware device tree node passed as a parameter.
1343 * Unconditionally returns 0.
1346 ppc4xx_edac_remove(struct platform_device
*op
)
1348 struct mem_ctl_info
*mci
= dev_get_drvdata(&op
->dev
);
1349 struct ppc4xx_edac_pdata
*pdata
= mci
->pvt_info
;
1351 if (edac_op_state
== EDAC_OPSTATE_INT
) {
1352 free_irq(pdata
->irqs
.sec
, mci
);
1353 free_irq(pdata
->irqs
.ded
, mci
);
1356 dcr_unmap(pdata
->dcr_host
, SDRAM_DCR_RESOURCE_LEN
);
1358 edac_mc_del_mc(mci
->pdev
);
1365 * ppc4xx_edac_opstate_init - initialize EDAC reporting method
1367 * This routine ensures that the EDAC memory controller reporting
1368 * method is mapped to a sane value as the EDAC core defines the value
1369 * to EDAC_OPSTATE_INVAL by default. We don't call the global
1370 * opstate_init as that defaults to polling and we want interrupt as
1373 static inline void __init
1374 ppc4xx_edac_opstate_init(void)
1376 switch (edac_op_state
) {
1377 case EDAC_OPSTATE_POLL
:
1378 case EDAC_OPSTATE_INT
:
1381 edac_op_state
= EDAC_OPSTATE_INT
;
1385 ppc4xx_edac_printk(KERN_INFO
, "Reporting type: %s\n",
1386 ((edac_op_state
== EDAC_OPSTATE_POLL
) ?
1387 EDAC_OPSTATE_POLL_STR
:
1388 ((edac_op_state
== EDAC_OPSTATE_INT
) ?
1389 EDAC_OPSTATE_INT_STR
:
1390 EDAC_OPSTATE_UNKNOWN_STR
)));
1394 * ppc4xx_edac_init - driver/module insertion entry point
1396 * This routine is the driver/module insertion entry point. It
1397 * initializes the EDAC memory controller reporting state and
1398 * registers the driver as an OpenFirmware device tree platform
1402 ppc4xx_edac_init(void)
1404 ppc4xx_edac_printk(KERN_INFO
, PPC4XX_EDAC_MODULE_REVISION
"\n");
1406 ppc4xx_edac_opstate_init();
1408 return platform_driver_register(&ppc4xx_edac_driver
);
1412 * ppc4xx_edac_exit - driver/module removal entry point
1414 * This routine is the driver/module removal entry point. It
1415 * unregisters the driver as an OpenFirmware device tree platform
1419 ppc4xx_edac_exit(void)
1421 platform_driver_unregister(&ppc4xx_edac_driver
);
1424 module_init(ppc4xx_edac_init
);
1425 module_exit(ppc4xx_edac_exit
);
1427 MODULE_LICENSE("GPL v2");
1428 MODULE_AUTHOR("Grant Erickson <gerickson@nuovations.com>");
1429 MODULE_DESCRIPTION("EDAC MC Driver for the PPC4xx IBM DDR2 Memory Controller");
1430 module_param(edac_op_state
, int, 0444);
1431 MODULE_PARM_DESC(edac_op_state
, "EDAC Error Reporting State: "
1432 "0=" EDAC_OPSTATE_POLL_STR
", 2=" EDAC_OPSTATE_INT_STR
);