xref: /openbmc/linux/drivers/edac/amd76x_edac.c (revision 18dbc337af5d6efd30cb9291e74722c8ad134fd3)
1806c35f5SAlan Cox /*
2806c35f5SAlan Cox  * AMD 76x Memory Controller kernel module
3806c35f5SAlan Cox  * (C) 2003 Linux Networx (http://lnxi.com)
4806c35f5SAlan Cox  * This file may be distributed under the terms of the
5806c35f5SAlan Cox  * GNU General Public License.
6806c35f5SAlan Cox  *
7806c35f5SAlan Cox  * Written by Thayne Harbaugh
8806c35f5SAlan Cox  * Based on work by Dan Hollis <goemon at anime dot net> and others.
9806c35f5SAlan Cox  *	http://www.anime.net/~goemon/linux-ecc/
10806c35f5SAlan Cox  *
11806c35f5SAlan Cox  * $Id: edac_amd76x.c,v 1.4.2.5 2005/10/05 00:43:44 dsp_llnl Exp $
12806c35f5SAlan Cox  *
13806c35f5SAlan Cox  */
14806c35f5SAlan Cox 
15806c35f5SAlan Cox 
16806c35f5SAlan Cox #include <linux/config.h>
17806c35f5SAlan Cox #include <linux/module.h>
18806c35f5SAlan Cox #include <linux/init.h>
19806c35f5SAlan Cox 
20806c35f5SAlan Cox #include <linux/pci.h>
21806c35f5SAlan Cox #include <linux/pci_ids.h>
22806c35f5SAlan Cox 
23806c35f5SAlan Cox #include <linux/slab.h>
24806c35f5SAlan Cox 
25806c35f5SAlan Cox #include "edac_mc.h"
26806c35f5SAlan Cox 
27806c35f5SAlan Cox 
28537fba28SDave Peterson #define amd76x_printk(level, fmt, arg...) \
29537fba28SDave Peterson     edac_printk(level, "amd76x", fmt, ##arg)
30537fba28SDave Peterson 
31537fba28SDave Peterson 
32537fba28SDave Peterson #define amd76x_mc_printk(mci, level, fmt, arg...) \
33537fba28SDave Peterson     edac_mc_chipset_printk(mci, level, "amd76x", fmt, ##arg)
34537fba28SDave Peterson 
35537fba28SDave Peterson 
36806c35f5SAlan Cox #define AMD76X_NR_CSROWS 8
37806c35f5SAlan Cox #define AMD76X_NR_CHANS  1
38806c35f5SAlan Cox #define AMD76X_NR_DIMMS  4
39806c35f5SAlan Cox 
40806c35f5SAlan Cox 
41806c35f5SAlan Cox /* AMD 76x register addresses - device 0 function 0 - PCI bridge */
42806c35f5SAlan Cox #define AMD76X_ECC_MODE_STATUS	0x48	/* Mode and status of ECC (32b)
43806c35f5SAlan Cox 					 *
44806c35f5SAlan Cox 					 * 31:16 reserved
45806c35f5SAlan Cox 					 * 15:14 SERR enabled: x1=ue 1x=ce
46806c35f5SAlan Cox 					 * 13    reserved
47806c35f5SAlan Cox 					 * 12    diag: disabled, enabled
48806c35f5SAlan Cox 					 * 11:10 mode: dis, EC, ECC, ECC+scrub
49806c35f5SAlan Cox 					 *  9:8  status: x1=ue 1x=ce
50806c35f5SAlan Cox 					 *  7:4  UE cs row
51806c35f5SAlan Cox 					 *  3:0  CE cs row
52806c35f5SAlan Cox 					 */
53806c35f5SAlan Cox #define AMD76X_DRAM_MODE_STATUS	0x58	/* DRAM Mode and status (32b)
54806c35f5SAlan Cox 					 *
55806c35f5SAlan Cox 					 * 31:26 clock disable 5 - 0
56806c35f5SAlan Cox 					 * 25    SDRAM init
57806c35f5SAlan Cox 					 * 24    reserved
58806c35f5SAlan Cox 					 * 23    mode register service
59806c35f5SAlan Cox 					 * 22:21 suspend to RAM
60806c35f5SAlan Cox 					 * 20    burst refresh enable
61806c35f5SAlan Cox 					 * 19    refresh disable
62806c35f5SAlan Cox 					 * 18    reserved
63806c35f5SAlan Cox 					 * 17:16 cycles-per-refresh
64806c35f5SAlan Cox 					 * 15:8  reserved
65806c35f5SAlan Cox 					 *  7:0  x4 mode enable 7 - 0
66806c35f5SAlan Cox 					 */
67806c35f5SAlan Cox #define AMD76X_MEM_BASE_ADDR	0xC0	/* Memory base address (8 x 32b)
68806c35f5SAlan Cox 					 *
69806c35f5SAlan Cox 					 * 31:23 chip-select base
70806c35f5SAlan Cox 					 * 22:16 reserved
71806c35f5SAlan Cox 					 * 15:7  chip-select mask
72806c35f5SAlan Cox 					 *  6:3  reserved
73806c35f5SAlan Cox 					 *  2:1  address mode
74806c35f5SAlan Cox 					 *  0    chip-select enable
75806c35f5SAlan Cox 					 */
76806c35f5SAlan Cox 
77806c35f5SAlan Cox 
78806c35f5SAlan Cox struct amd76x_error_info {
79806c35f5SAlan Cox 	u32 ecc_mode_status;
80806c35f5SAlan Cox };
81806c35f5SAlan Cox 
82806c35f5SAlan Cox 
83806c35f5SAlan Cox enum amd76x_chips {
84806c35f5SAlan Cox 	AMD761 = 0,
85806c35f5SAlan Cox 	AMD762
86806c35f5SAlan Cox };
87806c35f5SAlan Cox 
88806c35f5SAlan Cox 
89806c35f5SAlan Cox struct amd76x_dev_info {
90806c35f5SAlan Cox 	const char *ctl_name;
91806c35f5SAlan Cox };
92806c35f5SAlan Cox 
93806c35f5SAlan Cox 
94806c35f5SAlan Cox static const struct amd76x_dev_info amd76x_devs[] = {
95806c35f5SAlan Cox 	[AMD761] = {.ctl_name = "AMD761"},
96806c35f5SAlan Cox 	[AMD762] = {.ctl_name = "AMD762"},
97806c35f5SAlan Cox };
98806c35f5SAlan Cox 
99806c35f5SAlan Cox 
100806c35f5SAlan Cox /**
101806c35f5SAlan Cox  *	amd76x_get_error_info	-	fetch error information
102806c35f5SAlan Cox  *	@mci: Memory controller
103806c35f5SAlan Cox  *	@info: Info to fill in
104806c35f5SAlan Cox  *
105806c35f5SAlan Cox  *	Fetch and store the AMD76x ECC status. Clear pending status
106806c35f5SAlan Cox  *	on the chip so that further errors will be reported
107806c35f5SAlan Cox  */
108806c35f5SAlan Cox 
109806c35f5SAlan Cox static void amd76x_get_error_info (struct mem_ctl_info *mci,
110806c35f5SAlan Cox 				   struct amd76x_error_info *info)
111806c35f5SAlan Cox {
112806c35f5SAlan Cox 	pci_read_config_dword(mci->pdev, AMD76X_ECC_MODE_STATUS,
113806c35f5SAlan Cox 				&info->ecc_mode_status);
114806c35f5SAlan Cox 
115806c35f5SAlan Cox 	if (info->ecc_mode_status & BIT(8))
116806c35f5SAlan Cox 		pci_write_bits32(mci->pdev, AMD76X_ECC_MODE_STATUS,
117806c35f5SAlan Cox 				   (u32) BIT(8), (u32) BIT(8));
118806c35f5SAlan Cox 
119806c35f5SAlan Cox 	if (info->ecc_mode_status & BIT(9))
120806c35f5SAlan Cox 		pci_write_bits32(mci->pdev, AMD76X_ECC_MODE_STATUS,
121806c35f5SAlan Cox 				   (u32) BIT(9), (u32) BIT(9));
122806c35f5SAlan Cox }
123806c35f5SAlan Cox 
124806c35f5SAlan Cox 
125806c35f5SAlan Cox /**
126806c35f5SAlan Cox  *	amd76x_process_error_info	-	Error check
127806c35f5SAlan Cox  *	@mci: Memory controller
128806c35f5SAlan Cox  *	@info: Previously fetched information from chip
129806c35f5SAlan Cox  *	@handle_errors: 1 if we should do recovery
130806c35f5SAlan Cox  *
131806c35f5SAlan Cox  *	Process the chip state and decide if an error has occurred.
132806c35f5SAlan Cox  *	A return of 1 indicates an error. Also if handle_errors is true
133806c35f5SAlan Cox  *	then attempt to handle and clean up after the error
134806c35f5SAlan Cox  */
135806c35f5SAlan Cox 
136806c35f5SAlan Cox static int amd76x_process_error_info (struct mem_ctl_info *mci,
137806c35f5SAlan Cox 		struct amd76x_error_info *info, int handle_errors)
138806c35f5SAlan Cox {
139806c35f5SAlan Cox 	int error_found;
140806c35f5SAlan Cox 	u32 row;
141806c35f5SAlan Cox 
142806c35f5SAlan Cox 	error_found = 0;
143806c35f5SAlan Cox 
144806c35f5SAlan Cox 	/*
145806c35f5SAlan Cox 	 *	Check for an uncorrectable error
146806c35f5SAlan Cox 	 */
147806c35f5SAlan Cox 	if (info->ecc_mode_status & BIT(8)) {
148806c35f5SAlan Cox 		error_found = 1;
149806c35f5SAlan Cox 
150806c35f5SAlan Cox 		if (handle_errors) {
151806c35f5SAlan Cox 			row = (info->ecc_mode_status >> 4) & 0xf;
152806c35f5SAlan Cox 			edac_mc_handle_ue(mci,
153806c35f5SAlan Cox 			    mci->csrows[row].first_page, 0, row,
154806c35f5SAlan Cox 			    mci->ctl_name);
155806c35f5SAlan Cox 		}
156806c35f5SAlan Cox 	}
157806c35f5SAlan Cox 
158806c35f5SAlan Cox 	/*
159806c35f5SAlan Cox 	 *	Check for a correctable error
160806c35f5SAlan Cox 	 */
161806c35f5SAlan Cox 	if (info->ecc_mode_status & BIT(9)) {
162806c35f5SAlan Cox 		error_found = 1;
163806c35f5SAlan Cox 
164806c35f5SAlan Cox 		if (handle_errors) {
165806c35f5SAlan Cox 			row = info->ecc_mode_status & 0xf;
166806c35f5SAlan Cox 			edac_mc_handle_ce(mci,
167806c35f5SAlan Cox 			    mci->csrows[row].first_page, 0, 0, row, 0,
168806c35f5SAlan Cox 			    mci->ctl_name);
169806c35f5SAlan Cox 		}
170806c35f5SAlan Cox 	}
171806c35f5SAlan Cox 	return error_found;
172806c35f5SAlan Cox }
173806c35f5SAlan Cox 
174806c35f5SAlan Cox /**
175806c35f5SAlan Cox  *	amd76x_check	-	Poll the controller
176806c35f5SAlan Cox  *	@mci: Memory controller
177806c35f5SAlan Cox  *
178806c35f5SAlan Cox  *	Called by the poll handlers this function reads the status
179806c35f5SAlan Cox  *	from the controller and checks for errors.
180806c35f5SAlan Cox  */
181806c35f5SAlan Cox 
182806c35f5SAlan Cox static void amd76x_check(struct mem_ctl_info *mci)
183806c35f5SAlan Cox {
184806c35f5SAlan Cox 	struct amd76x_error_info info;
185537fba28SDave Peterson 	debugf3("%s()\n", __func__);
186806c35f5SAlan Cox 	amd76x_get_error_info(mci, &info);
187806c35f5SAlan Cox 	amd76x_process_error_info(mci, &info, 1);
188806c35f5SAlan Cox }
189806c35f5SAlan Cox 
190806c35f5SAlan Cox 
191806c35f5SAlan Cox /**
192806c35f5SAlan Cox  *	amd76x_probe1	-	Perform set up for detected device
193806c35f5SAlan Cox  *	@pdev; PCI device detected
194806c35f5SAlan Cox  *	@dev_idx: Device type index
195806c35f5SAlan Cox  *
196806c35f5SAlan Cox  *	We have found an AMD76x and now need to set up the memory
197806c35f5SAlan Cox  *	controller status reporting. We configure and set up the
198806c35f5SAlan Cox  *	memory controller reporting and claim the device.
199806c35f5SAlan Cox  */
200806c35f5SAlan Cox 
201806c35f5SAlan Cox static int amd76x_probe1(struct pci_dev *pdev, int dev_idx)
202806c35f5SAlan Cox {
203806c35f5SAlan Cox 	int rc = -ENODEV;
204806c35f5SAlan Cox 	int index;
205806c35f5SAlan Cox 	struct mem_ctl_info *mci = NULL;
206806c35f5SAlan Cox 	enum edac_type ems_modes[] = {
207806c35f5SAlan Cox 		EDAC_NONE,
208806c35f5SAlan Cox 		EDAC_EC,
209806c35f5SAlan Cox 		EDAC_SECDED,
210806c35f5SAlan Cox 		EDAC_SECDED
211806c35f5SAlan Cox 	};
212806c35f5SAlan Cox 	u32 ems;
213806c35f5SAlan Cox 	u32 ems_mode;
214749ede57SDave Peterson 	struct amd76x_error_info discard;
215806c35f5SAlan Cox 
216537fba28SDave Peterson 	debugf0("%s()\n", __func__);
217806c35f5SAlan Cox 
218806c35f5SAlan Cox 	pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS, &ems);
219806c35f5SAlan Cox 	ems_mode = (ems >> 10) & 0x3;
220806c35f5SAlan Cox 
221806c35f5SAlan Cox 	mci = edac_mc_alloc(0, AMD76X_NR_CSROWS, AMD76X_NR_CHANS);
222806c35f5SAlan Cox 
223806c35f5SAlan Cox 	if (mci == NULL) {
224806c35f5SAlan Cox 		rc = -ENOMEM;
225806c35f5SAlan Cox 		goto fail;
226806c35f5SAlan Cox 	}
227806c35f5SAlan Cox 
228537fba28SDave Peterson 	debugf0("%s(): mci = %p\n", __func__, mci);
229806c35f5SAlan Cox 
230225159bdSDave Peterson 	mci->pdev = pdev;
231806c35f5SAlan Cox 	mci->mtype_cap = MEM_FLAG_RDDR;
232806c35f5SAlan Cox 
233806c35f5SAlan Cox 	mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_EC | EDAC_FLAG_SECDED;
234806c35f5SAlan Cox 	mci->edac_cap = ems_mode ?
235806c35f5SAlan Cox 	    (EDAC_FLAG_EC | EDAC_FLAG_SECDED) : EDAC_FLAG_NONE;
236806c35f5SAlan Cox 
237680cbbbbSDave Peterson 	mci->mod_name = EDAC_MOD_STR;
238806c35f5SAlan Cox 	mci->mod_ver = "$Revision: 1.4.2.5 $";
239806c35f5SAlan Cox 	mci->ctl_name = amd76x_devs[dev_idx].ctl_name;
240806c35f5SAlan Cox 	mci->edac_check = amd76x_check;
241806c35f5SAlan Cox 	mci->ctl_page_to_phys = NULL;
242806c35f5SAlan Cox 
243806c35f5SAlan Cox 	for (index = 0; index < mci->nr_csrows; index++) {
244806c35f5SAlan Cox 		struct csrow_info *csrow = &mci->csrows[index];
245806c35f5SAlan Cox 		u32 mba;
246806c35f5SAlan Cox 		u32 mba_base;
247806c35f5SAlan Cox 		u32 mba_mask;
248806c35f5SAlan Cox 		u32 dms;
249806c35f5SAlan Cox 
250806c35f5SAlan Cox 		/* find the DRAM Chip Select Base address and mask */
251806c35f5SAlan Cox 		pci_read_config_dword(mci->pdev,
252806c35f5SAlan Cox 				      AMD76X_MEM_BASE_ADDR + (index * 4),
253806c35f5SAlan Cox 				      &mba);
254806c35f5SAlan Cox 
255806c35f5SAlan Cox 		if (!(mba & BIT(0)))
256806c35f5SAlan Cox 			continue;
257806c35f5SAlan Cox 
258806c35f5SAlan Cox 		mba_base = mba & 0xff800000UL;
259806c35f5SAlan Cox 		mba_mask = ((mba & 0xff80) << 16) | 0x7fffffUL;
260806c35f5SAlan Cox 
261806c35f5SAlan Cox 		pci_read_config_dword(mci->pdev, AMD76X_DRAM_MODE_STATUS,
262806c35f5SAlan Cox 				      &dms);
263806c35f5SAlan Cox 
264806c35f5SAlan Cox 		csrow->first_page = mba_base >> PAGE_SHIFT;
265806c35f5SAlan Cox 		csrow->nr_pages = (mba_mask + 1) >> PAGE_SHIFT;
266806c35f5SAlan Cox 		csrow->last_page = csrow->first_page + csrow->nr_pages - 1;
267806c35f5SAlan Cox 		csrow->page_mask = mba_mask >> PAGE_SHIFT;
268806c35f5SAlan Cox 		csrow->grain = csrow->nr_pages << PAGE_SHIFT;
269806c35f5SAlan Cox 		csrow->mtype = MEM_RDDR;
270806c35f5SAlan Cox 		csrow->dtype = ((dms >> index) & 0x1) ? DEV_X4 : DEV_UNKNOWN;
271806c35f5SAlan Cox 		csrow->edac_mode = ems_modes[ems_mode];
272806c35f5SAlan Cox 	}
273806c35f5SAlan Cox 
274749ede57SDave Peterson 	amd76x_get_error_info(mci, &discard);  /* clear counters */
275806c35f5SAlan Cox 
276806c35f5SAlan Cox 	if (edac_mc_add_mc(mci)) {
277537fba28SDave Peterson 		debugf3("%s(): failed edac_mc_add_mc()\n", __func__);
278806c35f5SAlan Cox 		goto fail;
279806c35f5SAlan Cox 	}
280806c35f5SAlan Cox 
281806c35f5SAlan Cox 	/* get this far and it's successful */
282537fba28SDave Peterson 	debugf3("%s(): success\n", __func__);
283806c35f5SAlan Cox 	return 0;
284806c35f5SAlan Cox 
285806c35f5SAlan Cox fail:
286225159bdSDave Peterson 	if (mci != NULL)
287806c35f5SAlan Cox 		edac_mc_free(mci);
288806c35f5SAlan Cox 	return rc;
289806c35f5SAlan Cox }
290806c35f5SAlan Cox 
291806c35f5SAlan Cox /* returns count (>= 0), or negative on error */
292806c35f5SAlan Cox static int __devinit amd76x_init_one(struct pci_dev *pdev,
293806c35f5SAlan Cox 				     const struct pci_device_id *ent)
294806c35f5SAlan Cox {
295537fba28SDave Peterson 	debugf0("%s()\n", __func__);
296806c35f5SAlan Cox 
297806c35f5SAlan Cox 	/* don't need to call pci_device_enable() */
298806c35f5SAlan Cox 	return amd76x_probe1(pdev, ent->driver_data);
299806c35f5SAlan Cox }
300806c35f5SAlan Cox 
301806c35f5SAlan Cox 
302806c35f5SAlan Cox /**
303806c35f5SAlan Cox  *	amd76x_remove_one	-	driver shutdown
304806c35f5SAlan Cox  *	@pdev: PCI device being handed back
305806c35f5SAlan Cox  *
306806c35f5SAlan Cox  *	Called when the driver is unloaded. Find the matching mci
307806c35f5SAlan Cox  *	structure for the device then delete the mci and free the
308806c35f5SAlan Cox  *	resources.
309806c35f5SAlan Cox  */
310806c35f5SAlan Cox 
311806c35f5SAlan Cox static void __devexit amd76x_remove_one(struct pci_dev *pdev)
312806c35f5SAlan Cox {
313806c35f5SAlan Cox 	struct mem_ctl_info *mci;
314806c35f5SAlan Cox 
315537fba28SDave Peterson 	debugf0("%s()\n", __func__);
316806c35f5SAlan Cox 
317*18dbc337SDave Peterson 	if ((mci = edac_mc_del_mc(pdev)) == NULL)
318806c35f5SAlan Cox 		return;
319*18dbc337SDave Peterson 
320806c35f5SAlan Cox 	edac_mc_free(mci);
321806c35f5SAlan Cox }
322806c35f5SAlan Cox 
323806c35f5SAlan Cox 
324806c35f5SAlan Cox static const struct pci_device_id amd76x_pci_tbl[] __devinitdata = {
325806c35f5SAlan Cox 	{PCI_VEND_DEV(AMD, FE_GATE_700C), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
326806c35f5SAlan Cox 	 AMD762},
327806c35f5SAlan Cox 	{PCI_VEND_DEV(AMD, FE_GATE_700E), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
328806c35f5SAlan Cox 	 AMD761},
329806c35f5SAlan Cox 	{0,}			/* 0 terminated list. */
330806c35f5SAlan Cox };
331806c35f5SAlan Cox 
332806c35f5SAlan Cox MODULE_DEVICE_TABLE(pci, amd76x_pci_tbl);
333806c35f5SAlan Cox 
334806c35f5SAlan Cox 
335806c35f5SAlan Cox static struct pci_driver amd76x_driver = {
336680cbbbbSDave Peterson 	.name = EDAC_MOD_STR,
337806c35f5SAlan Cox 	.probe = amd76x_init_one,
338806c35f5SAlan Cox 	.remove = __devexit_p(amd76x_remove_one),
339806c35f5SAlan Cox 	.id_table = amd76x_pci_tbl,
340806c35f5SAlan Cox };
341806c35f5SAlan Cox 
342da9bb1d2SAlan Cox static int __init amd76x_init(void)
343806c35f5SAlan Cox {
344806c35f5SAlan Cox 	return pci_register_driver(&amd76x_driver);
345806c35f5SAlan Cox }
346806c35f5SAlan Cox 
347806c35f5SAlan Cox static void __exit amd76x_exit(void)
348806c35f5SAlan Cox {
349806c35f5SAlan Cox 	pci_unregister_driver(&amd76x_driver);
350806c35f5SAlan Cox }
351806c35f5SAlan Cox 
352806c35f5SAlan Cox module_init(amd76x_init);
353806c35f5SAlan Cox module_exit(amd76x_exit);
354806c35f5SAlan Cox 
355806c35f5SAlan Cox MODULE_LICENSE("GPL");
356806c35f5SAlan Cox MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh");
357806c35f5SAlan Cox MODULE_DESCRIPTION("MC support for AMD 76x memory controllers");
358