/*

    Copyright (C) Corollary, Inc., 1993, 1994.
    All Rights Reserved.
    This Module contains Proprietary Information of
    Corollary, Inc., and should be treated as Confidential.

Module Name:

    cbus2ecc.c

Abstract:

    This module implements the Corollary Cbus2 ECC specific functions for
    the OS/2 2.x Corollary SMP PSD.

    Since the Cbus2 ECC register decoding is not yet finalized, this code
    has been #if 0'd.  It will need to be fleshed out when the hardware is
    finished.

Author:

    Landy Wang (landy@corollary.com) 11-Jan-1994
 */

#include <psd.h>
#include "cbustype.h"
#include "cbusrrd.h"		/* PSD <-> RRD interface definitions */
#include "cbus_sw.h"		/* C-bus OS/2-specific implementation stuff */
#include "cbus.h"		/* needed for cbus2.h inclusion */
#include "cbus2.h"		/* C-bus II specific stuff */

VOID
CbusClearEISANMI(VOID);

#if 0
/*
 *  defines for the Cbus2 ECC syndrome
 */
#define MULTIBIT	3
#define DOUBLEBIT	2
#define SINGLEBIT	1
#define NOECCERROR	0x7f

/*
 *  defines for the Cbus2 ECC error register
 */
typedef struct _extmear_t {
	ULONG Syndrome:8;
	ULONG reserved:24;
} EXTMEAR_T, *PEXTMEAR;

/*
 * we can decide whether we want to use the exact bit identifier table OR
 * just the "how-many bit error" identifier table.  When the hardware is
 * done, this decision should be made (based on the hardware) and one of
 * these tables can be removed.
 */
