Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Nemoto | d9eec1a | 2007-10-31 01:21:03 +0900 | [diff] [blame] | 7 | * Common time service routines for MIPS machines. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
Ralf Baechle | 656db50 | 2007-10-26 13:24:06 +0100 | [diff] [blame] | 9 | #include <linux/bug.h> |
Ralf Baechle | 7bcf771 | 2007-10-11 23:46:09 +0100 | [diff] [blame] | 10 | #include <linux/clockchips.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/spinlock.h> |
Paul Gortmaker | 73bc256 | 2011-07-23 16:30:40 -0400 | [diff] [blame] | 20 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/cpu-features.h> |
Ralf Baechle | 69f24d1 | 2013-09-17 10:25:47 +0200 | [diff] [blame] | 23 | #include <asm/cpu-type.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/div64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/time.h> |
| 26 | |
| 27 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * forward reference |
| 29 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | DEFINE_SPINLOCK(rtc_lock); |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 31 | EXPORT_SYMBOL(rtc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Dmitri Vorobiev | 4668473 | 2008-04-02 03:58:38 +0400 | [diff] [blame] | 33 | static int null_perf_irq(void) |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 34 | { |
| 35 | return 0; |
| 36 | } |
| 37 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 38 | int (*perf_irq)(void) = null_perf_irq; |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 39 | |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 40 | EXPORT_SYMBOL(perf_irq); |
| 41 | |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 42 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * time_init() - it does the following things. |
| 44 | * |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 45 | * 1) plat_time_init() - |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 46 | * a) (optional) set up RTC routines, |
| 47 | * b) (optional) calibrate and set the mips_hpt_frequency |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 48 | * (only needed if you intended to use cpu counter as timer interrupt |
| 49 | * source) |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 50 | * 2) calculate a couple of cached variables for later usage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | */ |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | unsigned int mips_hpt_frequency; |
James Hogan | c992a4f | 2017-03-14 10:15:31 +0000 | [diff] [blame] | 54 | EXPORT_SYMBOL_GPL(mips_hpt_frequency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Ralf Baechle | 5aa85c9 | 2007-11-21 16:39:44 +0000 | [diff] [blame] | 56 | static __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 Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 64 | * Afaik this is the last version of the R4000. Later versions |
Ralf Baechle | 5aa85c9 | 2007-11-21 16:39:44 +0000 | [diff] [blame] | 65 | * were marketed as R4400. |
| 66 | */ |
| 67 | return 1; |
| 68 | |
| 69 | case CPU_R4400PC: |
| 70 | case CPU_R4400SC: |
| 71 | case CPU_R4400MC: |
| 72 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 73 | * The published errata for the R4400 up to 3.0 say the CPU |
Ralf Baechle | 5aa85c9 | 2007-11-21 16:39:44 +0000 | [diff] [blame] | 74 | * has the mfc0 from count bug. |
| 75 | */ |
| 76 | if ((current_cpu_data.processor_id & 0xff) <= 0x30) |
| 77 | return 1; |
| 78 | |
| 79 | /* |
Thomas Bogendoerfer | ce202cb | 2008-01-04 23:38:31 +0100 | [diff] [blame] | 80 | * we assume newer revisions are ok |
Ralf Baechle | 5aa85c9 | 2007-11-21 16:39:44 +0000 | [diff] [blame] | 81 | */ |
Thomas Bogendoerfer | ce202cb | 2008-01-04 23:38:31 +0100 | [diff] [blame] | 82 | return 0; |
Ralf Baechle | 5aa85c9 | 2007-11-21 16:39:44 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | void __init time_init(void) |
| 89 | { |
Ralf Baechle | 4b55048 | 2007-10-11 23:46:08 +0100 | [diff] [blame] | 90 | plat_time_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Maciej W. Rozycki | afddce0 | 2013-09-03 01:29:58 +0100 | [diff] [blame] | 92 | /* |
| 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 Nemoto | d9eec1a | 2007-10-31 01:21:03 +0900 | [diff] [blame] | 101 | init_mips_clocksource(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |