162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
362306a36Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
462306a36Sopenharmony_ci * for more details.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 1998, 1999, 2003 by Ralf Baechle
762306a36Sopenharmony_ci * Copyright (C) 2014 by Maciej W. Rozycki
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci#ifndef _ASM_TIMEX_H
1062306a36Sopenharmony_ci#define _ASM_TIMEX_H
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#ifdef __KERNEL__
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/compiler.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include <asm/cpu.h>
1762306a36Sopenharmony_ci#include <asm/cpu-features.h>
1862306a36Sopenharmony_ci#include <asm/mipsregs.h>
1962306a36Sopenharmony_ci#include <asm/cpu-type.h>
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/*
2262306a36Sopenharmony_ci * This is the clock rate of the i8253 PIT.  A MIPS system may not have
2362306a36Sopenharmony_ci * a PIT by the symbol is used all over the kernel including some APIs.
2462306a36Sopenharmony_ci * So keeping it defined to the number for the PIT is the only sane thing
2562306a36Sopenharmony_ci * for now.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ci#define CLOCK_TICK_RATE 1193182
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci * Standard way to access the cycle counter.
3162306a36Sopenharmony_ci * Currently only used on SMP for scheduling.
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * Only the low 32 bits are available as a continuously counting entity.
3462306a36Sopenharmony_ci * But this only means we'll force a reschedule every 8 seconds or so,
3562306a36Sopenharmony_ci * which isn't an evil thing.
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * We know that all SMP capable CPUs have cycle counters.
3862306a36Sopenharmony_ci */
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_citypedef unsigned int cycles_t;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/*
4362306a36Sopenharmony_ci * On R4000/R4400 an erratum exists such that if the cycle counter is
4462306a36Sopenharmony_ci * read in the exact moment that it is matching the compare register,
4562306a36Sopenharmony_ci * no interrupt will be generated.
4662306a36Sopenharmony_ci *
4762306a36Sopenharmony_ci * There is a suggested workaround and also the erratum can't strike if
4862306a36Sopenharmony_ci * the compare interrupt isn't being used as the clock source device.
4962306a36Sopenharmony_ci * However for now the implementaton of this function doesn't get these
5062306a36Sopenharmony_ci * fine details right.
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_cistatic inline int can_use_mips_counter(unsigned int prid)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	int comp = (prid & PRID_COMP_MASK) != PRID_COMP_LEGACY;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	if (__builtin_constant_p(cpu_has_counter) && !cpu_has_counter)
5762306a36Sopenharmony_ci		return 0;
5862306a36Sopenharmony_ci	else if (__builtin_constant_p(cpu_has_mips_r) && cpu_has_mips_r)
5962306a36Sopenharmony_ci		return 1;
6062306a36Sopenharmony_ci	else if (likely(!__builtin_constant_p(cpu_has_mips_r) && comp))
6162306a36Sopenharmony_ci		return 1;
6262306a36Sopenharmony_ci	/* Make sure we don't peek at cpu_data[0].options in the fast path! */
6362306a36Sopenharmony_ci	if (!__builtin_constant_p(cpu_has_counter))
6462306a36Sopenharmony_ci		asm volatile("" : "=m" (cpu_data[0].options));
6562306a36Sopenharmony_ci	if (likely(cpu_has_counter &&
6662306a36Sopenharmony_ci		   prid > (PRID_IMP_R4000 | PRID_REV_ENCODE_44(15, 15))))
6762306a36Sopenharmony_ci		return 1;
6862306a36Sopenharmony_ci	else
6962306a36Sopenharmony_ci		return 0;
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistatic inline cycles_t get_cycles(void)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	if (can_use_mips_counter(read_c0_prid()))
7562306a36Sopenharmony_ci		return read_c0_count();
7662306a36Sopenharmony_ci	else
7762306a36Sopenharmony_ci		return 0;	/* no usable counter */
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci#define get_cycles get_cycles
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/*
8262306a36Sopenharmony_ci * Like get_cycles - but where c0_count is not available we desperately
8362306a36Sopenharmony_ci * use c0_random in an attempt to get at least a little bit of entropy.
8462306a36Sopenharmony_ci */
8562306a36Sopenharmony_cistatic inline unsigned long random_get_entropy(void)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	unsigned int c0_random;
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	if (can_use_mips_counter(read_c0_prid()))
9062306a36Sopenharmony_ci		return read_c0_count();
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	if (cpu_has_3kex)
9362306a36Sopenharmony_ci		c0_random = (read_c0_random() >> 8) & 0x3f;
9462306a36Sopenharmony_ci	else
9562306a36Sopenharmony_ci		c0_random = read_c0_random() & 0x3f;
9662306a36Sopenharmony_ci	return (random_get_entropy_fallback() << 6) | (0x3f - c0_random);
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci#define random_get_entropy random_get_entropy
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci#endif /* __KERNEL__ */
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#endif /*  _ASM_TIMEX_H */
103