19a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel/*
29a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel * include/asm-xtensa/delay.h
39a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel *
49a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel * This file is subject to the terms and conditions of the GNU General Public
59a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel * License.  See the file "COPYING" in the main directory of this archive
69a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel * for more details.
79a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel *
89a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel * Copyright (C) 2001 - 2005 Tensilica Inc.
99a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel *
109a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel */
119a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
129a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel#ifndef _XTENSA_DELAY_H
139a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel#define _XTENSA_DELAY_H
149a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
159a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel#include <asm/processor.h>
169a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel#include <asm/param.h>
179a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
189a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankelextern unsigned long loops_per_jiffy;
199a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
20d99cf715a0751b0c819cdd8616c8870c1dd51910Adrian Bunkstatic inline void __delay(unsigned long loops)
219a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel{
229a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel  /* 2 cycles per loop. */
239ec55a9bd365dfc78945bb8e6bf5d0fdf1d75ad0Chris Zankel  __asm__ __volatile__ ("1: addi %0, %0, -2; bgeui %0, 2, 1b"
249a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel			: "=r" (loops) : "0" (loops));
259a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel}
269a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
279a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankelstatic __inline__ u32 xtensa_get_ccount(void)
289a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel{
299a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel	u32 ccount;
309a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel	asm volatile ("rsr %0, 234; # CCOUNT\n" : "=r" (ccount));
319a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel	return ccount;
329a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel}
339a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
349a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel/* For SMP/NUMA systems, change boot_cpu_data to something like
359a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel * local_cpu_data->... where local_cpu_data points to the current
369a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel * cpu. */
379a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
389a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankelstatic __inline__ void udelay (unsigned long usecs)
399a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel{
409a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel	unsigned long start = xtensa_get_ccount();
419a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel	unsigned long cycles = usecs * (loops_per_jiffy / (1000000UL / HZ));
429a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
439a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel	/* Note: all variables are unsigned (can wrap around)! */
449a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel	while (((unsigned long)xtensa_get_ccount()) - start < cycles)
459a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel		;
469a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel}
479a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
489a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel#endif
499a8fd5589902153a134111ed7a40f9cca1f83254Chris Zankel
50