18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Intel 3000/3010 Memory Controller kernel module
38c2ecf20Sopenharmony_ci * Copyright (C) 2007 Akamai Technologies, Inc.
48c2ecf20Sopenharmony_ci * Shamelessly copied from:
58c2ecf20Sopenharmony_ci * 	Intel D82875P Memory Controller kernel module
68c2ecf20Sopenharmony_ci * 	(C) 2003 Linux Networx (http://lnxi.com)
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This file may be distributed under the terms of the
98c2ecf20Sopenharmony_ci * GNU General Public License.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci#include <linux/pci_ids.h>
168c2ecf20Sopenharmony_ci#include <linux/edac.h>
178c2ecf20Sopenharmony_ci#include "edac_module.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define EDAC_MOD_STR		"i3000_edac"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define I3000_RANKS		8
228c2ecf20Sopenharmony_ci#define I3000_RANKS_PER_CHANNEL	4
238c2ecf20Sopenharmony_ci#define I3000_CHANNELS		2
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Intel 3000 register addresses - device 0 function 0 - DRAM Controller */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define I3000_MCHBAR		0x44	/* MCH Memory Mapped Register BAR */
288c2ecf20Sopenharmony_ci#define I3000_MCHBAR_MASK	0xffffc000
298c2ecf20Sopenharmony_ci#define I3000_MMR_WINDOW_SIZE	16384
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define I3000_EDEAP	0x70	/* Extended DRAM Error Address Pointer (8b)
328c2ecf20Sopenharmony_ci				 *
338c2ecf20Sopenharmony_ci				 * 7:1   reserved
348c2ecf20Sopenharmony_ci				 * 0     bit 32 of address
358c2ecf20Sopenharmony_ci				 */
368c2ecf20Sopenharmony_ci#define I3000_DEAP	0x58	/* DRAM Error Address Pointer (32b)
378c2ecf20Sopenharmony_ci				 *
388c2ecf20Sopenharmony_ci				 * 31:7  address
398c2ecf20Sopenharmony_ci				 * 6:1   reserved
408c2ecf20Sopenharmony_ci				 * 0     Error channel 0/1
418c2ecf20Sopenharmony_ci				 */
428c2ecf20Sopenharmony_ci#define I3000_DEAP_GRAIN 		(1 << 7)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/*
458c2ecf20Sopenharmony_ci * Helper functions to decode the DEAP/EDEAP hardware registers.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * The type promotion here is deliberate; we're deriving an
488c2ecf20Sopenharmony_ci * unsigned long pfn and offset from hardware regs which are u8/u32.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic inline unsigned long deap_pfn(u8 edeap, u32 deap)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	deap >>= PAGE_SHIFT;
548c2ecf20Sopenharmony_ci	deap |= (edeap & 1) << (32 - PAGE_SHIFT);
558c2ecf20Sopenharmony_ci	return deap;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic inline unsigned long deap_offset(u32 deap)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	return deap & ~(I3000_DEAP_GRAIN - 1) & ~PAGE_MASK;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic inline int deap_channel(u32 deap)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	return deap & 1;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define I3000_DERRSYN	0x5c	/* DRAM Error Syndrome (8b)
698c2ecf20Sopenharmony_ci				 *
708c2ecf20Sopenharmony_ci				 *  7:0  DRAM ECC Syndrome
718c2ecf20Sopenharmony_ci				 */
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define I3000_ERRSTS	0xc8	/* Error Status Register (16b)
748c2ecf20Sopenharmony_ci				 *
758c2ecf20Sopenharmony_ci				 * 15:12 reserved
768c2ecf20Sopenharmony_ci				 * 11    MCH Thermal Sensor Event
778c2ecf20Sopenharmony_ci				 *         for SMI/SCI/SERR
788c2ecf20Sopenharmony_ci				 * 10    reserved
798c2ecf20Sopenharmony_ci				 *  9    LOCK to non-DRAM Memory Flag (LCKF)
808c2ecf20Sopenharmony_ci				 *  8    Received Refresh Timeout Flag (RRTOF)
818c2ecf20Sopenharmony_ci				 *  7:2  reserved
828c2ecf20Sopenharmony_ci				 *  1    Multi-bit DRAM ECC Error Flag (DMERR)
838c2ecf20Sopenharmony_ci				 *  0    Single-bit DRAM ECC Error Flag (DSERR)
848c2ecf20Sopenharmony_ci				 */
858c2ecf20Sopenharmony_ci#define I3000_ERRSTS_BITS	0x0b03	/* bits which indicate errors */
868c2ecf20Sopenharmony_ci#define I3000_ERRSTS_UE		0x0002
878c2ecf20Sopenharmony_ci#define I3000_ERRSTS_CE		0x0001
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define I3000_ERRCMD	0xca	/* Error Command (16b)
908c2ecf20Sopenharmony_ci				 *
918c2ecf20Sopenharmony_ci				 * 15:12 reserved
928c2ecf20Sopenharmony_ci				 * 11    SERR on MCH Thermal Sensor Event
938c2ecf20Sopenharmony_ci				 *         (TSESERR)
948c2ecf20Sopenharmony_ci				 * 10    reserved
958c2ecf20Sopenharmony_ci				 *  9    SERR on LOCK to non-DRAM Memory
968c2ecf20Sopenharmony_ci				 *         (LCKERR)
978c2ecf20Sopenharmony_ci				 *  8    SERR on DRAM Refresh Timeout
988c2ecf20Sopenharmony_ci				 *         (DRTOERR)
998c2ecf20Sopenharmony_ci				 *  7:2  reserved
1008c2ecf20Sopenharmony_ci				 *  1    SERR Multi-Bit DRAM ECC Error
1018c2ecf20Sopenharmony_ci				 *         (DMERR)
1028c2ecf20Sopenharmony_ci				 *  0    SERR on Single-Bit ECC Error
1038c2ecf20Sopenharmony_ci				 *         (DSERR)
1048c2ecf20Sopenharmony_ci				 */
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* Intel  MMIO register space - device 0 function 0 - MMR space */
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define I3000_DRB_SHIFT 25	/* 32MiB grain */
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define I3000_C0DRB	0x100	/* Channel 0 DRAM Rank Boundary (8b x 4)
1118c2ecf20Sopenharmony_ci				 *
1128c2ecf20Sopenharmony_ci				 * 7:0   Channel 0 DRAM Rank Boundary Address
1138c2ecf20Sopenharmony_ci				 */
1148c2ecf20Sopenharmony_ci#define I3000_C1DRB	0x180	/* Channel 1 DRAM Rank Boundary (8b x 4)
1158c2ecf20Sopenharmony_ci				 *
1168c2ecf20Sopenharmony_ci				 * 7:0   Channel 1 DRAM Rank Boundary Address
1178c2ecf20Sopenharmony_ci				 */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci#define I3000_C0DRA	0x108	/* Channel 0 DRAM Rank Attribute (8b x 2)
1208c2ecf20Sopenharmony_ci				 *
1218c2ecf20Sopenharmony_ci				 * 7     reserved
1228c2ecf20Sopenharmony_ci				 * 6:4   DRAM odd Rank Attribute
1238c2ecf20Sopenharmony_ci				 * 3     reserved
1248c2ecf20Sopenharmony_ci				 * 2:0   DRAM even Rank Attribute
1258c2ecf20Sopenharmony_ci				 *
1268c2ecf20Sopenharmony_ci				 * Each attribute defines the page
1278c2ecf20Sopenharmony_ci				 * size of the corresponding rank:
1288c2ecf20Sopenharmony_ci				 *     000: unpopulated
1298c2ecf20Sopenharmony_ci				 *     001: reserved
1308c2ecf20Sopenharmony_ci				 *     010: 4 KB
1318c2ecf20Sopenharmony_ci				 *     011: 8 KB
1328c2ecf20Sopenharmony_ci				 *     100: 16 KB
1338c2ecf20Sopenharmony_ci				 *     Others: reserved
1348c2ecf20Sopenharmony_ci				 */
1358c2ecf20Sopenharmony_ci#define I3000_C1DRA	0x188	/* Channel 1 DRAM Rank Attribute (8b x 2) */
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic inline unsigned char odd_rank_attrib(unsigned char dra)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	return (dra & 0x70) >> 4;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic inline unsigned char even_rank_attrib(unsigned char dra)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	return dra & 0x07;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#define I3000_C0DRC0	0x120	/* DRAM Controller Mode 0 (32b)
1488c2ecf20Sopenharmony_ci				 *
1498c2ecf20Sopenharmony_ci				 * 31:30 reserved
1508c2ecf20Sopenharmony_ci				 * 29    Initialization Complete (IC)
1518c2ecf20Sopenharmony_ci				 * 28:11 reserved
1528c2ecf20Sopenharmony_ci				 * 10:8  Refresh Mode Select (RMS)
1538c2ecf20Sopenharmony_ci				 * 7     reserved
1548c2ecf20Sopenharmony_ci				 * 6:4   Mode Select (SMS)
1558c2ecf20Sopenharmony_ci				 * 3:2   reserved
1568c2ecf20Sopenharmony_ci				 * 1:0   DRAM Type (DT)
1578c2ecf20Sopenharmony_ci				 */
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define I3000_C0DRC1	0x124	/* DRAM Controller Mode 1 (32b)
1608c2ecf20Sopenharmony_ci				 *
1618c2ecf20Sopenharmony_ci				 * 31    Enhanced Addressing Enable (ENHADE)
1628c2ecf20Sopenharmony_ci				 * 30:0  reserved
1638c2ecf20Sopenharmony_ci				 */
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cienum i3000p_chips {
1668c2ecf20Sopenharmony_ci	I3000 = 0,
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistruct i3000_dev_info {
1708c2ecf20Sopenharmony_ci	const char *ctl_name;
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistruct i3000_error_info {
1748c2ecf20Sopenharmony_ci	u16 errsts;
1758c2ecf20Sopenharmony_ci	u8 derrsyn;
1768c2ecf20Sopenharmony_ci	u8 edeap;
1778c2ecf20Sopenharmony_ci	u32 deap;
1788c2ecf20Sopenharmony_ci	u16 errsts2;
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic const struct i3000_dev_info i3000_devs[] = {
1828c2ecf20Sopenharmony_ci	[I3000] = {
1838c2ecf20Sopenharmony_ci		.ctl_name = "i3000"},
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic struct pci_dev *mci_pdev;
1878c2ecf20Sopenharmony_cistatic int i3000_registered = 1;
1888c2ecf20Sopenharmony_cistatic struct edac_pci_ctl_info *i3000_pci;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic void i3000_get_error_info(struct mem_ctl_info *mci,
1918c2ecf20Sopenharmony_ci				 struct i3000_error_info *info)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	struct pci_dev *pdev;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	pdev = to_pci_dev(mci->pdev);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/*
1988c2ecf20Sopenharmony_ci	 * This is a mess because there is no atomic way to read all the
1998c2ecf20Sopenharmony_ci	 * registers at once and the registers can transition from CE being
2008c2ecf20Sopenharmony_ci	 * overwritten by UE.
2018c2ecf20Sopenharmony_ci	 */
2028c2ecf20Sopenharmony_ci	pci_read_config_word(pdev, I3000_ERRSTS, &info->errsts);
2038c2ecf20Sopenharmony_ci	if (!(info->errsts & I3000_ERRSTS_BITS))
2048c2ecf20Sopenharmony_ci		return;
2058c2ecf20Sopenharmony_ci	pci_read_config_byte(pdev, I3000_EDEAP, &info->edeap);
2068c2ecf20Sopenharmony_ci	pci_read_config_dword(pdev, I3000_DEAP, &info->deap);
2078c2ecf20Sopenharmony_ci	pci_read_config_byte(pdev, I3000_DERRSYN, &info->derrsyn);
2088c2ecf20Sopenharmony_ci	pci_read_config_word(pdev, I3000_ERRSTS, &info->errsts2);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/*
2118c2ecf20Sopenharmony_ci	 * If the error is the same for both reads then the first set
2128c2ecf20Sopenharmony_ci	 * of reads is valid.  If there is a change then there is a CE
2138c2ecf20Sopenharmony_ci	 * with no info and the second set of reads is valid and
2148c2ecf20Sopenharmony_ci	 * should be UE info.
2158c2ecf20Sopenharmony_ci	 */
2168c2ecf20Sopenharmony_ci	if ((info->errsts ^ info->errsts2) & I3000_ERRSTS_BITS) {
2178c2ecf20Sopenharmony_ci		pci_read_config_byte(pdev, I3000_EDEAP, &info->edeap);
2188c2ecf20Sopenharmony_ci		pci_read_config_dword(pdev, I3000_DEAP, &info->deap);
2198c2ecf20Sopenharmony_ci		pci_read_config_byte(pdev, I3000_DERRSYN, &info->derrsyn);
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	/*
2238c2ecf20Sopenharmony_ci	 * Clear any error bits.
2248c2ecf20Sopenharmony_ci	 * (Yes, we really clear bits by writing 1 to them.)
2258c2ecf20Sopenharmony_ci	 */
2268c2ecf20Sopenharmony_ci	pci_write_bits16(pdev, I3000_ERRSTS, I3000_ERRSTS_BITS,
2278c2ecf20Sopenharmony_ci			 I3000_ERRSTS_BITS);
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic int i3000_process_error_info(struct mem_ctl_info *mci,
2318c2ecf20Sopenharmony_ci				struct i3000_error_info *info,
2328c2ecf20Sopenharmony_ci				int handle_errors)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	int row, multi_chan, channel;
2358c2ecf20Sopenharmony_ci	unsigned long pfn, offset;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	multi_chan = mci->csrows[0]->nr_channels - 1;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	if (!(info->errsts & I3000_ERRSTS_BITS))
2408c2ecf20Sopenharmony_ci		return 0;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	if (!handle_errors)
2438c2ecf20Sopenharmony_ci		return 1;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	if ((info->errsts ^ info->errsts2) & I3000_ERRSTS_BITS) {
2468c2ecf20Sopenharmony_ci		edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1, 0, 0, 0,
2478c2ecf20Sopenharmony_ci				     -1, -1, -1,
2488c2ecf20Sopenharmony_ci				     "UE overwrote CE", "");
2498c2ecf20Sopenharmony_ci		info->errsts = info->errsts2;
2508c2ecf20Sopenharmony_ci	}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	pfn = deap_pfn(info->edeap, info->deap);
2538c2ecf20Sopenharmony_ci	offset = deap_offset(info->deap);
2548c2ecf20Sopenharmony_ci	channel = deap_channel(info->deap);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	row = edac_mc_find_csrow_by_page(mci, pfn);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	if (info->errsts & I3000_ERRSTS_UE)
2598c2ecf20Sopenharmony_ci		edac_mc_handle_error(HW_EVENT_ERR_UNCORRECTED, mci, 1,
2608c2ecf20Sopenharmony_ci				     pfn, offset, 0,
2618c2ecf20Sopenharmony_ci				     row, -1, -1,
2628c2ecf20Sopenharmony_ci				     "i3000 UE", "");
2638c2ecf20Sopenharmony_ci	else
2648c2ecf20Sopenharmony_ci		edac_mc_handle_error(HW_EVENT_ERR_CORRECTED, mci, 1,
2658c2ecf20Sopenharmony_ci				     pfn, offset, info->derrsyn,
2668c2ecf20Sopenharmony_ci				     row, multi_chan ? channel : 0, -1,
2678c2ecf20Sopenharmony_ci				     "i3000 CE", "");
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	return 1;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic void i3000_check(struct mem_ctl_info *mci)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	struct i3000_error_info info;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	edac_dbg(1, "MC%d\n", mci->mc_idx);
2778c2ecf20Sopenharmony_ci	i3000_get_error_info(mci, &info);
2788c2ecf20Sopenharmony_ci	i3000_process_error_info(mci, &info, 1);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic int i3000_is_interleaved(const unsigned char *c0dra,
2828c2ecf20Sopenharmony_ci				const unsigned char *c1dra,
2838c2ecf20Sopenharmony_ci				const unsigned char *c0drb,
2848c2ecf20Sopenharmony_ci				const unsigned char *c1drb)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	int i;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/*
2898c2ecf20Sopenharmony_ci	 * If the channels aren't populated identically then
2908c2ecf20Sopenharmony_ci	 * we're not interleaved.
2918c2ecf20Sopenharmony_ci	 */
2928c2ecf20Sopenharmony_ci	for (i = 0; i < I3000_RANKS_PER_CHANNEL / 2; i++)
2938c2ecf20Sopenharmony_ci		if (odd_rank_attrib(c0dra[i]) != odd_rank_attrib(c1dra[i]) ||
2948c2ecf20Sopenharmony_ci			even_rank_attrib(c0dra[i]) !=
2958c2ecf20Sopenharmony_ci						even_rank_attrib(c1dra[i]))
2968c2ecf20Sopenharmony_ci			return 0;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/*
2998c2ecf20Sopenharmony_ci	 * If the rank boundaries for the two channels are different
3008c2ecf20Sopenharmony_ci	 * then we're not interleaved.
3018c2ecf20Sopenharmony_ci	 */
3028c2ecf20Sopenharmony_ci	for (i = 0; i < I3000_RANKS_PER_CHANNEL; i++)
3038c2ecf20Sopenharmony_ci		if (c0drb[i] != c1drb[i])
3048c2ecf20Sopenharmony_ci			return 0;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	return 1;
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cistatic int i3000_probe1(struct pci_dev *pdev, int dev_idx)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	int rc;
3128c2ecf20Sopenharmony_ci	int i, j;
3138c2ecf20Sopenharmony_ci	struct mem_ctl_info *mci = NULL;
3148c2ecf20Sopenharmony_ci	struct edac_mc_layer layers[2];
3158c2ecf20Sopenharmony_ci	unsigned long last_cumul_size, nr_pages;
3168c2ecf20Sopenharmony_ci	int interleaved, nr_channels;
3178c2ecf20Sopenharmony_ci	unsigned char dra[I3000_RANKS / 2], drb[I3000_RANKS];
3188c2ecf20Sopenharmony_ci	unsigned char *c0dra = dra, *c1dra = &dra[I3000_RANKS_PER_CHANNEL / 2];
3198c2ecf20Sopenharmony_ci	unsigned char *c0drb = drb, *c1drb = &drb[I3000_RANKS_PER_CHANNEL];
3208c2ecf20Sopenharmony_ci	unsigned long mchbar;
3218c2ecf20Sopenharmony_ci	void __iomem *window;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	edac_dbg(0, "MC:\n");
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	pci_read_config_dword(pdev, I3000_MCHBAR, (u32 *) & mchbar);
3268c2ecf20Sopenharmony_ci	mchbar &= I3000_MCHBAR_MASK;
3278c2ecf20Sopenharmony_ci	window = ioremap(mchbar, I3000_MMR_WINDOW_SIZE);
3288c2ecf20Sopenharmony_ci	if (!window) {
3298c2ecf20Sopenharmony_ci		printk(KERN_ERR "i3000: cannot map mmio space at 0x%lx\n",
3308c2ecf20Sopenharmony_ci			mchbar);
3318c2ecf20Sopenharmony_ci		return -ENODEV;
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	c0dra[0] = readb(window + I3000_C0DRA + 0);	/* ranks 0,1 */
3358c2ecf20Sopenharmony_ci	c0dra[1] = readb(window + I3000_C0DRA + 1);	/* ranks 2,3 */
3368c2ecf20Sopenharmony_ci	c1dra[0] = readb(window + I3000_C1DRA + 0);	/* ranks 0,1 */
3378c2ecf20Sopenharmony_ci	c1dra[1] = readb(window + I3000_C1DRA + 1);	/* ranks 2,3 */
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	for (i = 0; i < I3000_RANKS_PER_CHANNEL; i++) {
3408c2ecf20Sopenharmony_ci		c0drb[i] = readb(window + I3000_C0DRB + i);
3418c2ecf20Sopenharmony_ci		c1drb[i] = readb(window + I3000_C1DRB + i);
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	iounmap(window);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/*
3478c2ecf20Sopenharmony_ci	 * Figure out how many channels we have.
3488c2ecf20Sopenharmony_ci	 *
3498c2ecf20Sopenharmony_ci	 * If we have what the datasheet calls "asymmetric channels"
3508c2ecf20Sopenharmony_ci	 * (essentially the same as what was called "virtual single
3518c2ecf20Sopenharmony_ci	 * channel mode" in the i82875) then it's a single channel as
3528c2ecf20Sopenharmony_ci	 * far as EDAC is concerned.
3538c2ecf20Sopenharmony_ci	 */
3548c2ecf20Sopenharmony_ci	interleaved = i3000_is_interleaved(c0dra, c1dra, c0drb, c1drb);
3558c2ecf20Sopenharmony_ci	nr_channels = interleaved ? 2 : 1;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	layers[0].type = EDAC_MC_LAYER_CHIP_SELECT;
3588c2ecf20Sopenharmony_ci	layers[0].size = I3000_RANKS / nr_channels;
3598c2ecf20Sopenharmony_ci	layers[0].is_virt_csrow = true;
3608c2ecf20Sopenharmony_ci	layers[1].type = EDAC_MC_LAYER_CHANNEL;
3618c2ecf20Sopenharmony_ci	layers[1].size = nr_channels;
3628c2ecf20Sopenharmony_ci	layers[1].is_virt_csrow = false;
3638c2ecf20Sopenharmony_ci	mci = edac_mc_alloc(0, ARRAY_SIZE(layers), layers, 0);
3648c2ecf20Sopenharmony_ci	if (!mci)
3658c2ecf20Sopenharmony_ci		return -ENOMEM;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	edac_dbg(3, "MC: init mci\n");
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	mci->pdev = &pdev->dev;
3708c2ecf20Sopenharmony_ci	mci->mtype_cap = MEM_FLAG_DDR2;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	mci->edac_ctl_cap = EDAC_FLAG_SECDED;
3738c2ecf20Sopenharmony_ci	mci->edac_cap = EDAC_FLAG_SECDED;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	mci->mod_name = EDAC_MOD_STR;
3768c2ecf20Sopenharmony_ci	mci->ctl_name = i3000_devs[dev_idx].ctl_name;
3778c2ecf20Sopenharmony_ci	mci->dev_name = pci_name(pdev);
3788c2ecf20Sopenharmony_ci	mci->edac_check = i3000_check;
3798c2ecf20Sopenharmony_ci	mci->ctl_page_to_phys = NULL;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	/*
3828c2ecf20Sopenharmony_ci	 * The dram rank boundary (DRB) reg values are boundary addresses
3838c2ecf20Sopenharmony_ci	 * for each DRAM rank with a granularity of 32MB.  DRB regs are
3848c2ecf20Sopenharmony_ci	 * cumulative; the last one will contain the total memory
3858c2ecf20Sopenharmony_ci	 * contained in all ranks.
3868c2ecf20Sopenharmony_ci	 *
3878c2ecf20Sopenharmony_ci	 * If we're in interleaved mode then we're only walking through
3888c2ecf20Sopenharmony_ci	 * the ranks of controller 0, so we double all the values we see.
3898c2ecf20Sopenharmony_ci	 */
3908c2ecf20Sopenharmony_ci	for (last_cumul_size = i = 0; i < mci->nr_csrows; i++) {
3918c2ecf20Sopenharmony_ci		u8 value;
3928c2ecf20Sopenharmony_ci		u32 cumul_size;
3938c2ecf20Sopenharmony_ci		struct csrow_info *csrow = mci->csrows[i];
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci		value = drb[i];
3968c2ecf20Sopenharmony_ci		cumul_size = value << (I3000_DRB_SHIFT - PAGE_SHIFT);
3978c2ecf20Sopenharmony_ci		if (interleaved)
3988c2ecf20Sopenharmony_ci			cumul_size <<= 1;
3998c2ecf20Sopenharmony_ci		edac_dbg(3, "MC: (%d) cumul_size 0x%x\n", i, cumul_size);
4008c2ecf20Sopenharmony_ci		if (cumul_size == last_cumul_size)
4018c2ecf20Sopenharmony_ci			continue;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci		csrow->first_page = last_cumul_size;
4048c2ecf20Sopenharmony_ci		csrow->last_page = cumul_size - 1;
4058c2ecf20Sopenharmony_ci		nr_pages = cumul_size - last_cumul_size;
4068c2ecf20Sopenharmony_ci		last_cumul_size = cumul_size;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci		for (j = 0; j < nr_channels; j++) {
4098c2ecf20Sopenharmony_ci			struct dimm_info *dimm = csrow->channels[j]->dimm;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci			dimm->nr_pages = nr_pages / nr_channels;
4128c2ecf20Sopenharmony_ci			dimm->grain = I3000_DEAP_GRAIN;
4138c2ecf20Sopenharmony_ci			dimm->mtype = MEM_DDR2;
4148c2ecf20Sopenharmony_ci			dimm->dtype = DEV_UNKNOWN;
4158c2ecf20Sopenharmony_ci			dimm->edac_mode = EDAC_UNKNOWN;
4168c2ecf20Sopenharmony_ci		}
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	/*
4208c2ecf20Sopenharmony_ci	 * Clear any error bits.
4218c2ecf20Sopenharmony_ci	 * (Yes, we really clear bits by writing 1 to them.)
4228c2ecf20Sopenharmony_ci	 */
4238c2ecf20Sopenharmony_ci	pci_write_bits16(pdev, I3000_ERRSTS, I3000_ERRSTS_BITS,
4248c2ecf20Sopenharmony_ci			 I3000_ERRSTS_BITS);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	rc = -ENODEV;
4278c2ecf20Sopenharmony_ci	if (edac_mc_add_mc(mci)) {
4288c2ecf20Sopenharmony_ci		edac_dbg(3, "MC: failed edac_mc_add_mc()\n");
4298c2ecf20Sopenharmony_ci		goto fail;
4308c2ecf20Sopenharmony_ci	}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	/* allocating generic PCI control info */
4338c2ecf20Sopenharmony_ci	i3000_pci = edac_pci_create_generic_ctl(&pdev->dev, EDAC_MOD_STR);
4348c2ecf20Sopenharmony_ci	if (!i3000_pci) {
4358c2ecf20Sopenharmony_ci		printk(KERN_WARNING
4368c2ecf20Sopenharmony_ci			"%s(): Unable to create PCI control\n",
4378c2ecf20Sopenharmony_ci			__func__);
4388c2ecf20Sopenharmony_ci		printk(KERN_WARNING
4398c2ecf20Sopenharmony_ci			"%s(): PCI error report via EDAC not setup\n",
4408c2ecf20Sopenharmony_ci			__func__);
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	/* get this far and it's successful */
4448c2ecf20Sopenharmony_ci	edac_dbg(3, "MC: success\n");
4458c2ecf20Sopenharmony_ci	return 0;
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cifail:
4488c2ecf20Sopenharmony_ci	if (mci)
4498c2ecf20Sopenharmony_ci		edac_mc_free(mci);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return rc;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci/* returns count (>= 0), or negative on error */
4558c2ecf20Sopenharmony_cistatic int i3000_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	int rc;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	edac_dbg(0, "MC:\n");
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	if (pci_enable_device(pdev) < 0)
4628c2ecf20Sopenharmony_ci		return -EIO;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	rc = i3000_probe1(pdev, ent->driver_data);
4658c2ecf20Sopenharmony_ci	if (!mci_pdev)
4668c2ecf20Sopenharmony_ci		mci_pdev = pci_dev_get(pdev);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	return rc;
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_cistatic void i3000_remove_one(struct pci_dev *pdev)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	struct mem_ctl_info *mci;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	edac_dbg(0, "\n");
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	if (i3000_pci)
4788c2ecf20Sopenharmony_ci		edac_pci_release_generic_ctl(i3000_pci);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	mci = edac_mc_del_mc(&pdev->dev);
4818c2ecf20Sopenharmony_ci	if (!mci)
4828c2ecf20Sopenharmony_ci		return;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	edac_mc_free(mci);
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic const struct pci_device_id i3000_pci_tbl[] = {
4888c2ecf20Sopenharmony_ci	{
4898c2ecf20Sopenharmony_ci	 PCI_VEND_DEV(INTEL, 3000_HB), PCI_ANY_ID, PCI_ANY_ID, 0, 0,
4908c2ecf20Sopenharmony_ci	 I3000},
4918c2ecf20Sopenharmony_ci	{
4928c2ecf20Sopenharmony_ci	 0,
4938c2ecf20Sopenharmony_ci	 }			/* 0 terminated list. */
4948c2ecf20Sopenharmony_ci};
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, i3000_pci_tbl);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_cistatic struct pci_driver i3000_driver = {
4998c2ecf20Sopenharmony_ci	.name = EDAC_MOD_STR,
5008c2ecf20Sopenharmony_ci	.probe = i3000_init_one,
5018c2ecf20Sopenharmony_ci	.remove = i3000_remove_one,
5028c2ecf20Sopenharmony_ci	.id_table = i3000_pci_tbl,
5038c2ecf20Sopenharmony_ci};
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic int __init i3000_init(void)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	int pci_rc;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	edac_dbg(3, "MC:\n");
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	/* Ensure that the OPSTATE is set correctly for POLL or NMI */
5128c2ecf20Sopenharmony_ci	opstate_init();
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	pci_rc = pci_register_driver(&i3000_driver);
5158c2ecf20Sopenharmony_ci	if (pci_rc < 0)
5168c2ecf20Sopenharmony_ci		goto fail0;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	if (!mci_pdev) {
5198c2ecf20Sopenharmony_ci		i3000_registered = 0;
5208c2ecf20Sopenharmony_ci		mci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
5218c2ecf20Sopenharmony_ci					PCI_DEVICE_ID_INTEL_3000_HB, NULL);
5228c2ecf20Sopenharmony_ci		if (!mci_pdev) {
5238c2ecf20Sopenharmony_ci			edac_dbg(0, "i3000 pci_get_device fail\n");
5248c2ecf20Sopenharmony_ci			pci_rc = -ENODEV;
5258c2ecf20Sopenharmony_ci			goto fail1;
5268c2ecf20Sopenharmony_ci		}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci		pci_rc = i3000_init_one(mci_pdev, i3000_pci_tbl);
5298c2ecf20Sopenharmony_ci		if (pci_rc < 0) {
5308c2ecf20Sopenharmony_ci			edac_dbg(0, "i3000 init fail\n");
5318c2ecf20Sopenharmony_ci			pci_rc = -ENODEV;
5328c2ecf20Sopenharmony_ci			goto fail1;
5338c2ecf20Sopenharmony_ci		}
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	return 0;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cifail1:
5398c2ecf20Sopenharmony_ci	pci_unregister_driver(&i3000_driver);
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cifail0:
5428c2ecf20Sopenharmony_ci	pci_dev_put(mci_pdev);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	return pci_rc;
5458c2ecf20Sopenharmony_ci}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_cistatic void __exit i3000_exit(void)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	edac_dbg(3, "MC:\n");
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	pci_unregister_driver(&i3000_driver);
5528c2ecf20Sopenharmony_ci	if (!i3000_registered) {
5538c2ecf20Sopenharmony_ci		i3000_remove_one(mci_pdev);
5548c2ecf20Sopenharmony_ci		pci_dev_put(mci_pdev);
5558c2ecf20Sopenharmony_ci	}
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cimodule_init(i3000_init);
5598c2ecf20Sopenharmony_cimodule_exit(i3000_exit);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
5628c2ecf20Sopenharmony_ciMODULE_AUTHOR("Akamai Technologies Arthur Ulfeldt/Jason Uhlenkott");
5638c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MC support for Intel 3000 memory hub controllers");
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cimodule_param(edac_op_state, int, 0444);
5668c2ecf20Sopenharmony_ciMODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");
567