#ifndef ICX1087_CYCLES_H
#define ICX1087_CYCLES_H
/*
 * icx1087_cycles.h -- Sony ICX1087 platform system cycles macro
 *
 * Copyright 2006 Sony Corporation.
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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
 *
 */

#ifdef CONFIG_ILATENCY
#include <asm/arch/timex.h>		/* get_cycles() */

#define ICX1087_SYSTEM_CLOCK    (11289600)  /* 88.577ns, 380.4sec in 32bit */

/* this macro has 2 or 3cycles overhead at 248MHz */
#define GetCycles()		((unsigned)get_cycles())
#define GET_CYCLES_OVERHEAD		(2)

/* this macro limit is 429496cycles (38043us=38ms) */
#define CalcMicroSec(cyc) \
	(((cyc) - GET_CYCLES_OVERHEAD) * (1000 * 1000 / 100) \
		/ (ICX1087_SYSTEM_CLOCK / 100))

/* this macro limit is 380.4sec */
#define CalcMilliSec(cyc) \
	(((cyc) - GET_CYCLES_OVERHEAD) / (ICX1087_SYSTEM_CLOCK / 1000))

#define Test_GetCycles() \
	do { \
		unsigned st, et; \
		st = GetCycles(); et = GetCycles(); \
		printk("GetCycles() overhead = %ucycles\n", et - st); \
		st = GetCycles();  udelay(1); et = GetCycles(); \
		printk("udelay(1)  : %uus, %ums\n", \
			CalcMicroSec(et - st), CalcMilliSec(et - st)); \
		st = GetCycles();  udelay(10); et = GetCycles(); \
		printk("udelay(10) : %uus, %ums\n", \
			CalcMicroSec(et - st), CalcMilliSec(et - st)); \
		st = GetCycles();  udelay(100); et = GetCycles(); \
		printk("udelay(100): %uus, %ums\n", \
			CalcMicroSec(et - st), CalcMilliSec(et - st)); \
		st = GetCycles();  mdelay(1); et = GetCycles(); \
		printk("mdelay(1)  : %uus, %ums\n", \
			CalcMicroSec(et - st), CalcMilliSec(et - st)); \
		st = GetCycles();  mdelay(2); et = GetCycles(); \
		printk("mdelay(2)  : %uus, %ums\n", \
			CalcMicroSec(et - st), CalcMilliSec(et - st)); \
		st = GetCycles();  mdelay(3); et = GetCycles(); \
		printk("mdelay(3)  : %uus, %ums\n", \
			CalcMicroSec(et - st), CalcMilliSec(et - st)); \
		st = GetCycles();  mdelay(10); et = GetCycles(); \
		printk("mdelay(10) : %uus, %ums\n", \
			CalcMicroSec(et - st), CalcMilliSec(et - st)); \
		st = GetCycles();  mdelay(40); et = GetCycles(); \
		printk("mdelay(40) : %uus(overflow), %ums\n", \
			CalcMicroSec(et - st), CalcMilliSec(et - st)); \
		st = GetCycles();  mdelay(100); et = GetCycles(); \
		printk("mdelay(100): %uus(overflow), %ums\n", \
			CalcMicroSec(et - st), CalcMilliSec(et - st)); \
	} while (0)

#else /* CONFIG_ILATENCY */
#define ICX1087_SYSTEM_CLOCK    (11289600)  /* 88.577ns, 380.4sec in 32bit */
/* this macro has 2 or 3cycles overhead at 248MHz */
#define GetCycles()		((unsigned)0)
#define GET_CYCLES_OVERHEAD		(2)
/* this macro limit is 429496cycles (38043us=38ms) */
#define CalcMicroSec(cyc) ((unsigned)0)

/* this macro limit is 380.4sec */
#define CalcMilliSec(cyc)  ((unsigned)0)

#define Test_GetCycles() \
      {\
          printk("Test_GetCycles() is not available\n"); \
      }
#endif /* CONFIG_ILATENCY */

#endif	/* ICX1087_CYCLES_H */
