/*++
 * Copyright (c) 1990-96 Digi International Inc., All Rights Reserved
 * 
 * This software contains proprietary and confidential information of Digi
 * International Inc.  By accepting transfer of this copy, Recipient agrees
 * to retain this software in confidence, to prevent disclosure to others,
 * and to make no use of this software other than that for which it was
 * delivered.  This is an unpublished copyrighted work of Digi International
 * Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 * prohibited.
 * 
 * Restricted Rights Legend
 *
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7031 or
 * subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 * Restricted Rights at 48 CFR 52.227-19, as applicable.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 *
 * Last Update: $Date: 10/22/97 11:56a $
 *
 * $Header: /Components/Windows/NT/Sync/Build/Mp570/io.h 3     10/22/97 11:56a Darinh $
 *
 * Revision History:
 * $Log: /Components/Windows/NT/Sync/Build/Mp570/io.h $
 * 
 * 3     10/22/97 11:56a Darinh
 * Changes to make multiple board installations work better.
 * 
 * 2     6/06/97 1:48p Darinh
 * Everything seems functional, but more testing is required.
 * 
 * 1     4/02/97 10:08a Darinh
 * Initial checkin; ISA cards initialize, but do not pass data.
 * 
--*/

#include <ntddk.h>

#ifndef _IO_H
#define _IO_H


#define IO_IN(_Addr)		\
	((UCHAR)(READ_PORT_UCHAR ((PUCHAR)(_Addr))))

#define IO_OUT(_Addr,_Val)	\
	WRITE_PORT_UCHAR((PUCHAR)(_Addr), (UCHAR)(_Val))


#define WRT_REG_UCHAR(_Addr,_Val)	\
	WRITE_REGISTER_UCHAR((PUCHAR)(_Addr), (UCHAR)(_Val))  

#define WRT_REG_USHORT(_Addr,_Val)	\
	WRITE_REGISTER_USHORT((PUSHORT)(_Addr), (USHORT)(_Val))

#define WRT_REG_ULONG(_Addr,_Val)	\
	WRITE_REGISTER_ULONG((PULONG)(_Addr), (ULONG)(_Val))


#define IO_DELAY(us)                KeStallExecutionProcessor(us)


#define READ_ADAPTER_BUFFER(src, dest, len)							\
	{																\
		if ((len) < 4)												\
		{															\
			READ_REGISTER_BUFFER_UCHAR((src), (dest), (len));		\
		}															\
		else														\
		{															\
			ULONG_PTR	NumDwords, OddEnd;							\
			PUCHAR	Source = (PUCHAR)(src);							\
			PUCHAR	Dest = (PUCHAR)(dest);							\
			ULONG_PTR	Length = (len);								\
			ULONG_PTR	OddStart = 4-((ULONG_PTR)Source & 3);		\
			if (OddStart)											\
			{														\
				READ_REGISTER_BUFFER_UCHAR(Source, Dest, (ULONG)OddStart);	\
				Source += OddStart;									\
				Dest += OddStart;									\
				Length -= OddStart;									\
			}														\
			NumDwords = Length>>2;									\
			if (NumDwords)											\
			{														\
				READ_REGISTER_BUFFER_ULONG((PULONG)Source, (PULONG)Dest, (ULONG)NumDwords);								 		\
			}														\
			OddEnd = Length&3;										\
			if (OddEnd)												\
			{														\
				NumDwords *= 4;										\
				Source += NumDwords;								\
				Dest += NumDwords;									\
				READ_REGISTER_BUFFER_UCHAR(Source, Dest, (ULONG)OddEnd);	\
			}														\
		}															\
	}

#define WRITE_ADAPTER_BUFFER(dest, src, len)						\
	{																\
		if ((len) < 4)												\
		{															\
			WRITE_REGISTER_BUFFER_UCHAR((dest), (src), (len));		\
		}															\
		else														\
		{															\
			ULONG_PTR	NumDwords, OddEnd;							\
			PUCHAR		Source = (PUCHAR)(src);						\
			PUCHAR		Dest = (PUCHAR)(dest);						\
			ULONG_PTR	Length = (len);								\
			ULONG_PTR	OddStart = 4-((ULONG_PTR)Dest & 3);			\
			if (OddStart)											\
			{														\
				WRITE_REGISTER_BUFFER_UCHAR(Dest, Source, (ULONG)OddStart);\
				Source += OddStart;									\
				Dest += OddStart;									\
				Length -= OddStart;									\
			}														\
			NumDwords = Length>>2;									\
			if (NumDwords)											\
			{														\
				WRITE_REGISTER_BUFFER_ULONG((PULONG)Dest, (PULONG)Source, (ULONG)NumDwords);										\
			}														\
			OddEnd = Length&3;										\
			if (OddEnd)												\
			{														\
				NumDwords *= 4;										\
				Source += NumDwords;								\
				Dest += NumDwords;									\
				WRITE_REGISTER_BUFFER_UCHAR(Dest, Source, (ULONG)OddEnd);	\
			}														\
		}															\
	}

//
//  Function prototypes from io.c
//

UCHAR
fnReadIO (
	PUCHAR		pSrc);

VOID
fnWriteIO (
	IN PUCHAR	pSrc,
	IN UCHAR	data);

UCHAR
fnReadMem (
	IN PUCHAR	pSrc);

VOID
fnWriteMem (
	IN PUCHAR	pSrc,
	IN UCHAR	data);


//
//  Some utility functions for dealing with register functions
//
VOID
WriteMemSelReg (
	IN PCONTROLLER_EXT	pControllerExt,
	IN UCHAR			WriteVal);

VOID
ResetMemSelReg (
	IN PCONTROLLER_EXT	pControllerExt);

VOID
DisableInterrupt (
	IN PCONTROLLER_EXT	pControllerExt);

VOID
EnableScaMemMap (
	IN PDEVICE_EXT	pDeviceExt);

VOID
EnableMemory (
	IN PDEVICE_EXT	pDeviceExt,
	IN UCHAR		DesiredWindow);

VOID
EnableControllerMemory (
	IN PCONTROLLER_EXT	pControllerExt,
	IN UCHAR			DesiredWindow);

VOID
DisableMemory (
	IN PDEVICE_EXT	pDeviceExt);

VOID
DisableControllerMemory (
	IN PCONTROLLER_EXT	pControllerExt);

#endif // _IO_H