UCHAR				cbus2_edac_syndrome[] = {

NOECCERROR,/* 00 */ BIT ECC0,  /* 01 */ BIT ECC1,  /* 02 */ MULTIBIT,  /* 03 */
BIT ECC2,  /* 04 */ MULTIBIT,  /* 05 */ MULTIBIT,  /* 06 */ MULTIBIT,  /* 07 */
BIT ECC3,  /* 08 */ MULTIBIT,  /* 09 */ MULTIBIT,  /* 0A */ BIT 17,    /* 0B */
MULTIBIT,  /* 0C */ MULTIBIT,  /* 0D */ BIT  16,   /* 0E */ MULTIBIT,  /* 0F */
BIT  ECC4, /* 10 */ MULTIBIT,  /* 11 */ MULTIBIT,  /* 12 */ BIT  18,   /* 13 */
MULTIBIT,  /* 14 */ BIT  19,   /* 15 */ BIT  20,   /* 16 */ MULTIBIT,  /* 17 */
MULTIBIT,  /* 18 */ BIT  21,   /* 19 */ BIT  22,   /* 1A */ MULTIBIT,  /* 1B */
BIT  23,   /* 1C */ MULTIBIT,  /* 1D */ MULTIBIT,  /* 1E */ MULTIBIT,  /* 1F */
BIT  ECC5, /* 20 */ MULTIBIT,  /* 21 */ MULTIBIT,  /* 22 */ BIT  8,    /* 23 */
MULTIBIT,  /* 24 */ BIT  9,    /* 25 */ BIT  10,   /* 26 */ MULTIBIT,  /* 27 */
MULTIBIT,  /* 28 */ BIT  11,   /* 29 */ BIT  12,   /* 2A */ MULTIBIT,  /* 2B */
BIT  13,   /* 2C */ MULTIBIT,  /* 2D */ MULTIBIT,  /* 2E */ MULTIBIT,  /* 2F */
MULTIBIT,  /* 30 */ BIT  14,   /* 31 */ MULTIBIT,  /* 32 */ MULTIBIT,  /* 33 */
BIT  15,   /* 34 */ MULTIBIT,  /* 35 */ MULTIBIT,  /* 36 */ MULTIBIT,  /* 37 */
MULTIBIT,  /* 38 */ MULTIBIT,  /* 39 */ MULTIBIT,  /* 3A */ MULTIBIT,  /* 3B */
MULTIBIT,  /* 3C */ MULTIBIT,  /* 3D */ MULTIBIT,  /* 3E */ MULTIBIT,  /* 3F */
BIT  ECC6, /* 40 */ MULTIBIT,  /* 41 */ MULTIBIT,  /* 42 */ MULTIBIT,  /* 43 */
MULTIBIT,  /* 44 */ MULTIBIT,  /* 45 */ MULTIBIT,  /* 46 */ MULTIBIT,  /* 47 */
MULTIBIT,  /* 48 */ MULTIBIT,  /* 49 */ BIT  33,   /* 4A */ MULTIBIT,  /* 4B */
MULTIBIT,  /* 4C */ MULTIBIT,  /* 4D */ MULTIBIT,  /* 4E */ BIT  32,   /* 4F */
MULTIBIT,  /* 50 */ MULTIBIT,  /* 51 */ BIT  34,   /* 52 */ MULTIBIT,  /* 53 */
BIT  35,   /* 54 */ MULTIBIT,  /* 55 */ MULTIBIT,  /* 56 */ BIT  36,   /* 57 */
BIT  37,   /* 58 */ MULTIBIT,  /* 59 */ MULTIBIT,  /* 5A */ BIT  38,   /* 5B */
MULTIBIT,  /* 5C */ BIT  39,   /* 5D */ MULTIBIT,  /* 5E */ MULTIBIT,  /* 5F */
MULTIBIT,  /* 60 */ MULTIBIT,  /* 61 */ BIT  56,   /* 62 */ MULTIBIT,  /* 63 */
BIT  57,   /* 64 */ MULTIBIT,  /* 65 */ MULTIBIT,  /* 66 */ BIT  58,   /* 67 */
BIT  59,   /* 68 */ MULTIBIT,  /* 69 */ MULTIBIT,  /* 6A */ BIT  60,   /* 6B */
MULTIBIT,  /* 6C */ BIT  61,   /* 6D */ MULTIBIT,  /* 6E */ MULTIBIT,  /* 6F */
BIT  62,   /* 70 */ MULTIBIT,  /* 71 */ MULTIBIT,  /* 72 */ MULTIBIT,  /* 73 */
MULTIBIT,  /* 74 */ BIT  63,   /* 75 */ MULTIBIT,  /* 76 */ MULTIBIT,  /* 77 */
MULTIBIT,  /* 78 */ MULTIBIT,  /* 79 */ MULTIBIT,  /* 7A */ MULTIBIT,  /* 7B */
MULTIBIT,  /* 7C */ MULTIBIT,  /* 7D */ MULTIBIT,  /* 7E */ MULTIBIT,  /* 7F */
BIT  ECC7, /* 80 */ MULTIBIT,  /* 81 */ MULTIBIT,  /* 82 */ MULTIBIT,  /* 83 */
MULTIBIT,  /* 84 */ MULTIBIT,  /* 85 */ MULTIBIT,  /* 86 */ MULTIBIT,  /* 87 */
MULTIBIT,  /* 88 */ MULTIBIT,  /* 89 */ BIT  49,   /* 8A */ MULTIBIT,  /* 8B */
MULTIBIT,  /* 8C */ MULTIBIT,  /* 8D */ MULTIBIT,  /* 8E */ BIT  48,   /* 8F */
MULTIBIT,  /* 90 */ MULTIBIT,  /* 91 */ BIT  50,   /* 92 */ MULTIBIT,  /* 93 */
BIT  51,   /* 94 */ MULTIBIT,  /* 95 */ MULTIBIT,  /* 96 */ BIT  52,   /* 97 */
BIT  53,   /* 98 */ MULTIBIT,  /* 99 */ MULTIBIT,  /* 9A */ BIT  54,   /* 9B */
MULTIBIT,  /* 9C */ BIT  55,   /* 9D */ MULTIBIT,  /* 9E */ MULTIBIT,  /* 9F */
MULTIBIT,  /* A0 */ MULTIBIT,  /* A1 */ BIT  40,   /* A2 */ MULTIBIT,  /* A3 */
BIT  41,   /* A4 */ MULTIBIT,  /* A5 */ MULTIBIT,  /* A6 */ BIT  42,   /* A7 */
BIT  43,   /* A8 */ MULTIBIT,  /* A9 */ MULTIBIT,  /* AA */ BIT  44,   /* AB */
MULTIBIT,  /* AC */ BIT  45,   /* AD */ MULTIBIT,  /* AE */ MULTIBIT,  /* AF */
BIT  46,   /* B0 */ MULTIBIT,  /* B1 */ MULTIBIT,  /* B2 */ MULTIBIT,  /* B3 */
MULTIBIT,  /* B4 */ BIT  47,   /* B5 */ MULTIBIT,  /* B6 */ MULTIBIT,  /* B7 */
MULTIBIT,  /* B8 */ MULTIBIT,  /* B9 */ MULTIBIT,  /* BA */ MULTIBIT,  /* BB */
MULTIBIT,  /* BC */ MULTIBIT,  /* BD */ MULTIBIT,  /* BE */ MULTIBIT,  /* BF */
MULTIBIT,  /* C0 */ MULTIBIT,  /* C1 */ MULTIBIT,  /* C2 */ MULTIBIT,  /* C3 */
MULTIBIT,  /* C4 */ MULTIBIT,  /* C5 */ MULTIBIT,  /* C6 */ MULTIBIT,  /* C7 */
MULTIBIT,  /* C8 */ MULTIBIT,  /* C9 */ MULTIBIT,  /* CA */ BIT  1,    /* CB */
MULTIBIT,  /* CC */ MULTIBIT,  /* CD */ BIT  0,    /* CE */ MULTIBIT,  /* CF */
MULTIBIT,  /* D0 */ MULTIBIT,  /* D1 */ MULTIBIT,  /* D2 */ BIT  2,    /* D3 */
MULTIBIT,  /* D4 */ BIT  3,    /* D5 */ BIT  4,    /* D6 */ MULTIBIT,  /* D7 */
MULTIBIT,  /* D8 */ BIT  5,    /* D9 */ BIT  6,    /* DA */ MULTIBIT,  /* DB */
BIT  7,    /* DC */ MULTIBIT,  /* DD */ MULTIBIT,  /* DE */ MULTIBIT,  /* DF */
MULTIBIT,  /* E0 */ MULTIBIT,  /* E1 */ MULTIBIT,  /* E2 */ BIT  24,   /* E3 */
MULTIBIT,  /* E4 */ BIT  25,   /* E5 */ BIT  26,   /* E6 */ MULTIBIT,  /* E7 */
MULTIBIT,  /* E8 */ BIT  27,   /* E9 */ BIT  28,   /* EA */ MULTIBIT,  /* EB */
BIT  29,   /* EC */ MULTIBIT,  /* ED */ MULTIBIT,  /* EE */ MULTIBIT,  /* EF */
MULTIBIT,  /* F0 */ BIT  30,   /* F1 */ MULTIBIT,  /* F2 */ MULTIBIT,  /* F3 */
BIT  31,   /* F4 */ MULTIBIT,  /* F5 */ MULTIBIT,  /* F6 */ MULTIBIT,  /* F7 */
MULTIBIT,  /* F8 */ MULTIBIT,  /* F9 */ MULTIBIT,  /* FA */ MULTIBIT,  /* FB */
MULTIBIT,  /* FC */ MULTIBIT,  /* FD */ MULTIBIT,  /* FE */ MULTIBIT,  /* FF */

};

