/*---------------------------------------------------------------------------*\
 *   
 *   Copyright (C) 2006 AVM GmbH <fritzbox_info@avm.de>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation version 2 of the License.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
\*---------------------------------------------------------------------------*/

#if !defined (_ahcicopy_h_)
#define _ahcicopy_h_

#include "ahcidef.h"
#include "ahcireg.h"

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/
static inline void ahci_memcpy (void * dst, void * src, unsigned len) {
	unsigned * dp = (unsigned *) dst;
	unsigned * sp = (unsigned *) src;

	assert ((len & 3) == 0);
	while (len > 0) {
		*dp++ = *sp++;
		len -= 4;
	}
} /* ahci_memcpy */

/*---------------------------------------------------------------------------*\
	Copy Buffer (size of multiple DWORDs) to DWORD aligned AHCI Memory
\*---------------------------------------------------------------------------*/
AHCI_INLINE_PREFIX
void AHCI_DWORDCopyToAlignedHW (PAHCI_REGISTER pTo, void *pFrom, unsigned len) {
	assert (AHCI_isAligned (pTo));
	info (len > 0);
	assert ((len & 3) == 0);
	
	ahci_memcpy ((void*)pTo, (void*)pFrom, len);
}

/*---------------------------------------------------------------------------*\
	Copy Buffer (size of multiple DWORDs) to DWORD aligned AHCI Memory
\*---------------------------------------------------------------------------*/
AHCI_INLINE_PREFIX
void AHCI_DWORDCopyFromAlignedHW (void *pTo, PAHCI_REGISTER pFrom, unsigned len) {
	assert (AHCI_isAligned (pFrom));
	assert ((len & 3) == 0);
	
	ahci_memcpy ((void*)pTo, (void*)pFrom, len);
}

/*---------------------------------------------------------------------------*\
	Copy Buffer to DWORD aligned AHCI Memory
	(Last DWORD may be overwritten)
\*---------------------------------------------------------------------------*/
void AHCI_CopyToAlignedHW (PAHCI_REGISTER pTo, char * pFrom, unsigned len);

/*---------------------------------------------------------------------------*\
	Copy Buffer to unaligned AHCI Memory
	First DWORD is not overwritten.
	(Last DWORD may be overwritten)
\*---------------------------------------------------------------------------*/
void AHCI_CopyToHWSafe (void *pTo, char * pFrom, unsigned len);

#endif //_ahcicopy_h_
