1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Clock event driver for the CS5535/CS5536
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2006, Advanced Micro Devices, Inc.
6*4882a593Smuzhiyun * Copyright (C) 2007 Andres Salomon <dilinger@debian.org>
7*4882a593Smuzhiyun * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk>
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * The MFGPTs are documented in AMD Geode CS5536 Companion Device Data Book.
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/irq.h>
14*4882a593Smuzhiyun #include <linux/interrupt.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/cs5535.h>
17*4882a593Smuzhiyun #include <linux/clockchips.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define DRV_NAME "cs5535-clockevt"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun static int timer_irq;
22*4882a593Smuzhiyun module_param_hw_named(irq, timer_irq, int, irq, 0644);
23*4882a593Smuzhiyun MODULE_PARM_DESC(irq, "Which IRQ to use for the clock source MFGPT ticks.");
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun * We are using the 32.768kHz input clock - it's the only one that has the
27*4882a593Smuzhiyun * ranges we find desirable. The following table lists the suitable
28*4882a593Smuzhiyun * divisors and the associated Hz, minimum interval and the maximum interval:
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * Divisor Hz Min Delta (s) Max Delta (s)
31*4882a593Smuzhiyun * 1 32768 .00048828125 2.000
32*4882a593Smuzhiyun * 2 16384 .0009765625 4.000
33*4882a593Smuzhiyun * 4 8192 .001953125 8.000
34*4882a593Smuzhiyun * 8 4096 .00390625 16.000
35*4882a593Smuzhiyun * 16 2048 .0078125 32.000
36*4882a593Smuzhiyun * 32 1024 .015625 64.000
37*4882a593Smuzhiyun * 64 512 .03125 128.000
38*4882a593Smuzhiyun * 128 256 .0625 256.000
39*4882a593Smuzhiyun * 256 128 .125 512.000
40*4882a593Smuzhiyun */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun static struct cs5535_mfgpt_timer *cs5535_event_clock;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /* Selected from the table above */
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #define MFGPT_DIVISOR 16
47*4882a593Smuzhiyun #define MFGPT_SCALE 4 /* divisor = 2^(scale) */
48*4882a593Smuzhiyun #define MFGPT_HZ (32768 / MFGPT_DIVISOR)
49*4882a593Smuzhiyun #define MFGPT_PERIODIC (MFGPT_HZ / HZ)
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun * The MFGPT timers on the CS5536 provide us with suitable timers to use
53*4882a593Smuzhiyun * as clock event sources - not as good as a HPET or APIC, but certainly
54*4882a593Smuzhiyun * better than the PIT. This isn't a general purpose MFGPT driver, but
55*4882a593Smuzhiyun * a simplified one designed specifically to act as a clock event source.
56*4882a593Smuzhiyun * For full details about the MFGPT, please consult the CS5536 data sheet.
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun
disable_timer(struct cs5535_mfgpt_timer * timer)59*4882a593Smuzhiyun static void disable_timer(struct cs5535_mfgpt_timer *timer)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun /* avoid races by clearing CMP1 and CMP2 unconditionally */
62*4882a593Smuzhiyun cs5535_mfgpt_write(timer, MFGPT_REG_SETUP,
63*4882a593Smuzhiyun (uint16_t) ~MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP1 |
64*4882a593Smuzhiyun MFGPT_SETUP_CMP2);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
start_timer(struct cs5535_mfgpt_timer * timer,uint16_t delta)67*4882a593Smuzhiyun static void start_timer(struct cs5535_mfgpt_timer *timer, uint16_t delta)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun cs5535_mfgpt_write(timer, MFGPT_REG_CMP2, delta);
70*4882a593Smuzhiyun cs5535_mfgpt_write(timer, MFGPT_REG_COUNTER, 0);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun cs5535_mfgpt_write(timer, MFGPT_REG_SETUP,
73*4882a593Smuzhiyun MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
mfgpt_shutdown(struct clock_event_device * evt)76*4882a593Smuzhiyun static int mfgpt_shutdown(struct clock_event_device *evt)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun disable_timer(cs5535_event_clock);
79*4882a593Smuzhiyun return 0;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
mfgpt_set_periodic(struct clock_event_device * evt)82*4882a593Smuzhiyun static int mfgpt_set_periodic(struct clock_event_device *evt)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun disable_timer(cs5535_event_clock);
85*4882a593Smuzhiyun start_timer(cs5535_event_clock, MFGPT_PERIODIC);
86*4882a593Smuzhiyun return 0;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
mfgpt_next_event(unsigned long delta,struct clock_event_device * evt)89*4882a593Smuzhiyun static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun start_timer(cs5535_event_clock, delta);
92*4882a593Smuzhiyun return 0;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun static struct clock_event_device cs5535_clockevent = {
96*4882a593Smuzhiyun .name = DRV_NAME,
97*4882a593Smuzhiyun .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
98*4882a593Smuzhiyun .set_state_shutdown = mfgpt_shutdown,
99*4882a593Smuzhiyun .set_state_periodic = mfgpt_set_periodic,
100*4882a593Smuzhiyun .set_state_oneshot = mfgpt_shutdown,
101*4882a593Smuzhiyun .tick_resume = mfgpt_shutdown,
102*4882a593Smuzhiyun .set_next_event = mfgpt_next_event,
103*4882a593Smuzhiyun .rating = 250,
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun
mfgpt_tick(int irq,void * dev_id)106*4882a593Smuzhiyun static irqreturn_t mfgpt_tick(int irq, void *dev_id)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun uint16_t val = cs5535_mfgpt_read(cs5535_event_clock, MFGPT_REG_SETUP);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /* See if the interrupt was for us */
111*4882a593Smuzhiyun if (!(val & (MFGPT_SETUP_SETUP | MFGPT_SETUP_CMP2 | MFGPT_SETUP_CMP1)))
112*4882a593Smuzhiyun return IRQ_NONE;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* Turn off the clock (and clear the event) */
115*4882a593Smuzhiyun disable_timer(cs5535_event_clock);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun if (clockevent_state_detached(&cs5535_clockevent) ||
118*4882a593Smuzhiyun clockevent_state_shutdown(&cs5535_clockevent))
119*4882a593Smuzhiyun return IRQ_HANDLED;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* Clear the counter */
122*4882a593Smuzhiyun cs5535_mfgpt_write(cs5535_event_clock, MFGPT_REG_COUNTER, 0);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /* Restart the clock in periodic mode */
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun if (clockevent_state_periodic(&cs5535_clockevent))
127*4882a593Smuzhiyun cs5535_mfgpt_write(cs5535_event_clock, MFGPT_REG_SETUP,
128*4882a593Smuzhiyun MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun cs5535_clockevent.event_handler(&cs5535_clockevent);
131*4882a593Smuzhiyun return IRQ_HANDLED;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
cs5535_mfgpt_init(void)134*4882a593Smuzhiyun static int __init cs5535_mfgpt_init(void)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun unsigned long flags = IRQF_NOBALANCING | IRQF_TIMER | IRQF_SHARED;
137*4882a593Smuzhiyun struct cs5535_mfgpt_timer *timer;
138*4882a593Smuzhiyun int ret;
139*4882a593Smuzhiyun uint16_t val;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
142*4882a593Smuzhiyun if (!timer) {
143*4882a593Smuzhiyun printk(KERN_ERR DRV_NAME ": Could not allocate MFGPT timer\n");
144*4882a593Smuzhiyun return -ENODEV;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun cs5535_event_clock = timer;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* Set up the IRQ on the MFGPT side */
149*4882a593Smuzhiyun if (cs5535_mfgpt_setup_irq(timer, MFGPT_CMP2, &timer_irq)) {
150*4882a593Smuzhiyun printk(KERN_ERR DRV_NAME ": Could not set up IRQ %d\n",
151*4882a593Smuzhiyun timer_irq);
152*4882a593Smuzhiyun goto err_timer;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* And register it with the kernel */
156*4882a593Smuzhiyun ret = request_irq(timer_irq, mfgpt_tick, flags, DRV_NAME, timer);
157*4882a593Smuzhiyun if (ret) {
158*4882a593Smuzhiyun printk(KERN_ERR DRV_NAME ": Unable to set up the interrupt.\n");
159*4882a593Smuzhiyun goto err_irq;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /* Set the clock scale and enable the event mode for CMP2 */
163*4882a593Smuzhiyun val = MFGPT_SCALE | (3 << 8);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun cs5535_mfgpt_write(cs5535_event_clock, MFGPT_REG_SETUP, val);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /* Set up the clock event */
168*4882a593Smuzhiyun printk(KERN_INFO DRV_NAME
169*4882a593Smuzhiyun ": Registering MFGPT timer as a clock event, using IRQ %d\n",
170*4882a593Smuzhiyun timer_irq);
171*4882a593Smuzhiyun clockevents_config_and_register(&cs5535_clockevent, MFGPT_HZ,
172*4882a593Smuzhiyun 0xF, 0xFFFE);
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun return 0;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun err_irq:
177*4882a593Smuzhiyun cs5535_mfgpt_release_irq(cs5535_event_clock, MFGPT_CMP2, &timer_irq);
178*4882a593Smuzhiyun err_timer:
179*4882a593Smuzhiyun cs5535_mfgpt_free_timer(cs5535_event_clock);
180*4882a593Smuzhiyun printk(KERN_ERR DRV_NAME ": Unable to set up the MFGPT clock source\n");
181*4882a593Smuzhiyun return -EIO;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun module_init(cs5535_mfgpt_init);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
187*4882a593Smuzhiyun MODULE_DESCRIPTION("CS5535/CS5536 MFGPT clock event driver");
188*4882a593Smuzhiyun MODULE_LICENSE("GPL");
189