2 * AMD 76x Memory Controller kernel module
3 * (C) 2003 Linux Networx (http://lnxi.com)
4 * This file may be distributed under the terms of the
5 * GNU General Public License.
7 * Written by Thayne Harbaugh
8 * Based on work by Dan Hollis <goemon at anime dot net> and others.
9 * http://www.anime.net/~goemon/linux-ecc/
11 * $Id: edac_amd76x.c,v 1.4.2.5 2005/10/05 00:43:44 dsp_llnl Exp $
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <linux/pci_ids.h>
20 #include <linux/slab.h>
23 #define AMD76X_REVISION " Ver: 2.0.0 " __DATE__
26 #define amd76x_printk(level, fmt, arg...) \
27 edac_printk(level, "amd76x", fmt, ##arg)
29 #define amd76x_mc_printk(mci, level, fmt, arg...) \
30 edac_mc_chipset_printk(mci, level, "amd76x", fmt, ##arg)
32 #define AMD76X_NR_CSROWS 8
33 #define AMD76X_NR_CHANS 1
34 #define AMD76X_NR_DIMMS 4
36 /* AMD 76x register addresses - device 0 function 0 - PCI bridge */
38 #define AMD76X_ECC_MODE_STATUS 0x48 /* Mode and status of ECC (32b)
41 * 15:14 SERR enabled: x1=ue 1x=ce
43 * 12 diag: disabled, enabled
44 * 11:10 mode: dis, EC, ECC, ECC+scrub
45 * 9:8 status: x1=ue 1x=ce
50 #define AMD76X_DRAM_MODE_STATUS 0x58 /* DRAM Mode and status (32b)
52 * 31:26 clock disable 5 - 0
55 * 23 mode register service
56 * 22:21 suspend to RAM
57 * 20 burst refresh enable
60 * 17:16 cycles-per-refresh
62 * 7:0 x4 mode enable 7 - 0
65 #define AMD76X_MEM_BASE_ADDR 0xC0 /* Memory base address (8 x 32b)
67 * 31:23 chip-select base
69 * 15:7 chip-select mask
72 * 0 chip-select enable
75 struct amd76x_error_info {
84 struct amd76x_dev_info {
88 static const struct amd76x_dev_info amd76x_devs[] = {
98 * amd76x_get_error_info - fetch error information
99 * @mci: Memory controller
100 * @info: Info to fill in
102 * Fetch and store the AMD76x ECC status. Clear pending status
103 * on the chip so that further errors will be reported
105 static void amd76x_get_error_info(struct mem_ctl_info *mci,
106 struct amd76x_error_info *info)
108 struct pci_dev *pdev;
110 pdev = to_pci_dev(mci->dev);
111 pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS,
112 &info->ecc_mode_status);
114 if (info->ecc_mode_status & BIT(8))
115 pci_write_bits32(pdev, AMD76X_ECC_MODE_STATUS,
116 (u32) BIT(8), (u32) BIT(8));
118 if (info->ecc_mode_status & BIT(9))
119 pci_write_bits32(pdev, AMD76X_ECC_MODE_STATUS,
120 (u32) BIT(9), (u32) BIT(9));
124 * amd76x_process_error_info - Error check
125 * @mci: Memory controller
126 * @info: Previously fetched information from chip
127 * @handle_errors: 1 if we should do recovery
129 * Process the chip state and decide if an error has occurred.
130 * A return of 1 indicates an error. Also if handle_errors is true
131 * then attempt to handle and clean up after the error
133 static int amd76x_process_error_info(struct mem_ctl_info *mci,
134 struct amd76x_error_info *info, int handle_errors)
142 * Check for an uncorrectable error
144 if (info->ecc_mode_status & BIT(8)) {
148 row = (info->ecc_mode_status >> 4) & 0xf;
149 edac_mc_handle_ue(mci, mci->csrows[row].first_page, 0,
155 * Check for a correctable error
157 if (info->ecc_mode_status & BIT(9)) {
161 row = info->ecc_mode_status & 0xf;
162 edac_mc_handle_ce(mci, mci->csrows[row].first_page, 0,
163 0, row, 0, mci->ctl_name);
171 * amd76x_check - Poll the controller
172 * @mci: Memory controller
174 * Called by the poll handlers this function reads the status
175 * from the controller and checks for errors.
177 static void amd76x_check(struct mem_ctl_info *mci)
179 struct amd76x_error_info info;
180 debugf3("%s()\n", __func__);
181 amd76x_get_error_info(mci, &info);
182 amd76x_process_error_info(mci, &info, 1);
186 * amd76x_probe1 - Perform set up for detected device
187 * @pdev; PCI device detected
188 * @dev_idx: Device type index
190 * We have found an AMD76x and now need to set up the memory
191 * controller status reporting. We configure and set up the
192 * memory controller reporting and claim the device.
194 static int amd76x_probe1(struct pci_dev *pdev, int dev_idx)
198 struct mem_ctl_info *mci = NULL;
199 enum edac_type ems_modes[] = {
207 struct amd76x_error_info discard;
209 debugf0("%s()\n", __func__);
210 pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS, &ems);
211 ems_mode = (ems >> 10) & 0x3;
212 mci = edac_mc_alloc(0, AMD76X_NR_CSROWS, AMD76X_NR_CHANS);
219 debugf0("%s(): mci = %p\n", __func__, mci);
220 mci->dev = &pdev->dev;
221 mci->mtype_cap = MEM_FLAG_RDDR;
222 mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_EC | EDAC_FLAG_SECDED;
223 mci->edac_cap = ems_mode ?
224 (EDAC_FLAG_EC | EDAC_FLAG_SECDED) : EDAC_FLAG_NONE;
225 mci->mod_name = EDAC_MOD_STR;
226 mci->mod_ver = AMD76X_REVISION;
227 mci->ctl_name = amd76x_devs[dev_idx].ctl_name;
228 mci->edac_check = amd76x_check;
229 mci->ctl_page_to_phys = NULL;
231 for (index = 0; index < mci->nr_csrows; index++) {
232 struct csrow_info *csrow = &mci->csrows[index];
238 /* find the DRAM Chip Select Base address and mask */
239 pci_read_config_dword(pdev,
240 AMD76X_MEM_BASE_ADDR + (index * 4), &mba);
245 mba_base = mba & 0xff800000UL;
246 mba_mask = ((mba & 0xff80) << 16) | 0x7fffffUL;
247 pci_read_config_dword(pdev, AMD76X_DRAM_MODE_STATUS, &dms);
248 csrow->first_page = mba_base >> PAGE_SHIFT;
249 csrow->nr_pages = (mba_mask + 1) >> PAGE_SHIFT;
250 csrow->last_page = csrow->first_page + csrow->nr_pages - 1;
251 csrow->page_mask = mba_mask >> PAGE_SHIFT;
252 csrow->grain = csrow->nr_pages << PAGE_SHIFT;
253 csrow->mtype = MEM_RDDR;
254 csrow->dtype = ((dms >> index) & 0x1) ? DEV_X4 : DEV_UNKNOWN;
255 csrow->edac_mode = ems_modes[ems_mode];
258 amd76x_get_error_info(mci, &discard); /* clear counters */
260 /* Here we assume that we will never see multiple instances of this
261 * type of memory controller. The ID is therefore hardcoded to 0.
263 if (edac_mc_add_mc(mci,0)) {
264 debugf3("%s(): failed edac_mc_add_mc()\n", __func__);
268 /* get this far and it's successful */
269 debugf3("%s(): success\n", __func__);
278 /* returns count (>= 0), or negative on error */
279 static int __devinit amd76x_init_one(struct pci_dev *pdev,
280 const struct pci_device_id *ent)
282 debugf0("%s()\n", __func__);
284 /* don't need to call pci_device_enable() */
285 return amd76x_probe1(pdev, ent->driver_data);
289 * amd76x_remove_one - driver shutdown
290 * @pdev: PCI device being handed back
292 * Called when the driver is unloaded. Find the matching mci
293 * structure for the device then delete the mci and free the
296 static void __devexit amd76x_remove_one(struct pci_dev *pdev)
298 struct mem_ctl_info *mci;
300 debugf0("%s()\n", __func__);
302 if ((mci = edac_mc_del_mc(&pdev->dev)) == NULL)
308 static const struct pci_device_id amd76x_pci_tbl[] __devinitdata = {
310 PCI_VEND_DEV(AMD, FE_GATE_700C), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
314 PCI_VEND_DEV(AMD, FE_GATE_700E), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
319 } /* 0 terminated list. */
322 MODULE_DEVICE_TABLE(pci, amd76x_pci_tbl);
324 static struct pci_driver amd76x_driver = {
325 .name = EDAC_MOD_STR,
326 .probe = amd76x_init_one,
327 .remove = __devexit_p(amd76x_remove_one),
328 .id_table = amd76x_pci_tbl,
331 static int __init amd76x_init(void)
333 return pci_register_driver(&amd76x_driver);
336 static void __exit amd76x_exit(void)
338 pci_unregister_driver(&amd76x_driver);
341 module_init(amd76x_init);
342 module_exit(amd76x_exit);
344 MODULE_LICENSE("GPL");
345 MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh");
346 MODULE_DESCRIPTION("MC support for AMD 76x memory controllers");