Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 7 | */ |
| 8 | #include <linux/init.h> |
Andrew Bresticker | 4060bbe | 2014-10-20 12:03:53 -0700 | [diff] [blame] | 9 | #include <linux/irqchip/mips-gic.h> |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 10 | |
Maciej W. Rozycki | 8ff374b | 2013-09-17 16:58:10 +0100 | [diff] [blame] | 11 | #include <asm/cpu.h> |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 12 | #include <asm/setup.h> |
| 13 | #include <asm/time.h> |
| 14 | #include <asm/irq.h> |
| 15 | #include <asm/mips-boards/generic.h> |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 16 | |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 17 | static void __iomem *status_reg = (void __iomem *)0xbf000410; |
| 18 | |
| 19 | /* |
| 20 | * Estimate CPU frequency. Sets mips_hpt_frequency as a side-effect. |
| 21 | */ |
| 22 | static unsigned int __init estimate_cpu_frequency(void) |
| 23 | { |
Maciej W. Rozycki | 8ff374b | 2013-09-17 16:58:10 +0100 | [diff] [blame] | 24 | unsigned int prid = read_c0_prid() & (PRID_COMP_MASK | PRID_IMP_MASK); |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 25 | unsigned int tick = 0; |
| 26 | unsigned int freq; |
| 27 | unsigned int orig; |
| 28 | unsigned long flags; |
| 29 | |
| 30 | local_irq_save(flags); |
| 31 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 32 | orig = readl(status_reg) & 0x2; /* get original sample */ |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 33 | /* wait for transition */ |
| 34 | while ((readl(status_reg) & 0x2) == orig) |
| 35 | ; |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 36 | orig = orig ^ 0x2; /* flip the bit */ |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 37 | |
| 38 | write_c0_count(0); |
| 39 | |
| 40 | /* wait 1 second (the sampling clock transitions every 10ms) */ |
| 41 | while (tick < 100) { |
| 42 | /* wait for transition */ |
| 43 | while ((readl(status_reg) & 0x2) == orig) |
| 44 | ; |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 45 | orig = orig ^ 0x2; /* flip the bit */ |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 46 | tick++; |
| 47 | } |
| 48 | |
| 49 | freq = read_c0_count(); |
| 50 | |
| 51 | local_irq_restore(flags); |
| 52 | |
| 53 | mips_hpt_frequency = freq; |
| 54 | |
| 55 | /* Adjust for processor */ |
| 56 | if ((prid != (PRID_COMP_MIPS | PRID_IMP_20KC)) && |
| 57 | (prid != (PRID_COMP_MIPS | PRID_IMP_25KF))) |
| 58 | freq *= 2; |
| 59 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 60 | freq += 5000; /* rounding */ |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 61 | freq -= freq%10000; |
| 62 | |
| 63 | return freq ; |
| 64 | } |
| 65 | |
| 66 | void read_persistent_clock(struct timespec *ts) |
| 67 | { |
| 68 | ts->tv_sec = 0; |
| 69 | ts->tv_nsec = 0; |
| 70 | } |
| 71 | |
Andrew Bresticker | a669efc | 2014-09-18 14:47:12 -0700 | [diff] [blame] | 72 | int get_c0_perfcount_int(void) |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 73 | { |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 74 | if (gic_present) |
Niklas Cassel | 44923c9 | 2015-02-06 17:18:29 +0100 | [diff] [blame] | 75 | return gic_get_c0_perfcount_int(); |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 76 | if (cp0_perfcount_irq >= 0) |
| 77 | return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq; |
| 78 | return -1; |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 79 | } |
Felix Fietkau | 0cb0985 | 2015-07-23 18:59:52 +0200 | [diff] [blame] | 80 | EXPORT_SYMBOL_GPL(get_c0_perfcount_int); |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 81 | |
Paul Gortmaker | 078a55f | 2013-06-18 13:38:59 +0000 | [diff] [blame] | 82 | unsigned int get_c0_compare_int(void) |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 83 | { |
Andrew Bresticker | e9de688 | 2014-09-18 14:47:27 -0700 | [diff] [blame] | 84 | if (gic_present) |
| 85 | return gic_get_c0_compare_int(); |
| 86 | return MIPS_CPU_IRQ_BASE + cp0_compare_irq; |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void __init plat_time_init(void) |
| 90 | { |
| 91 | unsigned int est_freq; |
| 92 | |
| 93 | est_freq = estimate_cpu_frequency(); |
| 94 | |
| 95 | pr_debug("CPU frequency %d.%02d MHz\n", (est_freq / 1000000), |
| 96 | (est_freq % 1000000) * 100 / 1000000); |
| 97 | |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 98 | mips_scroll_message(); |
Steven J. Hill | 3070033 | 2012-05-30 21:02:49 +0000 | [diff] [blame] | 99 | } |