/*---------------------------------------------------------------------------*\
 *   
 *   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 (_ahciops_h_)
#define _ahciops_h_

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

/*---------------------------------------------------------------------------*\
  CONFIG_AHCI_ACCESS_GCR_SAFE is only needed for temporary hardware version
  where GlobalControlRegister has not W1C semantics for all flags
\*---------------------------------------------------------------------------*/
/* #define CONFIG_AHCI_ACCESS_GCR_SAFE */

#if !defined (CONFIG_AHCI_ACCESS_GCR_SAFE)
#define OldValue 0
#endif

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/
AHCI_INLINE_PREFIX
unsigned AHCI_GetFrameNumber (void) {
	AHCI_DWORD Value = AHCI_READ_REGISTER (pAHCI_FrameNumberReg);
	assert (Value == AHCI_FNR_curFrameNumber(Value));
	return Value;
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/
AHCI_INLINE_PREFIX
unsigned AHCI_GetCurRxAddress (void) {
	AHCI_DWORD Value = AHCI_READ_REGISTER (pAHCI_CurRxAddrReg);
	assert (Value == AHCI_CRXAR_curRxAddress (Value));
	return Value;
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/
AHCI_INLINE_PREFIX
int AHCI_ResetHost (void) {
#   if defined (CONFIG_AHCI_ACCESS_GCR_SAFE)
    unsigned OldValue = AHCI_READ_REGISTER (pAHCI_GlobalControlReg);
#   endif
    unsigned old_jiffies = jiffies;
    unsigned jiffie_diff = 0;

	AHCI_WRITE_REGISTER (pAHCI_GlobalControlReg, OldValue | AHCI_GCR_globalReset_FLAG);
	
	while (AHCI_READ_REGISTER (pAHCI_GlobalControlReg) & AHCI_GCR_globalReset_FLAG) {
		jiffie_diff = jiffies - old_jiffies;

		if (jiffie_diff > (HZ*2)) {
			PrintInfoRelease ("AHCI Reset failed after %u jiffies\n", jiffie_diff);
			return -1;
		}
	}
	
	PrintInfo ("Reset done after %u jiffies\n", jiffie_diff);
	return 0;
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/
AHCI_INLINE_PREFIX
void AHCI_HaltHost (void) {
#   if defined (CONFIG_AHCI_ACCESS_GCR_SAFE)
    unsigned OldValue = AHCI_READ_REGISTER (pAHCI_GlobalControlReg);
#   endif

	info ((AHCI_READ_REGISTER (pAHCI_InterruptStatusReg) & AHCI_ISR_hostHalted_FLAG) == 0);
	
	AHCI_WRITE_REGISTER (pAHCI_GlobalControlReg, OldValue | AHCI_GCR_haltHost_FLAG);
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/
AHCI_INLINE_PREFIX
void AHCI_RestartHost (void) {
#   if defined (CONFIG_AHCI_ACCESS_GCR_SAFE)
    unsigned OldValue = AHCI_READ_REGISTER (pAHCI_GlobalControlReg);
#   endif
    
	//Halt Flag muss schon gelöscht worden sein
	assert ((AHCI_READ_REGISTER (pAHCI_InterruptStatusReg) & AHCI_ISR_hostHalted_FLAG) == 0);

	AHCI_WRITE_REGISTER (pAHCI_GlobalControlReg, OldValue | AHCI_GCR_restartHost_FLAG);
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/
AHCI_INLINE_PREFIX
void AHCI_ConfigRxMemorySize (size_t rxBufferSize) {
	assert ((rxBufferSize & 0x3) == 0);
	assert (rxBufferSize < (1 << 18));
	
	AHCI_WRITE_REGISTER (pAHCI_MemoryConfigReg, rxBufferSize-4);
}

#endif //_ahcireg_h_