UCHAR				cbus2_edac_syndrome[] = {

NOECCERROR,/* 00 */ SINGLEBIT, /* 01 */ SINGLEBIT, /* 02 */ MULTIBIT,  /* 03 */
SINGLEBIT, /* 04 */ MULTIBIT,  /* 05 */ MULTIBIT,  /* 06 */ MULTIBIT,  /* 07 */
SINGLEBIT, /* 08 */ MULTIBIT,  /* 09 */ MULTIBIT,  /* 0A */ SINGLEBIT, /* 0B */
MULTIBIT,  /* 0C */ MULTIBIT,  /* 0D */ SINGLEBIT, /* 0E */ MULTIBIT,  /* 0F */
SINGLEBIT, /* 10 */ MULTIBIT,  /* 11 */ MULTIBIT,  /* 12 */ SINGLEBIT, /* 13 */
MULTIBIT,  /* 14 */ SINGLEBIT, /* 15 */ SINGLEBIT, /* 16 */ MULTIBIT,  /* 17 */
MULTIBIT,  /* 18 */ SINGLEBIT, /* 19 */ SINGLEBIT, /* 1A */ MULTIBIT,  /* 1B */
SINGLEBIT, /* 1C */ MULTIBIT,  /* 1D */ MULTIBIT,  /* 1E */ MULTIBIT,  /* 1F */
SINGLEBIT, /* 20 */ MULTIBIT,  /* 21 */ MULTIBIT,  /* 22 */ SINGLEBIT, /* 23 */
MULTIBIT,  /* 24 */ SINGLEBIT, /* 25 */ SINGLEBIT, /* 26 */ MULTIBIT,  /* 27 */
MULTIBIT,  /* 28 */ SINGLEBIT, /* 29 */ SINGLEBIT, /* 2A */ MULTIBIT,  /* 2B */
SINGLEBIT, /* 2C */ MULTIBIT,  /* 2D */ MULTIBIT,  /* 2E */ MULTIBIT,  /* 2F */
MULTIBIT,  /* 30 */ SINGLEBIT, /* 31 */ MULTIBIT,  /* 32 */ MULTIBIT,  /* 33 */
SINGLEBIT, /* 34 */ MULTIBIT,  /* 35 */ MULTIBIT,  /* 36 */ MULTIBIT,  /* 37 */
MULTIBIT,  /* 38 */ MULTIBIT,  /* 39 */ MULTIBIT,  /* 3A */ MULTIBIT,  /* 3B */
MULTIBIT,  /* 3C */ MULTIBIT,  /* 3D */ MULTIBIT,  /* 3E */ MULTIBIT,  /* 3F */
SINGLEBIT, /* 40 */ MULTIBIT,  /* 41 */ MULTIBIT,  /* 42 */ MULTIBIT,  /* 43 */
MULTIBIT,  /* 44 */ MULTIBIT,  /* 45 */ MULTIBIT,  /* 46 */ MULTIBIT,  /* 47 */
MULTIBIT,  /* 48 */ MULTIBIT,  /* 49 */ SINGLEBIT, /* 4A */ MULTIBIT,  /* 4B */
MULTIBIT,  /* 4C */ MULTIBIT,  /* 4D */ MULTIBIT,  /* 4E */ SINGLEBIT, /* 4F */
MULTIBIT,  /* 50 */ MULTIBIT,  /* 51 */ SINGLEBIT, /* 52 */ MULTIBIT,  /* 53 */
SINGLEBIT, /* 54 */ MULTIBIT,  /* 55 */ MULTIBIT,  /* 56 */ SINGLEBIT, /* 57 */
SINGLEBIT, /* 58 */ MULTIBIT,  /* 59 */ MULTIBIT,  /* 5A */ SINGLEBIT, /* 5B */
MULTIBIT,  /* 5C */ SINGLEBIT, /* 5D */ MULTIBIT,  /* 5E */ MULTIBIT,  /* 5F */
MULTIBIT,  /* 60 */ MULTIBIT,  /* 61 */ SINGLEBIT, /* 62 */ MULTIBIT,  /* 63 */
SINGLEBIT, /* 64 */ MULTIBIT,  /* 65 */ MULTIBIT,  /* 66 */ SINGLEBIT, /* 67 */
SINGLEBIT, /* 68 */ MULTIBIT,  /* 69 */ MULTIBIT,  /* 6A */ SINGLEBIT, /* 6B */
MULTIBIT,  /* 6C */ SINGLEBIT, /* 6D */ MULTIBIT,  /* 6E */ MULTIBIT,  /* 6F */
SINGLEBIT, /* 70 */ MULTIBIT,  /* 71 */ MULTIBIT,  /* 72 */ MULTIBIT,  /* 73 */
MULTIBIT,  /* 74 */ SINGLEBIT, /* 75 */ MULTIBIT,  /* 76 */ MULTIBIT,  /* 77 */
MULTIBIT,  /* 78 */ MULTIBIT,  /* 79 */ MULTIBIT,  /* 7A */ MULTIBIT,  /* 7B */
MULTIBIT,  /* 7C */ MULTIBIT,  /* 7D */ MULTIBIT,  /* 7E */ MULTIBIT,  /* 7F */
SINGLEBIT, /* 80 */ MULTIBIT,  /* 81 */ MULTIBIT,  /* 82 */ MULTIBIT,  /* 83 */
MULTIBIT,  /* 84 */ MULTIBIT,  /* 85 */ MULTIBIT,  /* 86 */ MULTIBIT,  /* 87 */
MULTIBIT,  /* 88 */ MULTIBIT,  /* 89 */ SINGLEBIT, /* 8A */ MULTIBIT,  /* 8B */
MULTIBIT,  /* 8C */ MULTIBIT,  /* 8D */ MULTIBIT,  /* 8E */ SINGLEBIT, /* 8F */
MULTIBIT,  /* 90 */ MULTIBIT,  /* 91 */ SINGLEBIT, /* 92 */ MULTIBIT,  /* 93 */
SINGLEBIT, /* 94 */ MULTIBIT,  /* 95 */ MULTIBIT,  /* 96 */ SINGLEBIT, /* 97 */
SINGLEBIT, /* 98 */ MULTIBIT,  /* 99 */ MULTIBIT,  /* 9A */ SINGLEBIT, /* 9B */
MULTIBIT,  /* 9C */ SINGLEBIT, /* 9D */ MULTIBIT,  /* 9E */ MULTIBIT,  /* 9F */
MULTIBIT,  /* A0 */ MULTIBIT,  /* A1 */ SINGLEBIT, /* A2 */ MULTIBIT,  /* A3 */
SINGLEBIT, /* A4 */ MULTIBIT,  /* A5 */ MULTIBIT,  /* A6 */ SINGLEBIT, /* A7 */
SINGLEBIT, /* A8 */ MULTIBIT,  /* A9 */ MULTIBIT,  /* AA */ SINGLEBIT, /* AB */
MULTIBIT,  /* AC */ SINGLEBIT, /* AD */ MULTIBIT,  /* AE */ MULTIBIT,  /* AF */
SINGLEBIT, /* B0 */ MULTIBIT,  /* B1 */ MULTIBIT,  /* B2 */ MULTIBIT,  /* B3 */
MULTIBIT,  /* B4 */ SINGLEBIT, /* B5 */ MULTIBIT,  /* B6 */ MULTIBIT,  /* B7 */
MULTIBIT,  /* B8 */ MULTIBIT,  /* B9 */ MULTIBIT,  /* BA */ MULTIBIT,  /* BB */
MULTIBIT,  /* BC */ MULTIBIT,  /* BD */ MULTIBIT,  /* BE */ MULTIBIT,  /* BF */
MULTIBIT,  /* C0 */ MULTIBIT,  /* C1 */ MULTIBIT,  /* C2 */ MULTIBIT,  /* C3 */
MULTIBIT,  /* C4 */ MULTIBIT,  /* C5 */ MULTIBIT,  /* C6 */ MULTIBIT,  /* C7 */
MULTIBIT,  /* C8 */ MULTIBIT,  /* C9 */ MULTIBIT,  /* CA */ SINGLEBIT, /* CB */
MULTIBIT,  /* CC */ MULTIBIT,  /* CD */ SINGLEBIT, /* CE */ MULTIBIT,  /* CF */
MULTIBIT,  /* D0 */ MULTIBIT,  /* D1 */ MULTIBIT,  /* D2 */ SINGLEBIT, /* D3 */
MULTIBIT,  /* D4 */ SINGLEBIT, /* D5 */ SINGLEBIT, /* D6 */ MULTIBIT,  /* D7 */
MULTIBIT,  /* D8 */ SINGLEBIT, /* D9 */ SINGLEBIT, /* DA */ MULTIBIT,  /* DB */
SINGLEBIT, /* DC */ MULTIBIT,  /* DD */ MULTIBIT,  /* DE */ MULTIBIT,  /* DF */
MULTIBIT,  /* E0 */ MULTIBIT,  /* E1 */ MULTIBIT,  /* E2 */ SINGLEBIT, /* E3 */
MULTIBIT,  /* E4 */ SINGLEBIT, /* E5 */ SINGLEBIT, /* E6 */ MULTIBIT,  /* E7 */
MULTIBIT,  /* E8 */ SINGLEBIT, /* E9 */ SINGLEBIT, /* EA */ MULTIBIT,  /* EB */
SINGLEBIT, /* EC */ MULTIBIT,  /* ED */ MULTIBIT,  /* EE */ MULTIBIT,  /* EF */
MULTIBIT,  /* F0 */ SINGLEBIT, /* F1 */ MULTIBIT,  /* F2 */ MULTIBIT,  /* F3 */
SINGLEBIT, /* F4 */ MULTIBIT,  /* F5 */ MULTIBIT,  /* F6 */ MULTIBIT,  /* F7 */
MULTIBIT,  /* F8 */ MULTIBIT,  /* F9 */ MULTIBIT,  /* FA */ MULTIBIT,  /* FB */
MULTIBIT,  /* FC */ MULTIBIT,  /* FD */ MULTIBIT,  /* FE */ MULTIBIT,  /* FF */

};
#endif

