blob: 37e9413a393d30666521931d50a74b423feccabf [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright 2001 MontaVista Software Inc.
4 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
5 * Copyright (c) 2003, 2004 Maciej W. Rozycki
6 *
Atsushi Nemotod9eec1a2007-10-31 01:21:03 +09007 * Common time service routines for MIPS machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Ralf Baechle656db502007-10-26 13:24:06 +01009#include <linux/bug.h>
Ralf Baechle7bcf7712007-10-11 23:46:09 +010010#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/param.h>
16#include <linux/time.h>
17#include <linux/timex.h>
18#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/spinlock.h>
Paul Gortmaker73bc2562011-07-23 16:30:40 -040020#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/cpu-features.h>
Ralf Baechle69f24d12013-09-17 10:25:47 +020023#include <asm/cpu-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/time.h>
26
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * forward reference
29 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030DEFINE_SPINLOCK(rtc_lock);
Ralf Baechle4b550482007-10-11 23:46:08 +010031EXPORT_SYMBOL(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Dmitri Vorobiev46684732008-04-02 03:58:38 +040033static int null_perf_irq(void)
Ralf Baechleba339c02005-12-09 12:29:38 +000034{
35 return 0;
36}
37
David Howells7d12e782006-10-05 14:55:46 +010038int (*perf_irq)(void) = null_perf_irq;
Ralf Baechleba339c02005-12-09 12:29:38 +000039
Ralf Baechleba339c02005-12-09 12:29:38 +000040EXPORT_SYMBOL(perf_irq);
41
Chris Dearmanffe9ee42007-05-24 22:24:20 +010042/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * time_init() - it does the following things.
44 *
Ralf Baechle4b550482007-10-11 23:46:08 +010045 * 1) plat_time_init() -
Ralf Baechle70342282013-01-22 12:59:30 +010046 * a) (optional) set up RTC routines,
47 * b) (optional) calibrate and set the mips_hpt_frequency
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +090048 * (only needed if you intended to use cpu counter as timer interrupt
49 * source)
Ralf Baechle4b550482007-10-11 23:46:08 +010050 * 2) calculate a couple of cached variables for later usage
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053unsigned int mips_hpt_frequency;
James Hoganc992a4f2017-03-14 10:15:31 +000054EXPORT_SYMBOL_GPL(mips_hpt_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Ralf Baechle5aa85c92007-11-21 16:39:44 +000056static __init int cpu_has_mfc0_count_bug(void)
57{
58 switch (current_cpu_type()) {
59 case CPU_R4000PC:
60 case CPU_R4000SC:
61 case CPU_R4000MC:
62 /*
63 * V3.0 is documented as suffering from the mfc0 from count bug.
Ralf Baechle70342282013-01-22 12:59:30 +010064 * Afaik this is the last version of the R4000. Later versions
Ralf Baechle5aa85c92007-11-21 16:39:44 +000065 * were marketed as R4400.
66 */
67 return 1;
68
69 case CPU_R4400PC:
70 case CPU_R4400SC:
71 case CPU_R4400MC:
72 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -030073 * The published errata for the R4400 up to 3.0 say the CPU
Ralf Baechle5aa85c92007-11-21 16:39:44 +000074 * has the mfc0 from count bug.
75 */
76 if ((current_cpu_data.processor_id & 0xff) <= 0x30)
77 return 1;
78
79 /*
Thomas Bogendoerferce202cb2008-01-04 23:38:31 +010080 * we assume newer revisions are ok
Ralf Baechle5aa85c92007-11-21 16:39:44 +000081 */
Thomas Bogendoerferce202cb2008-01-04 23:38:31 +010082 return 0;
Ralf Baechle5aa85c92007-11-21 16:39:44 +000083 }
84
85 return 0;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088void __init time_init(void)
89{
Ralf Baechle4b550482007-10-11 23:46:08 +010090 plat_time_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Maciej W. Rozyckiafddce02013-09-03 01:29:58 +010092 /*
93 * The use of the R4k timer as a clock event takes precedence;
94 * if reading the Count register might interfere with the timer
95 * interrupt, then we don't use the timer as a clock source.
96 * We may still use the timer as a clock source though if the
97 * timer interrupt isn't reliable; the interference doesn't
98 * matter then, because we don't use the interrupt.
99 */
100 if (mips_clockevent_init() != 0 || !cpu_has_mfc0_count_bug())
Atsushi Nemotod9eec1a2007-10-31 01:21:03 +0900101 init_mips_clocksource();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}