blob: 7ee14f41fc25dc20e0b5556c13e11df281b9ea76 [file] [log] [blame]
Thomas Bogendoerferc066a322006-12-28 18:22:32 +01001#include <linux/types.h>
Ralf Baechle334955e2011-06-01 19:04:57 +01002#include <linux/i8253.h>
Thomas Bogendoerferc066a322006-12-28 18:22:32 +01003#include <linux/interrupt.h>
David Howellsca4d3e672010-10-07 14:08:54 +01004#include <linux/irq.h>
Ralf Baechle631330f2009-06-19 14:05:26 +01005#include <linux/smp.h>
Thomas Bogendoerferc066a322006-12-28 18:22:32 +01006#include <linux/time.h>
Thomas Bogendoerferc9294022007-11-01 11:36:42 +01007#include <linux/clockchips.h>
Thomas Bogendoerferc066a322006-12-28 18:22:32 +01008
9#include <asm/sni.h>
10#include <asm/time.h>
11
Ralf Baechle70342282013-01-22 12:59:30 +010012#define SNI_CLOCK_TICK_RATE 3686400
13#define SNI_COUNTER2_DIV 64
14#define SNI_COUNTER0_DIV ((SNI_CLOCK_TICK_RATE / SNI_COUNTER2_DIV) / HZ)
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010015
Viresh Kumar397d08b2015-07-06 16:42:05 +053016static int a20r_set_periodic(struct clock_event_device *evt)
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010017{
Viresh Kumar397d08b2015-07-06 16:42:05 +053018 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 12) = 0x34;
19 wmb();
20 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 0) = SNI_COUNTER0_DIV;
21 wmb();
22 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 0) = SNI_COUNTER0_DIV >> 8;
23 wmb();
Ralf Baechle84953b32007-10-26 14:36:10 +010024
Viresh Kumar397d08b2015-07-06 16:42:05 +053025 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 12) = 0xb4;
26 wmb();
27 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 8) = SNI_COUNTER2_DIV;
28 wmb();
29 *(volatile u8 *)(A20R_PT_CLOCK_BASE + 8) = SNI_COUNTER2_DIV >> 8;
30 wmb();
31 return 0;
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010032}
33
Ralf Baechle84953b32007-10-26 14:36:10 +010034static struct clock_event_device a20r_clockevent_device = {
Viresh Kumar397d08b2015-07-06 16:42:05 +053035 .name = "a20r-timer",
36 .features = CLOCK_EVT_FEAT_PERIODIC,
Ralf Baechle84953b32007-10-26 14:36:10 +010037
38 /* .mult, .shift, .max_delta_ns and .min_delta_ns left uninitialized */
39
Viresh Kumar397d08b2015-07-06 16:42:05 +053040 .rating = 300,
41 .irq = SNI_A20R_IRQ_TIMER,
42 .set_state_periodic = a20r_set_periodic,
Ralf Baechle84953b32007-10-26 14:36:10 +010043};
44
45static irqreturn_t a20r_interrupt(int irq, void *dev_id)
46{
47 struct clock_event_device *cd = dev_id;
48
49 *(volatile u8 *)A20R_PT_TIM0_ACK = 0;
50 wmb();
51
52 cd->event_handler(cd);
53
54 return IRQ_HANDLED;
55}
56
57static struct irqaction a20r_irqaction = {
58 .handler = a20r_interrupt,
Yong Zhang8b5690f2011-11-22 14:38:03 +000059 .flags = IRQF_PERCPU | IRQF_TIMER,
Ralf Baechle84953b32007-10-26 14:36:10 +010060 .name = "a20r-timer",
61};
62
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010063/*
64 * a20r platform uses 2 counters to divide the input frequency.
65 * Counter 2 output is connected to Counter 0 & 1 input.
66 */
Ralf Baechle84953b32007-10-26 14:36:10 +010067static void __init sni_a20r_timer_setup(void)
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010068{
Ralf Baechle84953b32007-10-26 14:36:10 +010069 struct clock_event_device *cd = &a20r_clockevent_device;
70 struct irqaction *action = &a20r_irqaction;
71 unsigned int cpu = smp_processor_id();
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010072
Ralf Baechle70342282013-01-22 12:59:30 +010073 cd->cpumask = cpumask_of(cpu);
Thomas Bogendoerferc9294022007-11-01 11:36:42 +010074 clockevents_register_device(cd);
Ralf Baechle84953b32007-10-26 14:36:10 +010075 action->dev_id = cd;
76 setup_irq(SNI_A20R_IRQ_TIMER, &a20r_irqaction);
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010077}
78
Ralf Baechle70342282013-01-22 12:59:30 +010079#define SNI_8254_TICK_RATE 1193182UL
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010080
Ralf Baechle70342282013-01-22 12:59:30 +010081#define SNI_8254_TCSAMP_COUNTER ((SNI_8254_TICK_RATE / HZ) + 255)
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010082
83static __init unsigned long dosample(void)
84{
85 u32 ct0, ct1;
Ralf Baechle11b9d0e2011-03-29 11:57:11 +020086 volatile u8 msb;
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010087
88 /* Start the counter. */
Ralf Baechle49a89ef2007-10-11 23:46:15 +010089 outb_p(0x34, 0x43);
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010090 outb_p(SNI_8254_TCSAMP_COUNTER & 0xff, 0x40);
Ralf Baechle49a89ef2007-10-11 23:46:15 +010091 outb(SNI_8254_TCSAMP_COUNTER >> 8, 0x40);
Thomas Bogendoerferc066a322006-12-28 18:22:32 +010092
93 /* Get initial counter invariant */
94 ct0 = read_c0_count();
95
96 /* Latch and spin until top byte of counter0 is zero */
97 do {
Ralf Baechle49a89ef2007-10-11 23:46:15 +010098 outb(0x00, 0x43);
Ralf Baechle11b9d0e2011-03-29 11:57:11 +020099 (void) inb(0x40);
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100100 msb = inb(0x40);
Thomas Bogendoerferc066a322006-12-28 18:22:32 +0100101 ct1 = read_c0_count();
102 } while (msb);
103
104 /* Stop the counter. */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100105 outb(0x38, 0x43);
Thomas Bogendoerferc066a322006-12-28 18:22:32 +0100106 /*
107 * Return the difference, this is how far the r4k counter increments
108 * for every 1/HZ seconds. We round off the nearest 1 MHz of master
109 * clock (= 1000000 / HZ / 2).
110 */
111 /*return (ct1 - ct0 + (500000/HZ/2)) / (500000/HZ) * (500000/HZ);*/
112 return (ct1 - ct0) / (500000/HZ) * (500000/HZ);
113}
114
115/*
116 * Here we need to calibrate the cycle counter to at least be close.
117 */
Ralf Baechle4b550482007-10-11 23:46:08 +0100118void __init plat_time_init(void)
Thomas Bogendoerferc066a322006-12-28 18:22:32 +0100119{
120 unsigned long r4k_ticks[3];
121 unsigned long r4k_tick;
122
123 /*
124 * Figure out the r4k offset, the algorithm is very simple and works in
125 * _all_ cases as long as the 8254 counter register itself works ok (as
126 * an interrupt driving timer it does not because of bug, this is why
127 * we are using the onchip r4k counter/compare register to serve this
128 * purpose, but for r4k_offset calculation it will work ok for us).
129 * There are other very complicated ways of performing this calculation
130 * but this one works just fine so I am not going to futz around. ;-)
131 */
132 printk(KERN_INFO "Calibrating system timer... ");
133 dosample(); /* Prime cache. */
134 dosample(); /* Prime cache. */
135 /* Zero is NOT an option. */
136 do {
137 r4k_ticks[0] = dosample();
138 } while (!r4k_ticks[0]);
139 do {
140 r4k_ticks[1] = dosample();
141 } while (!r4k_ticks[1]);
142
143 if (r4k_ticks[0] != r4k_ticks[1]) {
144 printk("warning: timer counts differ, retrying... ");
145 r4k_ticks[2] = dosample();
146 if (r4k_ticks[2] == r4k_ticks[0]
147 || r4k_ticks[2] == r4k_ticks[1])
148 r4k_tick = r4k_ticks[2];
149 else {
150 printk("disagreement, using average... ");
151 r4k_tick = (r4k_ticks[0] + r4k_ticks[1]
152 + r4k_ticks[2]) / 3;
153 }
154 } else
155 r4k_tick = r4k_ticks[0];
156
157 printk("%d [%d.%04d MHz CPU]\n", (int) r4k_tick,
158 (int) (r4k_tick / (500000 / HZ)),
159 (int) (r4k_tick % (500000 / HZ)));
160
161 mips_hpt_frequency = r4k_tick * HZ;
Ralf Baechled865bea2007-10-11 23:46:10 +0100162
Thomas Bogendoerferc066a322006-12-28 18:22:32 +0100163 switch (sni_brd_type) {
164 case SNI_BRD_10:
165 case SNI_BRD_10NEW:
166 case SNI_BRD_TOWER_OASIC:
167 case SNI_BRD_MINITOWER:
Ralf Baechle84953b32007-10-26 14:36:10 +0100168 sni_a20r_timer_setup();
169 break;
Thomas Bogendoerferc066a322006-12-28 18:22:32 +0100170 }
Thomas Bogendoerfer231a35d2008-01-04 23:31:07 +0100171 setup_pit_timer();
Thomas Bogendoerferc066a322006-12-28 18:22:32 +0100172}
Ralf Baechle4b550482007-10-11 23:46:08 +0100173
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200174void read_persistent_clock(struct timespec *ts)
Ralf Baechle4b550482007-10-11 23:46:08 +0100175{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200176 ts->tv_sec = -1;
177 ts->tv_nsec = 0;
Ralf Baechle4b550482007-10-11 23:46:08 +0100178}