#if DBG
#define NMI_BUTTON_PRESSED()	1
#else
#define NMI_BUTTON_PRESSED()	0
#endif

/*

Routine Description:

    This function determines the cause of the NMI so that the user can
    replace any offending SIMMs.

Arguments:

    NmiInfo - pointer to the NMI information structure

Return Value:

    Returns the byte address which caused the NMI, 0 if indeterminate

 */
ULONG
Cbus2ResolveNMI()
{
#if 0
	PCSR			csr;
	ULONG			syndrome;
	ULONG			ErrorType;
#endif
	ULONG			NMIAddressLow;
	ULONG			NMIAddressHigh;

	if (NMI_BUTTON_PRESSED()) {

		/*
		 * NMI button was pressed, so go to the debugger
		 */
		_asm int 3;

		/*
		 * Clear the NMI in hardware so the system can continue
		 */

		CbusClearEISANMI();

                return NO_ERROR;          /* ignore this NMI */
	}

	if (CbusGlobal.nonstdecc)
                return API_FAILED;

	NMIAddressLow = 0;
	NMIAddressHigh = 0;

#if 0
	/*
	 * no need to poll memory cards for Cbus2 - the error will be
	 * latched in this processor's DPX registers.
	 */

        csr = CbusCSR[CbusPerCPU->processor].csr;

	syndrome = (ULONG)((PEXTMEAR)(csr->EccSyndrome))->Syndrome;

	/*
	 * check if this memory board generated the ecc error
	 * double check all the byte vs. clicks conversions and
	 * also the baseram (for jumpered megabytes)
	 */

	ErrorType = cbus2_edac_syndrome[syndrome];

	if (ErrorType != NOECCERROR && ErrorType != SINGLEBIT) {

		NMIAddressLow = csr->EccReadAddress;
		NMIAddressHigh = csr->EccWriteAddress;
	
		/*
		 * disable ecc so we can safely take the system down
		 * without getting another double-bit error (because the
		 * kernel may iret later).
		 */
	
		Cbus2ECCDisable();
	}
#endif

	if (NMIAddressHigh || NMIAddressLow) {

                printf("NMI: double-bit ecc error address=0x%x\n",
				NMIAddressHigh, NMIAddressLow);

                return NO_ERROR;
	}
	
	/*
	 * No errors found in Cbus RAM, so check for EISA errors
	 */

	return API_FAILED;
}
