xref: /openbmc/linux/drivers/edac/amd76x_edac.c (revision 806c35f5057a64d3061ee4e2b1023bf6f6d328e2)
1*806c35f5SAlan Cox /*
2*806c35f5SAlan Cox  * AMD 76x Memory Controller kernel module
3*806c35f5SAlan Cox  * (C) 2003 Linux Networx (http://lnxi.com)
4*806c35f5SAlan Cox  * This file may be distributed under the terms of the
5*806c35f5SAlan Cox  * GNU General Public License.
6*806c35f5SAlan Cox  *
7*806c35f5SAlan Cox  * Written by Thayne Harbaugh
8*806c35f5SAlan Cox  * Based on work by Dan Hollis <goemon at anime dot net> and others.
9*806c35f5SAlan Cox  *	http://www.anime.net/~goemon/linux-ecc/
10*806c35f5SAlan Cox  *
11*806c35f5SAlan Cox  * $Id: edac_amd76x.c,v 1.4.2.5 2005/10/05 00:43:44 dsp_llnl Exp $
12*806c35f5SAlan Cox  *
13*806c35f5SAlan Cox  */
14*806c35f5SAlan Cox 
15*806c35f5SAlan Cox 
16*806c35f5SAlan Cox #include <linux/config.h>
17*806c35f5SAlan Cox #include <linux/module.h>
18*806c35f5SAlan Cox #include <linux/init.h>
19*806c35f5SAlan Cox 
20*806c35f5SAlan Cox #include <linux/pci.h>
21*806c35f5SAlan Cox #include <linux/pci_ids.h>
22*806c35f5SAlan Cox 
23*806c35f5SAlan Cox #include <linux/slab.h>
24*806c35f5SAlan Cox 
25*806c35f5SAlan Cox #include "edac_mc.h"
26*806c35f5SAlan Cox 
27*806c35f5SAlan Cox 
28*806c35f5SAlan Cox #define AMD76X_NR_CSROWS 8
29*806c35f5SAlan Cox #define AMD76X_NR_CHANS  1
30*806c35f5SAlan Cox #define AMD76X_NR_DIMMS  4
31*806c35f5SAlan Cox 
32*806c35f5SAlan Cox 
33*806c35f5SAlan Cox /* AMD 76x register addresses - device 0 function 0 - PCI bridge */
34*806c35f5SAlan Cox #define AMD76X_ECC_MODE_STATUS	0x48	/* Mode and status of ECC (32b)
35*806c35f5SAlan Cox 					 *
36*806c35f5SAlan Cox 					 * 31:16 reserved
37*806c35f5SAlan Cox 					 * 15:14 SERR enabled: x1=ue 1x=ce
38*806c35f5SAlan Cox 					 * 13    reserved
39*806c35f5SAlan Cox 					 * 12    diag: disabled, enabled
40*806c35f5SAlan Cox 					 * 11:10 mode: dis, EC, ECC, ECC+scrub
41*806c35f5SAlan Cox 					 *  9:8  status: x1=ue 1x=ce
42*806c35f5SAlan Cox 					 *  7:4  UE cs row
43*806c35f5SAlan Cox 					 *  3:0  CE cs row
44*806c35f5SAlan Cox 					 */
45*806c35f5SAlan Cox #define AMD76X_DRAM_MODE_STATUS	0x58	/* DRAM Mode and status (32b)
46*806c35f5SAlan Cox 					 *
47*806c35f5SAlan Cox 					 * 31:26 clock disable 5 - 0
48*806c35f5SAlan Cox 					 * 25    SDRAM init
49*806c35f5SAlan Cox 					 * 24    reserved
50*806c35f5SAlan Cox 					 * 23    mode register service
51*806c35f5SAlan Cox 					 * 22:21 suspend to RAM
52*806c35f5SAlan Cox 					 * 20    burst refresh enable
53*806c35f5SAlan Cox 					 * 19    refresh disable
54*806c35f5SAlan Cox 					 * 18    reserved
55*806c35f5SAlan Cox 					 * 17:16 cycles-per-refresh
56*806c35f5SAlan Cox 					 * 15:8  reserved
57*806c35f5SAlan Cox 					 *  7:0  x4 mode enable 7 - 0
58*806c35f5SAlan Cox 					 */
59*806c35f5SAlan Cox #define AMD76X_MEM_BASE_ADDR	0xC0	/* Memory base address (8 x 32b)
60*806c35f5SAlan Cox 					 *
61*806c35f5SAlan Cox 					 * 31:23 chip-select base
62*806c35f5SAlan Cox 					 * 22:16 reserved
63*806c35f5SAlan Cox 					 * 15:7  chip-select mask
64*806c35f5SAlan Cox 					 *  6:3  reserved
65*806c35f5SAlan Cox 					 *  2:1  address mode
66*806c35f5SAlan Cox 					 *  0    chip-select enable
67*806c35f5SAlan Cox 					 */
68*806c35f5SAlan Cox 
69*806c35f5SAlan Cox 
70*806c35f5SAlan Cox struct amd76x_error_info {
71*806c35f5SAlan Cox 	u32 ecc_mode_status;
72*806c35f5SAlan Cox };
73*806c35f5SAlan Cox 
74*806c35f5SAlan Cox 
75*806c35f5SAlan Cox enum amd76x_chips {
76*806c35f5SAlan Cox 	AMD761 = 0,
77*806c35f5SAlan Cox 	AMD762
78*806c35f5SAlan Cox };
79*806c35f5SAlan Cox 
80*806c35f5SAlan Cox 
81*806c35f5SAlan Cox struct amd76x_dev_info {
82*806c35f5SAlan Cox 	const char *ctl_name;
83*806c35f5SAlan Cox };
84*806c35f5SAlan Cox 
85*806c35f5SAlan Cox 
86*806c35f5SAlan Cox static const struct amd76x_dev_info amd76x_devs[] = {
87*806c35f5SAlan Cox 	[AMD761] = {.ctl_name = "AMD761"},
88*806c35f5SAlan Cox 	[AMD762] = {.ctl_name = "AMD762"},
89*806c35f5SAlan Cox };
90*806c35f5SAlan Cox 
91*806c35f5SAlan Cox 
92*806c35f5SAlan Cox /**
93*806c35f5SAlan Cox  *	amd76x_get_error_info	-	fetch error information
94*806c35f5SAlan Cox  *	@mci: Memory controller
95*806c35f5SAlan Cox  *	@info: Info to fill in
96*806c35f5SAlan Cox  *
97*806c35f5SAlan Cox  *	Fetch and store the AMD76x ECC status. Clear pending status
98*806c35f5SAlan Cox  *	on the chip so that further errors will be reported
99*806c35f5SAlan Cox  */
100*806c35f5SAlan Cox 
101*806c35f5SAlan Cox static void amd76x_get_error_info (struct mem_ctl_info *mci,
102*806c35f5SAlan Cox 				   struct amd76x_error_info *info)
103*806c35f5SAlan Cox {
104*806c35f5SAlan Cox 	pci_read_config_dword(mci->pdev, AMD76X_ECC_MODE_STATUS,
105*806c35f5SAlan Cox 				&info->ecc_mode_status);
106*806c35f5SAlan Cox 
107*806c35f5SAlan Cox 	if (info->ecc_mode_status & BIT(8))
108*806c35f5SAlan Cox 		pci_write_bits32(mci->pdev, AMD76X_ECC_MODE_STATUS,
109*806c35f5SAlan Cox 				   (u32) BIT(8), (u32) BIT(8));
110*806c35f5SAlan Cox 
111*806c35f5SAlan Cox 	if (info->ecc_mode_status & BIT(9))
112*806c35f5SAlan Cox 		pci_write_bits32(mci->pdev, AMD76X_ECC_MODE_STATUS,
113*806c35f5SAlan Cox 				   (u32) BIT(9), (u32) BIT(9));
114*806c35f5SAlan Cox }
115*806c35f5SAlan Cox 
116*806c35f5SAlan Cox 
117*806c35f5SAlan Cox /**
118*806c35f5SAlan Cox  *	amd76x_process_error_info	-	Error check
119*806c35f5SAlan Cox  *	@mci: Memory controller
120*806c35f5SAlan Cox  *	@info: Previously fetched information from chip
121*806c35f5SAlan Cox  *	@handle_errors: 1 if we should do recovery
122*806c35f5SAlan Cox  *
123*806c35f5SAlan Cox  *	Process the chip state and decide if an error has occurred.
124*806c35f5SAlan Cox  *	A return of 1 indicates an error. Also if handle_errors is true
125*806c35f5SAlan Cox  *	then attempt to handle and clean up after the error
126*806c35f5SAlan Cox  */
127*806c35f5SAlan Cox 
128*806c35f5SAlan Cox static int amd76x_process_error_info (struct mem_ctl_info *mci,
129*806c35f5SAlan Cox 		struct amd76x_error_info *info, int handle_errors)
130*806c35f5SAlan Cox {
131*806c35f5SAlan Cox 	int error_found;
132*806c35f5SAlan Cox 	u32 row;
133*806c35f5SAlan Cox 
134*806c35f5SAlan Cox 	error_found = 0;
135*806c35f5SAlan Cox 
136*806c35f5SAlan Cox 	/*
137*806c35f5SAlan Cox 	 *	Check for an uncorrectable error
138*806c35f5SAlan Cox 	 */
139*806c35f5SAlan Cox 	if (info->ecc_mode_status & BIT(8)) {
140*806c35f5SAlan Cox 		error_found = 1;
141*806c35f5SAlan Cox 
142*806c35f5SAlan Cox 		if (handle_errors) {
143*806c35f5SAlan Cox 			row = (info->ecc_mode_status >> 4) & 0xf;
144*806c35f5SAlan Cox 			edac_mc_handle_ue(mci,
145*806c35f5SAlan Cox 			    mci->csrows[row].first_page, 0, row,
146*806c35f5SAlan Cox 			    mci->ctl_name);
147*806c35f5SAlan Cox 		}
148*806c35f5SAlan Cox 	}
149*806c35f5SAlan Cox 
150*806c35f5SAlan Cox 	/*
151*806c35f5SAlan Cox 	 *	Check for a correctable error
152*806c35f5SAlan Cox 	 */
153*806c35f5SAlan Cox 	if (info->ecc_mode_status & BIT(9)) {
154*806c35f5SAlan Cox 		error_found = 1;
155*806c35f5SAlan Cox 
156*806c35f5SAlan Cox 		if (handle_errors) {
157*806c35f5SAlan Cox 			row = info->ecc_mode_status & 0xf;
158*806c35f5SAlan Cox 			edac_mc_handle_ce(mci,
159*806c35f5SAlan Cox 			    mci->csrows[row].first_page, 0, 0, row, 0,
160*806c35f5SAlan Cox 			    mci->ctl_name);
161*806c35f5SAlan Cox 		}
162*806c35f5SAlan Cox 	}
163*806c35f5SAlan Cox 	return error_found;
164*806c35f5SAlan Cox }
165*806c35f5SAlan Cox 
166*806c35f5SAlan Cox /**
167*806c35f5SAlan Cox  *	amd76x_check	-	Poll the controller
168*806c35f5SAlan Cox  *	@mci: Memory controller
169*806c35f5SAlan Cox  *
170*806c35f5SAlan Cox  *	Called by the poll handlers this function reads the status
171*806c35f5SAlan Cox  *	from the controller and checks for errors.
172*806c35f5SAlan Cox  */
173*806c35f5SAlan Cox 
174*806c35f5SAlan Cox static void amd76x_check(struct mem_ctl_info *mci)
175*806c35f5SAlan Cox {
176*806c35f5SAlan Cox 	struct amd76x_error_info info;
177*806c35f5SAlan Cox 	debugf3("MC: " __FILE__ ": %s()\n", __func__);
178*806c35f5SAlan Cox 	amd76x_get_error_info(mci, &info);
179*806c35f5SAlan Cox 	amd76x_process_error_info(mci, &info, 1);
180*806c35f5SAlan Cox }
181*806c35f5SAlan Cox 
182*806c35f5SAlan Cox 
183*806c35f5SAlan Cox /**
184*806c35f5SAlan Cox  *	amd76x_probe1	-	Perform set up for detected device
185*806c35f5SAlan Cox  *	@pdev; PCI device detected
186*806c35f5SAlan Cox  *	@dev_idx: Device type index
187*806c35f5SAlan Cox  *
188*806c35f5SAlan Cox  *	We have found an AMD76x and now need to set up the memory
189*806c35f5SAlan Cox  *	controller status reporting. We configure and set up the
190*806c35f5SAlan Cox  *	memory controller reporting and claim the device.
191*806c35f5SAlan Cox  */
192*806c35f5SAlan Cox 
193*806c35f5SAlan Cox static int amd76x_probe1(struct pci_dev *pdev, int dev_idx)
194*806c35f5SAlan Cox {
195*806c35f5SAlan Cox 	int rc = -ENODEV;
196*806c35f5SAlan Cox 	int index;
197*806c35f5SAlan Cox 	struct mem_ctl_info *mci = NULL;
198*806c35f5SAlan Cox 	enum edac_type ems_modes[] = {
199*806c35f5SAlan Cox 		EDAC_NONE,
200*806c35f5SAlan Cox 		EDAC_EC,
201*806c35f5SAlan Cox 		EDAC_SECDED,
202*806c35f5SAlan Cox 		EDAC_SECDED
203*806c35f5SAlan Cox 	};
204*806c35f5SAlan Cox 	u32 ems;
205*806c35f5SAlan Cox 	u32 ems_mode;
206*806c35f5SAlan Cox 
207*806c35f5SAlan Cox 	debugf0("MC: " __FILE__ ": %s()\n", __func__);
208*806c35f5SAlan Cox 
209*806c35f5SAlan Cox 	pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS, &ems);
210*806c35f5SAlan Cox 	ems_mode = (ems >> 10) & 0x3;
211*806c35f5SAlan Cox 
212*806c35f5SAlan Cox 	mci = edac_mc_alloc(0, AMD76X_NR_CSROWS, AMD76X_NR_CHANS);
213*806c35f5SAlan Cox 
214*806c35f5SAlan Cox 	if (mci == NULL) {
215*806c35f5SAlan Cox 		rc = -ENOMEM;
216*806c35f5SAlan Cox 		goto fail;
217*806c35f5SAlan Cox 	}
218*806c35f5SAlan Cox 
219*806c35f5SAlan Cox 	debugf0("MC: " __FILE__ ": %s(): mci = %p\n", __func__, mci);
220*806c35f5SAlan Cox 
221*806c35f5SAlan Cox 	mci->pdev = pci_dev_get(pdev);
222*806c35f5SAlan Cox 	mci->mtype_cap = MEM_FLAG_RDDR;
223*806c35f5SAlan Cox 
224*806c35f5SAlan Cox 	mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_EC | EDAC_FLAG_SECDED;
225*806c35f5SAlan Cox 	mci->edac_cap = ems_mode ?
226*806c35f5SAlan Cox 	    (EDAC_FLAG_EC | EDAC_FLAG_SECDED) : EDAC_FLAG_NONE;
227*806c35f5SAlan Cox 
228*806c35f5SAlan Cox 	mci->mod_name = BS_MOD_STR;
229*806c35f5SAlan Cox 	mci->mod_ver = "$Revision: 1.4.2.5 $";
230*806c35f5SAlan Cox 	mci->ctl_name = amd76x_devs[dev_idx].ctl_name;
231*806c35f5SAlan Cox 	mci->edac_check = amd76x_check;
232*806c35f5SAlan Cox 	mci->ctl_page_to_phys = NULL;
233*806c35f5SAlan Cox 
234*806c35f5SAlan Cox 	for (index = 0; index < mci->nr_csrows; index++) {
235*806c35f5SAlan Cox 		struct csrow_info *csrow = &mci->csrows[index];
236*806c35f5SAlan Cox 		u32 mba;
237*806c35f5SAlan Cox 		u32 mba_base;
238*806c35f5SAlan Cox 		u32 mba_mask;
239*806c35f5SAlan Cox 		u32 dms;
240*806c35f5SAlan Cox 
241*806c35f5SAlan Cox 		/* find the DRAM Chip Select Base address and mask */
242*806c35f5SAlan Cox 		pci_read_config_dword(mci->pdev,
243*806c35f5SAlan Cox 				      AMD76X_MEM_BASE_ADDR + (index * 4),
244*806c35f5SAlan Cox 				      &mba);
245*806c35f5SAlan Cox 
246*806c35f5SAlan Cox 		if (!(mba & BIT(0)))
247*806c35f5SAlan Cox 			continue;
248*806c35f5SAlan Cox 
249*806c35f5SAlan Cox 		mba_base = mba & 0xff800000UL;
250*806c35f5SAlan Cox 		mba_mask = ((mba & 0xff80) << 16) | 0x7fffffUL;
251*806c35f5SAlan Cox 
252*806c35f5SAlan Cox 		pci_read_config_dword(mci->pdev, AMD76X_DRAM_MODE_STATUS,
253*806c35f5SAlan Cox 				      &dms);
254*806c35f5SAlan Cox 
255*806c35f5SAlan Cox 		csrow->first_page = mba_base >> PAGE_SHIFT;
256*806c35f5SAlan Cox 		csrow->nr_pages = (mba_mask + 1) >> PAGE_SHIFT;
257*806c35f5SAlan Cox 		csrow->last_page = csrow->first_page + csrow->nr_pages - 1;
258*806c35f5SAlan Cox 		csrow->page_mask = mba_mask >> PAGE_SHIFT;
259*806c35f5SAlan Cox 		csrow->grain = csrow->nr_pages << PAGE_SHIFT;
260*806c35f5SAlan Cox 		csrow->mtype = MEM_RDDR;
261*806c35f5SAlan Cox 		csrow->dtype = ((dms >> index) & 0x1) ? DEV_X4 : DEV_UNKNOWN;
262*806c35f5SAlan Cox 		csrow->edac_mode = ems_modes[ems_mode];
263*806c35f5SAlan Cox 	}
264*806c35f5SAlan Cox 
265*806c35f5SAlan Cox 	/* clear counters */
266*806c35f5SAlan Cox 	pci_write_bits32(mci->pdev, AMD76X_ECC_MODE_STATUS, (u32) (0x3 << 8),
267*806c35f5SAlan Cox 			 (u32) (0x3 << 8));
268*806c35f5SAlan Cox 
269*806c35f5SAlan Cox 	if (edac_mc_add_mc(mci)) {
270*806c35f5SAlan Cox 		debugf3("MC: " __FILE__
271*806c35f5SAlan Cox 			": %s(): failed edac_mc_add_mc()\n", __func__);
272*806c35f5SAlan Cox 		goto fail;
273*806c35f5SAlan Cox 	}
274*806c35f5SAlan Cox 
275*806c35f5SAlan Cox 	/* get this far and it's successful */
276*806c35f5SAlan Cox 	debugf3("MC: " __FILE__ ": %s(): success\n", __func__);
277*806c35f5SAlan Cox 	return 0;
278*806c35f5SAlan Cox 
279*806c35f5SAlan Cox fail:
280*806c35f5SAlan Cox 	if (mci) {
281*806c35f5SAlan Cox 		if(mci->pdev)
282*806c35f5SAlan Cox 			pci_dev_put(mci->pdev);
283*806c35f5SAlan Cox 		edac_mc_free(mci);
284*806c35f5SAlan Cox 	}
285*806c35f5SAlan Cox 	return rc;
286*806c35f5SAlan Cox }
287*806c35f5SAlan Cox 
288*806c35f5SAlan Cox /* returns count (>= 0), or negative on error */
289*806c35f5SAlan Cox static int __devinit amd76x_init_one(struct pci_dev *pdev,
290*806c35f5SAlan Cox 				     const struct pci_device_id *ent)
291*806c35f5SAlan Cox {
292*806c35f5SAlan Cox 	debugf0("MC: " __FILE__ ": %s()\n", __func__);
293*806c35f5SAlan Cox 
294*806c35f5SAlan Cox 	/* don't need to call pci_device_enable() */
295*806c35f5SAlan Cox 	return amd76x_probe1(pdev, ent->driver_data);
296*806c35f5SAlan Cox }
297*806c35f5SAlan Cox 
298*806c35f5SAlan Cox 
299*806c35f5SAlan Cox /**
300*806c35f5SAlan Cox  *	amd76x_remove_one	-	driver shutdown
301*806c35f5SAlan Cox  *	@pdev: PCI device being handed back
302*806c35f5SAlan Cox  *
303*806c35f5SAlan Cox  *	Called when the driver is unloaded. Find the matching mci
304*806c35f5SAlan Cox  *	structure for the device then delete the mci and free the
305*806c35f5SAlan Cox  *	resources.
306*806c35f5SAlan Cox  */
307*806c35f5SAlan Cox 
308*806c35f5SAlan Cox static void __devexit amd76x_remove_one(struct pci_dev *pdev)
309*806c35f5SAlan Cox {
310*806c35f5SAlan Cox 	struct mem_ctl_info *mci;
311*806c35f5SAlan Cox 
312*806c35f5SAlan Cox 	debugf0(__FILE__ ": %s()\n", __func__);
313*806c35f5SAlan Cox 
314*806c35f5SAlan Cox 	if ((mci = edac_mc_find_mci_by_pdev(pdev)) == NULL)
315*806c35f5SAlan Cox 		return;
316*806c35f5SAlan Cox 	if (edac_mc_del_mc(mci))
317*806c35f5SAlan Cox 		return;
318*806c35f5SAlan Cox 	pci_dev_put(mci->pdev);
319*806c35f5SAlan Cox 	edac_mc_free(mci);
320*806c35f5SAlan Cox }
321*806c35f5SAlan Cox 
322*806c35f5SAlan Cox 
323*806c35f5SAlan Cox static const struct pci_device_id amd76x_pci_tbl[] __devinitdata = {
324*806c35f5SAlan Cox 	{PCI_VEND_DEV(AMD, FE_GATE_700C), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
325*806c35f5SAlan Cox 	 AMD762},
326*806c35f5SAlan Cox 	{PCI_VEND_DEV(AMD, FE_GATE_700E), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
327*806c35f5SAlan Cox 	 AMD761},
328*806c35f5SAlan Cox 	{0,}			/* 0 terminated list. */
329*806c35f5SAlan Cox };
330*806c35f5SAlan Cox 
331*806c35f5SAlan Cox MODULE_DEVICE_TABLE(pci, amd76x_pci_tbl);
332*806c35f5SAlan Cox 
333*806c35f5SAlan Cox 
334*806c35f5SAlan Cox static struct pci_driver amd76x_driver = {
335*806c35f5SAlan Cox 	.name = BS_MOD_STR,
336*806c35f5SAlan Cox 	.probe = amd76x_init_one,
337*806c35f5SAlan Cox 	.remove = __devexit_p(amd76x_remove_one),
338*806c35f5SAlan Cox 	.id_table = amd76x_pci_tbl,
339*806c35f5SAlan Cox };
340*806c35f5SAlan Cox 
341*806c35f5SAlan Cox int __init amd76x_init(void)
342*806c35f5SAlan Cox {
343*806c35f5SAlan Cox 	return pci_register_driver(&amd76x_driver);
344*806c35f5SAlan Cox }
345*806c35f5SAlan Cox 
346*806c35f5SAlan Cox static void __exit amd76x_exit(void)
347*806c35f5SAlan Cox {
348*806c35f5SAlan Cox 	pci_unregister_driver(&amd76x_driver);
349*806c35f5SAlan Cox }
350*806c35f5SAlan Cox 
351*806c35f5SAlan Cox module_init(amd76x_init);
352*806c35f5SAlan Cox module_exit(amd76x_exit);
353*806c35f5SAlan Cox 
354*806c35f5SAlan Cox MODULE_LICENSE("GPL");
355*806c35f5SAlan Cox MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh");
356*806c35f5SAlan Cox MODULE_DESCRIPTION("MC support for AMD 76x memory controllers");
357