blob: 855d0af470973878479094cc76c9f769ba784c83 [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
4/*
Greg Ungererb671b652006-06-26 10:33:10 +10005 * pit.c -- Freescale ColdFire PIT timer. Currently this type of
6 * hardware timer only exists in the Freescale ColdFire
Greg Ungerer8d80c5e2008-02-01 17:40:21 +10007 * 5270/5271, 5282 and 5208 CPUs. No doubt newer ColdFire
8 * family members will probably use it too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Greg Ungerer8d80c5e2008-02-01 17:40:21 +100010 * Copyright (C) 1999-2008, Greg Ungerer (gerg@snapgear.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright (C) 2001-2004, SnapGear Inc. (www.snapgear.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14/***************************************************************************/
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/sched.h>
18#include <linux/param.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
Greg Ungerer5c4525d2007-07-27 01:09:00 +100021#include <linux/irq.h>
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020022#include <linux/clockchips.h>
Greg Ungerer2f2c2672007-10-23 14:37:54 +100023#include <asm/machdep.h>
Greg Ungererb671b652006-06-26 10:33:10 +100024#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/coldfire.h>
26#include <asm/mcfpit.h>
27#include <asm/mcfsim.h>
28
29/***************************************************************************/
30
Greg Ungererb671b652006-06-26 10:33:10 +100031/*
32 * By default use timer1 as the system clock timer.
33 */
Greg Ungerer8d80c5e2008-02-01 17:40:21 +100034#define FREQ ((MCF_CLK / 2) / 64)
Greg Ungererf317c712011-03-05 23:32:35 +100035#define TA(a) (MCFPIT_BASE1 + (a))
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020036#define PIT_CYCLES_PER_JIFFY (FREQ / HZ)
Greg Ungerer8d80c5e2008-02-01 17:40:21 +100037
Greg Ungerer8d80c5e2008-02-01 17:40:21 +100038static u32 pit_cnt;
Greg Ungererb671b652006-06-26 10:33:10 +100039
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020040/*
41 * Initialize the PIT timer.
42 *
43 * This is also called after resume to bring the PIT into operation again.
44 */
45
Viresh Kumar5bbc08f2015-07-16 16:56:20 +053046static int cf_pit_set_periodic(struct clock_event_device *evt)
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020047{
Viresh Kumar5bbc08f2015-07-16 16:56:20 +053048 __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
49 __raw_writew(PIT_CYCLES_PER_JIFFY, TA(MCFPIT_PMR));
50 __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE |
51 MCFPIT_PCSR_OVW | MCFPIT_PCSR_RLD |
52 MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
53 return 0;
54}
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020055
Viresh Kumar5bbc08f2015-07-16 16:56:20 +053056static int cf_pit_set_oneshot(struct clock_event_device *evt)
57{
58 __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
59 __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE |
60 MCFPIT_PCSR_OVW | MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
61 return 0;
62}
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020063
Viresh Kumar5bbc08f2015-07-16 16:56:20 +053064static int cf_pit_shutdown(struct clock_event_device *evt)
65{
66 __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
67 return 0;
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020068}
69
70/*
71 * Program the next event in oneshot mode
72 *
73 * Delta is given in PIT ticks
74 */
75static int cf_pit_next_event(unsigned long delta,
76 struct clock_event_device *evt)
77{
78 __raw_writew(delta, TA(MCFPIT_PMR));
79 return 0;
80}
81
82struct clock_event_device cf_pit_clockevent = {
Viresh Kumar5bbc08f2015-07-16 16:56:20 +053083 .name = "pit",
84 .features = CLOCK_EVT_FEAT_PERIODIC |
85 CLOCK_EVT_FEAT_ONESHOT,
86 .set_state_shutdown = cf_pit_shutdown,
87 .set_state_periodic = cf_pit_set_periodic,
88 .set_state_oneshot = cf_pit_set_oneshot,
89 .set_next_event = cf_pit_next_event,
90 .shift = 32,
91 .irq = MCF_IRQ_PIT1,
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020092};
93
94
95
Greg Ungererb671b652006-06-26 10:33:10 +100096/***************************************************************************/
97
Greg Ungerer8d80c5e2008-02-01 17:40:21 +100098static irqreturn_t pit_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200100 struct clock_event_device *evt = &cf_pit_clockevent;
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000101 u16 pcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* Reset the ColdFire timer */
Greg Ungererb671b652006-06-26 10:33:10 +1000104 pcsr = __raw_readw(TA(MCFPIT_PCSR));
105 __raw_writew(pcsr | MCFPIT_PCSR_PIF, TA(MCFPIT_PCSR));
Greg Ungerer2f2c2672007-10-23 14:37:54 +1000106
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200107 pit_cnt += PIT_CYCLES_PER_JIFFY;
108 evt->event_handler(evt);
109 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/***************************************************************************/
113
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100114static u64 pit_read_clk(struct clocksource *cs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000116 unsigned long flags;
117 u32 cycles;
118 u16 pcntr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000120 local_irq_save(flags);
121 pcntr = __raw_readw(TA(MCFPIT_PCNTR));
122 cycles = pit_cnt;
123 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200125 return cycles + PIT_CYCLES_PER_JIFFY - pcntr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128/***************************************************************************/
129
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000130static struct clocksource pit_clk = {
131 .name = "pit",
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200132 .rating = 100,
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000133 .read = pit_read_clk,
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000134 .mask = CLOCKSOURCE_MASK(32),
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000135};
136
137/***************************************************************************/
138
Arnd Bergmannf9a01532020-09-24 17:29:17 +0200139void hw_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
afzal mohammedba000762020-03-01 06:56:55 +0530141 int ret;
142
Rusty Russell320ab2b2008-12-13 21:20:26 +1030143 cf_pit_clockevent.cpumask = cpumask_of(smp_processor_id());
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200144 cf_pit_clockevent.mult = div_sc(FREQ, NSEC_PER_SEC, 32);
145 cf_pit_clockevent.max_delta_ns =
146 clockevent_delta2ns(0xFFFF, &cf_pit_clockevent);
Nicolai Stange33ae7a92017-03-30 21:44:31 +0200147 cf_pit_clockevent.max_delta_ticks = 0xFFFF;
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200148 cf_pit_clockevent.min_delta_ns =
149 clockevent_delta2ns(0x3f, &cf_pit_clockevent);
Nicolai Stange33ae7a92017-03-30 21:44:31 +0200150 cf_pit_clockevent.min_delta_ticks = 0x3f;
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200151 clockevents_register_device(&cf_pit_clockevent);
152
afzal mohammedba000762020-03-01 06:56:55 +0530153 ret = request_irq(MCF_IRQ_PIT1, pit_tick, IRQF_TIMER, "timer", NULL);
154 if (ret) {
155 pr_err("Failed to request irq %d (timer): %pe\n", MCF_IRQ_PIT1,
156 ERR_PTR(ret));
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
John Stultz010f3f12010-04-26 20:21:52 -0700159 clocksource_register_hz(&pit_clk, FREQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162/***************************************************************************/