| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  linux/arch/m32r/kernel/time.c | 
 | 3 |  * | 
 | 4 |  *  Copyright (c) 2001, 2002  Hiroyuki Kondo, Hirokazu Takata, | 
 | 5 |  *                            Hitoshi Yamamoto | 
 | 6 |  *  Taken from i386 version. | 
 | 7 |  *    Copyright (C) 1991, 1992, 1995  Linus Torvalds | 
 | 8 |  *    Copyright (C) 1996, 1997, 1998  Ralf Baechle | 
 | 9 |  * | 
 | 10 |  *  This file contains the time handling details for PC-style clocks as | 
 | 11 |  *  found in some MIPS systems. | 
 | 12 |  * | 
 | 13 |  *  Some code taken from sh version. | 
 | 14 |  *    Copyright (C) 1999  Tetsuya Okada & Niibe Yutaka | 
 | 15 |  *    Copyright (C) 2000  Philipp Rumpf <prumpf@tux.org> | 
 | 16 |  */ | 
 | 17 |  | 
 | 18 | #undef  DEBUG_TIMER | 
 | 19 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/errno.h> | 
 | 21 | #include <linux/init.h> | 
 | 22 | #include <linux/module.h> | 
 | 23 | #include <linux/sched.h> | 
 | 24 | #include <linux/kernel.h> | 
 | 25 | #include <linux/param.h> | 
 | 26 | #include <linux/string.h> | 
 | 27 | #include <linux/mm.h> | 
 | 28 | #include <linux/interrupt.h> | 
 | 29 | #include <linux/profile.h> | 
 | 30 |  | 
 | 31 | #include <asm/io.h> | 
 | 32 | #include <asm/m32r.h> | 
 | 33 |  | 
 | 34 | #include <asm/hw_irq.h> | 
 | 35 |  | 
| Hirokazu Takata | bac33bd | 2009-08-26 13:13:12 +0900 | [diff] [blame] | 36 | #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) | 
 | 37 | /* this needs a better home */ | 
 | 38 | DEFINE_SPINLOCK(rtc_lock); | 
 | 39 |  | 
 | 40 | #ifdef CONFIG_RTC_DRV_CMOS_MODULE | 
 | 41 | EXPORT_SYMBOL(rtc_lock); | 
 | 42 | #endif | 
 | 43 | #endif  /* pc-style 'CMOS' RTC support */ | 
 | 44 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #ifdef CONFIG_SMP | 
| Al Viro | 9c8e7f5 | 2006-10-07 16:29:18 +0100 | [diff] [blame] | 46 | extern void smp_local_timer_interrupt(void); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #endif | 
 | 48 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #define TICK_SIZE	(tick_nsec / 1000) | 
 | 50 |  | 
 | 51 | /* | 
 | 52 |  * Change this if you have some constant time drift | 
 | 53 |  */ | 
 | 54 |  | 
 | 55 | /* This is for machines which generate the exact clock. */ | 
 | 56 | #define USECS_PER_JIFFY (1000000/HZ) | 
 | 57 |  | 
 | 58 | static unsigned long latch; | 
 | 59 |  | 
