blob: eaa225192098aabab4fba3774ff33df3cc8ac6eb [file] [log] [blame]
Alan Cox806c35f2006-01-18 17:44:08 -08001/*
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.
6 *
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/
10 *
11 * $Id: edac_amd76x.c,v 1.4.2.5 2005/10/05 00:43:44 dsp_llnl Exp $
12 *
13 */
14
Alan Cox806c35f2006-01-18 17:44:08 -080015#include <linux/module.h>
16#include <linux/init.h>
Alan Cox806c35f2006-01-18 17:44:08 -080017#include <linux/pci.h>
18#include <linux/pci_ids.h>
Alan Cox806c35f2006-01-18 17:44:08 -080019#include <linux/slab.h>
Douglas Thompson20bcb7a2007-07-19 01:49:47 -070020#include "edac_core.h"
Alan Cox806c35f2006-01-18 17:44:08 -080021
Douglas Thompson20bcb7a2007-07-19 01:49:47 -070022#define AMD76X_REVISION " Ver: 2.0.2 " __DATE__
Doug Thompson929a40e2006-07-01 04:35:45 -070023#define EDAC_MOD_STR "amd76x_edac"
Doug Thompson37f04582006-06-30 01:56:07 -070024
Dave Peterson537fba22006-03-26 01:38:40 -080025#define amd76x_printk(level, fmt, arg...) \
Dave Petersone7ecd892006-03-26 01:38:52 -080026 edac_printk(level, "amd76x", fmt, ##arg)
Dave Peterson537fba22006-03-26 01:38:40 -080027
28#define amd76x_mc_printk(mci, level, fmt, arg...) \
Dave Petersone7ecd892006-03-26 01:38:52 -080029 edac_mc_chipset_printk(mci, level, "amd76x", fmt, ##arg)
Dave Peterson537fba22006-03-26 01:38:40 -080030
Alan Cox806c35f2006-01-18 17:44:08 -080031#define AMD76X_NR_CSROWS 8
32#define AMD76X_NR_CHANS 1
33#define AMD76X_NR_DIMMS 4
34
Alan Cox806c35f2006-01-18 17:44:08 -080035/* AMD 76x register addresses - device 0 function 0 - PCI bridge */
Dave Petersone7ecd892006-03-26 01:38:52 -080036
Alan Cox806c35f2006-01-18 17:44:08 -080037#define AMD76X_ECC_MODE_STATUS 0x48 /* Mode and status of ECC (32b)
38 *
39 * 31:16 reserved
40 * 15:14 SERR enabled: x1=ue 1x=ce
41 * 13 reserved
42 * 12 diag: disabled, enabled
43 * 11:10 mode: dis, EC, ECC, ECC+scrub
44 * 9:8 status: x1=ue 1x=ce
45 * 7:4 UE cs row
46 * 3:0 CE cs row
47 */
Dave Petersone7ecd892006-03-26 01:38:52 -080048
Alan Cox806c35f2006-01-18 17:44:08 -080049#define AMD76X_DRAM_MODE_STATUS 0x58 /* DRAM Mode and status (32b)
50 *
51 * 31:26 clock disable 5 - 0
52 * 25 SDRAM init
53 * 24 reserved
54 * 23 mode register service
55 * 22:21 suspend to RAM
56 * 20 burst refresh enable
57 * 19 refresh disable
58 * 18 reserved
59 * 17:16 cycles-per-refresh
60 * 15:8 reserved
61 * 7:0 x4 mode enable 7 - 0
62 */
Dave Petersone7ecd892006-03-26 01:38:52 -080063
Alan Cox806c35f2006-01-18 17:44:08 -080064#define AMD76X_MEM_BASE_ADDR 0xC0 /* Memory base address (8 x 32b)
65 *
66 * 31:23 chip-select base
67 * 22:16 reserved
68 * 15:7 chip-select mask
69 * 6:3 reserved
70 * 2:1 address mode
71 * 0 chip-select enable
72 */
73
Alan Cox806c35f2006-01-18 17:44:08 -080074struct amd76x_error_info {
75 u32 ecc_mode_status;
76};
77
Alan Cox806c35f2006-01-18 17:44:08 -080078enum amd76x_chips {
79 AMD761 = 0,
80 AMD762
81};
82
Alan Cox806c35f2006-01-18 17:44:08 -080083struct amd76x_dev_info {
84 const char *ctl_name;
85};
86
Alan Cox806c35f2006-01-18 17:44:08 -080087static const struct amd76x_dev_info amd76x_devs[] = {
Dave Petersone7ecd892006-03-26 01:38:52 -080088 [AMD761] = {
Douglas Thompson67cb2b62007-07-19 01:50:02 -070089 .ctl_name = "AMD761"},
Dave Petersone7ecd892006-03-26 01:38:52 -080090 [AMD762] = {
Douglas Thompson67cb2b62007-07-19 01:50:02 -070091 .ctl_name = "AMD762"},
Alan Cox806c35f2006-01-18 17:44:08 -080092};
93
Alan Cox806c35f2006-01-18 17:44:08 -080094/**
95 * amd76x_get_error_info - fetch error information
96 * @mci: Memory controller
97 * @info: Info to fill in
98 *
99 * Fetch and store the AMD76x ECC status. Clear pending status
100 * on the chip so that further errors will be reported
101 */
Dave Petersone7ecd892006-03-26 01:38:52 -0800102static void amd76x_get_error_info(struct mem_ctl_info *mci,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700103 struct amd76x_error_info *info)
Alan Cox806c35f2006-01-18 17:44:08 -0800104{
Doug Thompson37f04582006-06-30 01:56:07 -0700105 struct pci_dev *pdev;
106
107 pdev = to_pci_dev(mci->dev);
108 pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700109 &info->ecc_mode_status);
Alan Cox806c35f2006-01-18 17:44:08 -0800110
111 if (info->ecc_mode_status & BIT(8))
Doug Thompson37f04582006-06-30 01:56:07 -0700112 pci_write_bits32(pdev, AMD76X_ECC_MODE_STATUS,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700113 (u32) BIT(8), (u32) BIT(8));
Alan Cox806c35f2006-01-18 17:44:08 -0800114
115 if (info->ecc_mode_status & BIT(9))
Doug Thompson37f04582006-06-30 01:56:07 -0700116 pci_write_bits32(pdev, AMD76X_ECC_MODE_STATUS,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700117 (u32) BIT(9), (u32) BIT(9));
Alan Cox806c35f2006-01-18 17:44:08 -0800118}
119
Alan Cox806c35f2006-01-18 17:44:08 -0800120/**
121 * amd76x_process_error_info - Error check
122 * @mci: Memory controller
123 * @info: Previously fetched information from chip
124 * @handle_errors: 1 if we should do recovery
125 *
126 * Process the chip state and decide if an error has occurred.
127 * A return of 1 indicates an error. Also if handle_errors is true
128 * then attempt to handle and clean up after the error
129 */
Dave Petersone7ecd892006-03-26 01:38:52 -0800130static int amd76x_process_error_info(struct mem_ctl_info *mci,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700131 struct amd76x_error_info *info,
132 int handle_errors)
Alan Cox806c35f2006-01-18 17:44:08 -0800133{
134 int error_found;
135 u32 row;
136
137 error_found = 0;
138
139 /*
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700140 * Check for an uncorrectable error
Alan Cox806c35f2006-01-18 17:44:08 -0800141 */
142 if (info->ecc_mode_status & BIT(8)) {
143 error_found = 1;
144
145 if (handle_errors) {
146 row = (info->ecc_mode_status >> 4) & 0xf;
Dave Petersone7ecd892006-03-26 01:38:52 -0800147 edac_mc_handle_ue(mci, mci->csrows[row].first_page, 0,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700148 row, mci->ctl_name);
Alan Cox806c35f2006-01-18 17:44:08 -0800149 }
150 }
151
152 /*
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700153 * Check for a correctable error
Alan Cox806c35f2006-01-18 17:44:08 -0800154 */
155 if (info->ecc_mode_status & BIT(9)) {
156 error_found = 1;
157
158 if (handle_errors) {
159 row = info->ecc_mode_status & 0xf;
Dave Petersone7ecd892006-03-26 01:38:52 -0800160 edac_mc_handle_ce(mci, mci->csrows[row].first_page, 0,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700161 0, row, 0, mci->ctl_name);
Alan Cox806c35f2006-01-18 17:44:08 -0800162 }
163 }
Dave Petersone7ecd892006-03-26 01:38:52 -0800164
Alan Cox806c35f2006-01-18 17:44:08 -0800165 return error_found;
166}
167
168/**
169 * amd76x_check - Poll the controller
170 * @mci: Memory controller
171 *
172 * Called by the poll handlers this function reads the status
173 * from the controller and checks for errors.
174 */
Alan Cox806c35f2006-01-18 17:44:08 -0800175static void amd76x_check(struct mem_ctl_info *mci)
176{
177 struct amd76x_error_info info;
Dave Peterson537fba22006-03-26 01:38:40 -0800178 debugf3("%s()\n", __func__);
Alan Cox806c35f2006-01-18 17:44:08 -0800179 amd76x_get_error_info(mci, &info);
180 amd76x_process_error_info(mci, &info, 1);
181}
182
Doug Thompson13189522006-06-30 01:56:08 -0700183static void amd76x_init_csrows(struct mem_ctl_info *mci, struct pci_dev *pdev,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700184 enum edac_type edac_mode)
Alan Cox806c35f2006-01-18 17:44:08 -0800185{
Doug Thompson13189522006-06-30 01:56:08 -0700186 struct csrow_info *csrow;
187 u32 mba, mba_base, mba_mask, dms;
Alan Cox806c35f2006-01-18 17:44:08 -0800188 int index;
Alan Cox806c35f2006-01-18 17:44:08 -0800189
190 for (index = 0; index < mci->nr_csrows; index++) {
Doug Thompson13189522006-06-30 01:56:08 -0700191 csrow = &mci->csrows[index];
Alan Cox806c35f2006-01-18 17:44:08 -0800192
193 /* find the DRAM Chip Select Base address and mask */
Doug Thompson37f04582006-06-30 01:56:07 -0700194 pci_read_config_dword(pdev,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700195 AMD76X_MEM_BASE_ADDR + (index * 4), &mba);
Alan Cox806c35f2006-01-18 17:44:08 -0800196
197 if (!(mba & BIT(0)))
198 continue;
199
200 mba_base = mba & 0xff800000UL;
201 mba_mask = ((mba & 0xff80) << 16) | 0x7fffffUL;
Doug Thompson37f04582006-06-30 01:56:07 -0700202 pci_read_config_dword(pdev, AMD76X_DRAM_MODE_STATUS, &dms);
Alan Cox806c35f2006-01-18 17:44:08 -0800203 csrow->first_page = mba_base >> PAGE_SHIFT;
204 csrow->nr_pages = (mba_mask + 1) >> PAGE_SHIFT;
205 csrow->last_page = csrow->first_page + csrow->nr_pages - 1;
206 csrow->page_mask = mba_mask >> PAGE_SHIFT;
207 csrow->grain = csrow->nr_pages << PAGE_SHIFT;
208 csrow->mtype = MEM_RDDR;
209 csrow->dtype = ((dms >> index) & 0x1) ? DEV_X4 : DEV_UNKNOWN;
Doug Thompson13189522006-06-30 01:56:08 -0700210 csrow->edac_mode = edac_mode;
211 }
212}
213
214/**
215 * amd76x_probe1 - Perform set up for detected device
216 * @pdev; PCI device detected
217 * @dev_idx: Device type index
218 *
219 * We have found an AMD76x and now need to set up the memory
220 * controller status reporting. We configure and set up the
221 * memory controller reporting and claim the device.
222 */
223static int amd76x_probe1(struct pci_dev *pdev, int dev_idx)
224{
225 static const enum edac_type ems_modes[] = {
226 EDAC_NONE,
227 EDAC_EC,
228 EDAC_SECDED,
229 EDAC_SECDED
230 };
231 struct mem_ctl_info *mci = NULL;
232 u32 ems;
233 u32 ems_mode;
234 struct amd76x_error_info discard;
235
236 debugf0("%s()\n", __func__);
237 pci_read_config_dword(pdev, AMD76X_ECC_MODE_STATUS, &ems);
238 ems_mode = (ems >> 10) & 0x3;
239 mci = edac_mc_alloc(0, AMD76X_NR_CSROWS, AMD76X_NR_CHANS);
240
241 if (mci == NULL) {
242 return -ENOMEM;
Alan Cox806c35f2006-01-18 17:44:08 -0800243 }
244
Doug Thompson13189522006-06-30 01:56:08 -0700245 debugf0("%s(): mci = %p\n", __func__, mci);
246 mci->dev = &pdev->dev;
247 mci->mtype_cap = MEM_FLAG_RDDR;
248 mci->edac_ctl_cap = EDAC_FLAG_NONE | EDAC_FLAG_EC | EDAC_FLAG_SECDED;
249 mci->edac_cap = ems_mode ?
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700250 (EDAC_FLAG_EC | EDAC_FLAG_SECDED) : EDAC_FLAG_NONE;
Doug Thompson13189522006-06-30 01:56:08 -0700251 mci->mod_name = EDAC_MOD_STR;
252 mci->mod_ver = AMD76X_REVISION;
253 mci->ctl_name = amd76x_devs[dev_idx].ctl_name;
Dave Jiangc4192702007-07-19 01:49:47 -0700254 mci->dev_name = pci_name(pdev);
Doug Thompson13189522006-06-30 01:56:08 -0700255 mci->edac_check = amd76x_check;
256 mci->ctl_page_to_phys = NULL;
257
258 amd76x_init_csrows(mci, pdev, ems_modes[ems_mode]);
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700259 amd76x_get_error_info(mci, &discard); /* clear counters */
Alan Cox806c35f2006-01-18 17:44:08 -0800260
Doug Thompson2d7bbb92006-06-30 01:56:08 -0700261 /* Here we assume that we will never see multiple instances of this
262 * type of memory controller. The ID is therefore hardcoded to 0.
263 */
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700264 if (edac_mc_add_mc(mci, 0)) {
Dave Peterson537fba22006-03-26 01:38:40 -0800265 debugf3("%s(): failed edac_mc_add_mc()\n", __func__);
Alan Cox806c35f2006-01-18 17:44:08 -0800266 goto fail;
267 }
268
269 /* get this far and it's successful */
Dave Peterson537fba22006-03-26 01:38:40 -0800270 debugf3("%s(): success\n", __func__);
Alan Cox806c35f2006-01-18 17:44:08 -0800271 return 0;
272
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700273 fail:
Doug Thompson13189522006-06-30 01:56:08 -0700274 edac_mc_free(mci);
275 return -ENODEV;
Alan Cox806c35f2006-01-18 17:44:08 -0800276}
277
278/* returns count (>= 0), or negative on error */
279static int __devinit amd76x_init_one(struct pci_dev *pdev,
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700280 const struct pci_device_id *ent)
Alan Cox806c35f2006-01-18 17:44:08 -0800281{
Dave Peterson537fba22006-03-26 01:38:40 -0800282 debugf0("%s()\n", __func__);
Alan Cox806c35f2006-01-18 17:44:08 -0800283
284 /* don't need to call pci_device_enable() */
285 return amd76x_probe1(pdev, ent->driver_data);
286}
287
Alan Cox806c35f2006-01-18 17:44:08 -0800288/**
289 * amd76x_remove_one - driver shutdown
290 * @pdev: PCI device being handed back
291 *
292 * Called when the driver is unloaded. Find the matching mci
293 * structure for the device then delete the mci and free the
294 * resources.
295 */
Alan Cox806c35f2006-01-18 17:44:08 -0800296static void __devexit amd76x_remove_one(struct pci_dev *pdev)
297{
298 struct mem_ctl_info *mci;
299
Dave Peterson537fba22006-03-26 01:38:40 -0800300 debugf0("%s()\n", __func__);
Alan Cox806c35f2006-01-18 17:44:08 -0800301
Doug Thompson37f04582006-06-30 01:56:07 -0700302 if ((mci = edac_mc_del_mc(&pdev->dev)) == NULL)
Alan Cox806c35f2006-01-18 17:44:08 -0800303 return;
Dave Peterson18dbc332006-03-26 01:38:50 -0800304
Alan Cox806c35f2006-01-18 17:44:08 -0800305 edac_mc_free(mci);
306}
307
Alan Cox806c35f2006-01-18 17:44:08 -0800308static const struct pci_device_id amd76x_pci_tbl[] __devinitdata = {
Dave Petersone7ecd892006-03-26 01:38:52 -0800309 {
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700310 PCI_VEND_DEV(AMD, FE_GATE_700C), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
311 AMD762},
Dave Petersone7ecd892006-03-26 01:38:52 -0800312 {
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700313 PCI_VEND_DEV(AMD, FE_GATE_700E), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
314 AMD761},
Dave Petersone7ecd892006-03-26 01:38:52 -0800315 {
Douglas Thompson67cb2b62007-07-19 01:50:02 -0700316 0,
317 } /* 0 terminated list. */
Alan Cox806c35f2006-01-18 17:44:08 -0800318};
319
320MODULE_DEVICE_TABLE(pci, amd76x_pci_tbl);
321
Alan Cox806c35f2006-01-18 17:44:08 -0800322static struct pci_driver amd76x_driver = {
Dave Peterson680cbbb2006-03-26 01:38:41 -0800323 .name = EDAC_MOD_STR,
Alan Cox806c35f2006-01-18 17:44:08 -0800324 .probe = amd76x_init_one,
325 .remove = __devexit_p(amd76x_remove_one),
326 .id_table = amd76x_pci_tbl,
327};
328
Alan Coxda9bb1d2006-01-18 17:44:13 -0800329static int __init amd76x_init(void)
Alan Cox806c35f2006-01-18 17:44:08 -0800330{
331 return pci_register_driver(&amd76x_driver);
332}
333
334static void __exit amd76x_exit(void)
335{
336 pci_unregister_driver(&amd76x_driver);
337}
338
339module_init(amd76x_init);
340module_exit(amd76x_exit);
341
342MODULE_LICENSE("GPL");
343MODULE_AUTHOR("Linux Networx (http://lnxi.com) Thayne Harbaugh");
344MODULE_DESCRIPTION("MC support for AMD 76x memory controllers");