Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/plat-iop/time.c |
| 3 | * |
| 4 | * Timer code for IOP32x and IOP33x based systems |
| 5 | * |
| 6 | * Author: Deepak Saxena <dsaxena@mvista.com> |
| 7 | * |
| 8 | * Copyright 2002-2003 MontaVista Software Inc. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/time.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/timex.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
Mikael Pettersson | a91549a | 2009-10-29 11:46:54 -0700 | [diff] [blame^] | 22 | #include <linux/clocksource.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 23 | #include <mach/hardware.h> |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 24 | #include <asm/irq.h> |
| 25 | #include <asm/uaccess.h> |
| 26 | #include <asm/mach/irq.h> |
| 27 | #include <asm/mach/time.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 28 | #include <mach/time.h> |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 29 | |
Mikael Pettersson | a91549a | 2009-10-29 11:46:54 -0700 | [diff] [blame^] | 30 | /* |
| 31 | * IOP clocksource (free-running timer 1). |
| 32 | */ |
| 33 | static cycle_t iop_clocksource_read(struct clocksource *unused) |
| 34 | { |
| 35 | return 0xffffffffu - read_tcr1(); |
| 36 | } |
| 37 | |
| 38 | static struct clocksource iop_clocksource = { |
| 39 | .name = "iop_timer1", |
| 40 | .rating = 300, |
| 41 | .read = iop_clocksource_read, |
| 42 | .mask = CLOCKSOURCE_MASK(32), |
| 43 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 44 | }; |
| 45 | |
| 46 | static void __init iop_clocksource_set_hz(struct clocksource *cs, unsigned int hz) |
| 47 | { |
| 48 | u64 temp; |
| 49 | u32 shift; |
| 50 | |
| 51 | /* Find shift and mult values for hz. */ |
| 52 | shift = 32; |
| 53 | do { |
| 54 | temp = (u64) NSEC_PER_SEC << shift; |
| 55 | do_div(temp, hz); |
| 56 | if ((temp >> 32) == 0) |
| 57 | break; |
| 58 | } while (--shift != 0); |
| 59 | |
| 60 | cs->shift = shift; |
| 61 | cs->mult = (u32) temp; |
| 62 | |
| 63 | printk(KERN_INFO "clocksource: %s uses shift %u mult %#x\n", |
| 64 | cs->name, cs->shift, cs->mult); |
| 65 | } |
| 66 | |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 67 | static unsigned long ticks_per_jiffy; |
| 68 | static unsigned long ticks_per_usec; |
| 69 | static unsigned long next_jiffy_time; |
| 70 | |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 71 | unsigned long iop_gettimeoffset(void) |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 72 | { |
Dan Williams | 8903fcc | 2007-04-29 09:31:21 +0100 | [diff] [blame] | 73 | unsigned long offset, temp; |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 74 | |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 75 | /* enable cp6, if necessary, to avoid taking the overhead of an |
| 76 | * undefined instruction trap |
| 77 | */ |
| 78 | asm volatile ( |
| 79 | "mrc p15, 0, %0, c15, c1, 0\n\t" |
Dan Williams | 8903fcc | 2007-04-29 09:31:21 +0100 | [diff] [blame] | 80 | "tst %0, #(1 << 6)\n\t" |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 81 | "orreq %0, %0, #(1 << 6)\n\t" |
| 82 | "mcreq p15, 0, %0, c15, c1, 0\n\t" |
Dan Williams | 8903fcc | 2007-04-29 09:31:21 +0100 | [diff] [blame] | 83 | #ifdef CONFIG_CPU_XSCALE |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 84 | "mrceq p15, 0, %0, c15, c1, 0\n\t" |
| 85 | "moveq %0, %0\n\t" |
| 86 | "subeq pc, pc, #4\n\t" |
| 87 | #endif |
Dan Williams | 8903fcc | 2007-04-29 09:31:21 +0100 | [diff] [blame] | 88 | : "=r"(temp) : : "cc"); |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 89 | |
| 90 | offset = next_jiffy_time - read_tcr1(); |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 91 | |
| 92 | return offset / ticks_per_usec; |
| 93 | } |
| 94 | |
| 95 | static irqreturn_t |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 96 | iop_timer_interrupt(int irq, void *dev_id) |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 97 | { |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 98 | write_tisr(1); |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 99 | |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 100 | while ((signed long)(next_jiffy_time - read_tcr1()) |
| 101 | >= ticks_per_jiffy) { |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 102 | timer_tick(); |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 103 | next_jiffy_time -= ticks_per_jiffy; |
| 104 | } |
| 105 | |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 106 | return IRQ_HANDLED; |
| 107 | } |
| 108 | |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 109 | static struct irqaction iop_timer_irq = { |
| 110 | .name = "IOP Timer Tick", |
| 111 | .handler = iop_timer_interrupt, |
Bernhard Walle | b30faba | 2007-05-08 00:35:39 -0700 | [diff] [blame] | 112 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL, |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
Dan Williams | 70c14ff | 2007-07-20 02:07:26 +0100 | [diff] [blame] | 115 | static unsigned long iop_tick_rate; |
| 116 | unsigned long get_iop_tick_rate(void) |
| 117 | { |
| 118 | return iop_tick_rate; |
| 119 | } |
| 120 | EXPORT_SYMBOL(get_iop_tick_rate); |
| 121 | |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 122 | void __init iop_init_time(unsigned long tick_rate) |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 123 | { |
| 124 | u32 timer_ctl; |
| 125 | |
Julia Lawall | a692838 | 2009-08-02 10:46:45 +0200 | [diff] [blame] | 126 | ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ); |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 127 | ticks_per_usec = tick_rate / 1000000; |
| 128 | next_jiffy_time = 0xffffffff; |
Dan Williams | 70c14ff | 2007-07-20 02:07:26 +0100 | [diff] [blame] | 129 | iop_tick_rate = tick_rate; |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 130 | |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 131 | timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED | |
| 132 | IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1; |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * We use timer 0 for our timer interrupt, and timer 1 as |
| 136 | * monotonic counter for tracking missed jiffies. |
| 137 | */ |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 138 | write_trr0(ticks_per_jiffy - 1); |
| 139 | write_tmr0(timer_ctl); |
Mikael Pettersson | a91549a | 2009-10-29 11:46:54 -0700 | [diff] [blame^] | 140 | |
| 141 | /* |
| 142 | * Set up free-running clocksource timer 1. |
| 143 | */ |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 144 | write_trr1(0xffffffff); |
Mikael Pettersson | a91549a | 2009-10-29 11:46:54 -0700 | [diff] [blame^] | 145 | write_tcr1(0xffffffff); |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 146 | write_tmr1(timer_ctl); |
Mikael Pettersson | a91549a | 2009-10-29 11:46:54 -0700 | [diff] [blame^] | 147 | iop_clocksource_set_hz(&iop_clocksource, tick_rate); |
| 148 | clocksource_register(&iop_clocksource); |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 149 | |
Dan Williams | 3668b45 | 2007-02-13 17:13:34 +0100 | [diff] [blame] | 150 | setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq); |
Lennert Buytenhek | 48388b2 | 2006-09-18 23:18:16 +0100 | [diff] [blame] | 151 | } |