| john stultz | 95ad759 | 2009-09-21 17:04:04 -0700 | [diff] [blame] | 60 | u32 arch_gettimeoffset(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { | 
 | 62 | 	unsigned long  elapsed_time = 0;  /* [us] */ | 
 | 63 |  | 
 | 64 | #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \ | 
 | 65 | 	|| defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \ | 
| Hirokazu Takata | 9287d95 | 2006-01-06 00:18:41 -0800 | [diff] [blame] | 66 | 	|| defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #ifndef CONFIG_SMP | 
 | 68 |  | 
 | 69 | 	unsigned long count; | 
 | 70 |  | 
 | 71 | 	/* timer count may underflow right here */ | 
 | 72 | 	count = inl(M32R_MFT2CUT_PORTL); | 
 | 73 |  | 
 | 74 | 	if (inl(M32R_ICU_CR18_PORTL) & 0x00000100)	/* underflow check */ | 
 | 75 | 		count = 0; | 
 | 76 |  | 
 | 77 | 	count = (latch - count) * TICK_SIZE; | 
| Julia Lawall | 5602358 | 2009-08-02 10:47:27 +0200 | [diff] [blame] | 78 | 	elapsed_time = DIV_ROUND_CLOSEST(count, latch); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | 	/* NOTE: LATCH is equal to the "interval" value (= reload count). */ | 
 | 80 |  | 
 | 81 | #else /* CONFIG_SMP */ | 
 | 82 | 	unsigned long count; | 
 | 83 | 	static unsigned long p_jiffies = -1; | 
 | 84 | 	static unsigned long p_count = 0; | 
 | 85 |  | 
 | 86 | 	/* timer count may underflow right here */ | 
 | 87 | 	count = inl(M32R_MFT2CUT_PORTL); | 
 | 88 |  | 
 | 89 | 	if (jiffies == p_jiffies && count > p_count) | 
 | 90 | 		count = 0; | 
 | 91 |  | 
 | 92 | 	p_jiffies = jiffies; | 
 | 93 | 	p_count = count; | 
 | 94 |  | 
 | 95 | 	count = (latch - count) * TICK_SIZE; | 
| Julia Lawall | 5602358 | 2009-08-02 10:47:27 +0200 | [diff] [blame] | 96 | 	elapsed_time = DIV_ROUND_CLOSEST(count, latch); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | 	/* NOTE: LATCH is equal to the "interval" value (= reload count). */ | 
 | 98 | #endif /* CONFIG_SMP */ | 
 | 99 | #elif defined(CONFIG_CHIP_M32310) | 
 | 100 | #warning do_gettimeoffse not implemented | 
 | 101 | #else | 
 | 102 | #error no chip configuration | 
 | 103 | #endif | 
 | 104 |  | 
| john stultz | 95ad759 | 2009-09-21 17:04:04 -0700 | [diff] [blame] | 105 | 	return elapsed_time * 1000; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } | 
 | 107 |  | 
 | 108 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 |  * timer_interrupt() needs to keep up the real-time clock, | 
 | 110 |  * as well as call the "do_timer()" routine every clocktick | 
 | 111 |  */ | 
| Adrian Bunk | 81e4807 | 2008-09-24 15:01:47 +0900 | [diff] [blame] | 112 | static irqreturn_t timer_interrupt(int irq, void *dev_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { | 
 | 114 | #ifndef CONFIG_SMP | 
| Al Viro | 9c8e7f5 | 2006-10-07 16:29:18 +0100 | [diff] [blame] | 115 | 	profile_tick(CPU_PROFILING); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | #endif | 
| john stultz | 95ad759 | 2009-09-21 17:04:04 -0700 | [diff] [blame] | 117 | 	/* XXX FIXME. Uh, the xtime_lock should be held here, no? */ | 
| Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 118 | 	do_timer(1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 |  | 
 | 120 | #ifndef CONFIG_SMP | 
| Al Viro | 9c8e7f5 | 2006-10-07 16:29:18 +0100 | [diff] [blame] | 121 | 	update_process_times(user_mode(get_irq_regs())); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | 	/* 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 Viro | 9c8e7f5 | 2006-10-07 16:29:18 +0100 | [diff] [blame] | 129 | 	smp_local_timer_interrupt(); | 
| Hirokazu Takata | 2757a71 | 2005-08-01 21:11:35 -0700 | [diff] [blame] | 130 | 	smp_send_timer(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 |  | 
 | 133 | 	return IRQ_HANDLED; | 
 | 134 | } | 
 | 135 |  | 
| Adrian Bunk | 81e4807 | 2008-09-24 15:01:47 +0900 | [diff] [blame] | 136 | static struct irqaction irq0 = { | 
| Thomas Gleixner | 977676a | 2007-10-16 01:26:36 -0700 | [diff] [blame] | 137 | 	.handler = timer_interrupt, | 
 | 138 | 	.flags = IRQF_DISABLED, | 
| Thomas Gleixner | 977676a | 2007-10-16 01:26:36 -0700 | [diff] [blame] | 139 | 	.name = "MFT2", | 
 | 140 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 |  | 
| John Stultz | 9446947 | 2010-03-03 19:57:25 -0800 | [diff] [blame] | 142 | void read_persistent_clock(struct timespec *ts) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { | 
 | 144 | 	unsigned int epoch, year, mon, day, hour, min, sec; | 
 | 145 |  | 
 | 146 | 	sec = min = hour = day = mon = year = 0; | 
 | 147 | 	epoch = 0; | 
 | 148 |  | 
 | 149 | 	year = 23; | 
 | 150 | 	mon = 4; | 
 | 151 | 	day = 17; | 
 | 152 |  | 
 | 153 | 	/* Attempt to guess the epoch.  This is the same heuristic as in rtc.c | 
 | 154 | 	   so no stupid things will happen to timekeeping.  Who knows, maybe | 
 | 155 | 	   Ultrix also uses 1952 as epoch ...  */ | 
 | 156 | 	if (year > 10 && year < 44) | 
 | 157 | 		epoch = 1980; | 
 | 158 | 	else if (year < 96) | 
 | 159 | 		epoch = 1952; | 
 | 160 | 	year += epoch; | 
 | 161 |  | 
| John Stultz | 9446947 | 2010-03-03 19:57:25 -0800 | [diff] [blame] | 162 | 	ts->tv_sec = mktime(year, mon, day, hour, min, sec); | 
 | 163 | 	ts->tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ); | 
 | 164 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 |  | 
| John Stultz | 9446947 | 2010-03-03 19:57:25 -0800 | [diff] [blame] | 166 |  | 
 | 167 | void __init time_init(void) | 
 | 168 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \ | 
 | 170 | 	|| defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \ | 
| Hirokazu Takata | 9287d95 | 2006-01-06 00:18:41 -0800 | [diff] [blame] | 171 | 	|| defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 |  | 
 | 173 | 	/* M32102 MFT setup */ | 
 | 174 | 	setup_irq(M32R_IRQ_MFT2, &irq0); | 
 | 175 | 	{ | 
 | 176 | 		unsigned long bus_clock; | 
 | 177 | 		unsigned short divide; | 
 | 178 |  | 
 | 179 | 		bus_clock = boot_cpu_data.bus_clock; | 
 | 180 | 		divide = boot_cpu_data.timer_divide; | 
| Julia Lawall | 5602358 | 2009-08-02 10:47:27 +0200 | [diff] [blame] | 181 | 		latch = DIV_ROUND_CLOSEST(bus_clock/divide, HZ); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 |  | 
 | 183 | 		printk("Timer start : latch = %ld\n", latch); | 
 | 184 |  | 
 | 185 | 		outl((M32R_MFTMOD_CC_MASK | M32R_MFTMOD_TCCR \ | 
 | 186 | 			|M32R_MFTMOD_CSSEL011), M32R_MFT2MOD_PORTL); | 
 | 187 | 		outl(latch, M32R_MFT2RLD_PORTL); | 
 | 188 | 		outl(latch, M32R_MFT2CUT_PORTL); | 
 | 189 | 		outl(0, M32R_MFT2CMPRLD_PORTL); | 
 | 190 | 		outl((M32R_MFTCR_MFT2MSK|M32R_MFTCR_MFT2EN), M32R_MFTCR_PORTL); | 
 | 191 | 	} | 
 | 192 |  | 
 | 193 | #elif defined(CONFIG_CHIP_M32310) | 
 | 194 | #warning time_init not implemented | 
 | 195 | #else | 
 | 196 | #error no chip configuration | 
 | 197 | #endif | 
 | 198 | } |