blob: 521749fbbb564f084beb4799e4cbaa09f22d57d2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/arch/m32r/kernel/time.c
4 *
5 * Copyright (c) 2001, 2002 Hiroyuki Kondo, Hirokazu Takata,
6 * Hitoshi Yamamoto
7 * Taken from i386 version.
8 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
9 * Copyright (C) 1996, 1997, 1998 Ralf Baechle
10 *
11 * This file contains the time handling details for PC-style clocks as
12 * found in some MIPS systems.
13 *
14 * Some code taken from sh version.
15 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
16 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
17 */
18
19#undef DEBUG_TIMER
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/sched.h>
25#include <linux/kernel.h>
26#include <linux/param.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/interrupt.h>
30#include <linux/profile.h>
31
32#include <asm/io.h>
33#include <asm/m32r.h>
34
35#include <asm/hw_irq.h>
36
Hirokazu Takatabac33bd2009-08-26 13:13:12 +090037#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
38/* this needs a better home */
39DEFINE_SPINLOCK(rtc_lock);
40
41#ifdef CONFIG_RTC_DRV_CMOS_MODULE
42EXPORT_SYMBOL(rtc_lock);
43#endif
44#endif /* pc-style 'CMOS' RTC support */
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#ifdef CONFIG_SMP
Al Viro9c8e7f52006-10-07 16:29:18 +010047extern void smp_local_timer_interrupt(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#endif
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define TICK_SIZE (tick_nsec / 1000)
51
52/*
53 * Change this if you have some constant time drift
54 */
55
56/* This is for machines which generate the exact clock. */
57#define USECS_PER_JIFFY (1000000/HZ)
58
59static unsigned long latch;
60
Stephen Warren7b1f6202012-11-07 17:58:54 -070061static u32 m32r_gettimeoffset(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 unsigned long elapsed_time = 0; /* [us] */
64
65#if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
66 || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
Hirokazu Takata9287d952006-01-06 00:18:41 -080067 || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#ifndef CONFIG_SMP
69
70 unsigned long count;
71
72 /* timer count may underflow right here */
73 count = inl(M32R_MFT2CUT_PORTL);
74
75 if (inl(M32R_ICU_CR18_PORTL) & 0x00000100) /* underflow check */
76 count = 0;
77
78 count = (latch - count) * TICK_SIZE;
Julia Lawall56023582009-08-02 10:47:27 +020079 elapsed_time = DIV_ROUND_CLOSEST(count, latch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /* NOTE: LATCH is equal to the "interval" value (= reload count). */
81
82#else /* CONFIG_SMP */
83 unsigned long count;
84 static unsigned long p_jiffies = -1;
85 static unsigned long p_count = 0;
86
87 /* timer count may underflow right here */
88 count = inl(M32R_MFT2CUT_PORTL);
89
90 if (jiffies == p_jiffies && count > p_count)
91 count = 0;
92
93 p_jiffies = jiffies;
94 p_count = count;
95
96 count = (latch - count) * TICK_SIZE;
Julia Lawall56023582009-08-02 10:47:27 +020097 elapsed_time = DIV_ROUND_CLOSEST(count, latch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* NOTE: LATCH is equal to the "interval" value (= reload count). */
99#endif /* CONFIG_SMP */
100#elif defined(CONFIG_CHIP_M32310)
101#warning do_gettimeoffse not implemented
102#else
103#error no chip configuration
104#endif
105
john stultz95ad7592009-09-21 17:04:04 -0700106 return elapsed_time * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * timer_interrupt() needs to keep up the real-time clock,
Torben Hohn7bde2ab72011-01-27 16:00:01 +0100111 * as well as call the "xtime_update()" routine every clocktick
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Adrian Bunk81e48072008-09-24 15:01:47 +0900113static irqreturn_t timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115#ifndef CONFIG_SMP
Al Viro9c8e7f52006-10-07 16:29:18 +0100116 profile_tick(CPU_PROFILING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#endif
Torben Hohn7bde2ab72011-01-27 16:00:01 +0100118 xtime_update(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#ifndef CONFIG_SMP
Al Viro9c8e7f52006-10-07 16:29:18 +0100121 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* As we return to user mode fire off the other CPU schedulers..
124 this is basically because we don't yet share IRQ's around.
125 This message is rigged to be safe on the 386 - basically it's
126 a hack, so don't look closely for now.. */
127
128#ifdef CONFIG_SMP
Al Viro9c8e7f52006-10-07 16:29:18 +0100129 smp_local_timer_interrupt();
Hirokazu Takata2757a712005-08-01 21:11:35 -0700130 smp_send_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 return IRQ_HANDLED;
134}
135
Adrian Bunk81e48072008-09-24 15:01:47 +0900136static struct irqaction irq0 = {
Thomas Gleixner977676a2007-10-16 01:26:36 -0700137 .handler = timer_interrupt,
Thomas Gleixner977676a2007-10-16 01:26:36 -0700138 .name = "MFT2",
139};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
John Stultz94469472010-03-03 19:57:25 -0800141void read_persistent_clock(struct timespec *ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 unsigned int epoch, year, mon, day, hour, min, sec;
144
145 sec = min = hour = day = mon = year = 0;
146 epoch = 0;
147
148 year = 23;
149 mon = 4;
150 day = 17;
151
152 /* Attempt to guess the epoch. This is the same heuristic as in rtc.c
153 so no stupid things will happen to timekeeping. Who knows, maybe
154 Ultrix also uses 1952 as epoch ... */
155 if (year > 10 && year < 44)
156 epoch = 1980;
157 else if (year < 96)
158 epoch = 1952;
159 year += epoch;
160
John Stultz94469472010-03-03 19:57:25 -0800161 ts->tv_sec = mktime(year, mon, day, hour, min, sec);
162 ts->tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
John Stultz94469472010-03-03 19:57:25 -0800165
166void __init time_init(void)
167{
Stephen Warren7b1f6202012-11-07 17:58:54 -0700168 arch_gettimeoffset = m32r_gettimeoffset;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
171 || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
Hirokazu Takata9287d952006-01-06 00:18:41 -0800172 || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* M32102 MFT setup */
175 setup_irq(M32R_IRQ_MFT2, &irq0);
176 {
177 unsigned long bus_clock;
178 unsigned short divide;
179
180 bus_clock = boot_cpu_data.bus_clock;
181 divide = boot_cpu_data.timer_divide;
Julia Lawall56023582009-08-02 10:47:27 +0200182 latch = DIV_ROUND_CLOSEST(bus_clock/divide, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 printk("Timer start : latch = %ld\n", latch);
185
186 outl((M32R_MFTMOD_CC_MASK | M32R_MFTMOD_TCCR \
187 |M32R_MFTMOD_CSSEL011), M32R_MFT2MOD_PORTL);
188 outl(latch, M32R_MFT2RLD_PORTL);
189 outl(latch, M32R_MFT2CUT_PORTL);
190 outl(0, M32R_MFT2CMPRLD_PORTL);
191 outl((M32R_MFTCR_MFT2MSK|M32R_MFTCR_MFT2EN), M32R_MFTCR_PORTL);
192 }
193
194#elif defined(CONFIG_CHIP_M32310)
195#warning time_init not implemented
196#else
197#error no chip configuration
198#endif
199}