Blame view

drivers/clocksource/timer-cs5535.c 5.58 KB
25763b3c8   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
c30d7d2b9   Andres Salomon   cs5535: add a gen...
2
3
4
5
6
7
8
  /*
   * Clock event driver for the CS5535/CS5536
   *
   * Copyright (C) 2006, Advanced Micro Devices, Inc.
   * Copyright (C) 2007  Andres Salomon <dilinger@debian.org>
   * Copyright (C) 2009  Andres Salomon <dilinger@collabora.co.uk>
   *
c30d7d2b9   Andres Salomon   cs5535: add a gen...
9
10
11
12
13
14
15
16
17
18
19
   * The MFGPTs are documented in AMD Geode CS5536 Companion Device Data Book.
   */
  
  #include <linux/kernel.h>
  #include <linux/irq.h>
  #include <linux/interrupt.h>
  #include <linux/module.h>
  #include <linux/cs5535.h>
  #include <linux/clockchips.h>
  
  #define DRV_NAME "cs5535-clockevt"
115079aad   Jens Rottmann   geode-mfgpt: rest...
20
  static int timer_irq;
cc9c61755   David Howells   Annotate hardware...
21
  module_param_hw_named(irq, timer_irq, int, irq, 0644);
c30d7d2b9   Andres Salomon   cs5535: add a gen...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  MODULE_PARM_DESC(irq, "Which IRQ to use for the clock source MFGPT ticks.");
  
  /*
   * We are using the 32.768kHz input clock - it's the only one that has the
   * ranges we find desirable.  The following table lists the suitable
   * divisors and the associated Hz, minimum interval and the maximum interval:
   *
   *  Divisor   Hz      Min Delta (s)  Max Delta (s)
   *   1        32768   .00048828125      2.000
   *   2        16384   .0009765625       4.000
   *   4         8192   .001953125        8.000
   *   8         4096   .00390625        16.000
   *   16        2048   .0078125         32.000
   *   32        1024   .015625          64.000
   *   64         512   .03125          128.000
   *  128         256   .0625           256.000
   *  256         128   .125            512.000
   */
c30d7d2b9   Andres Salomon   cs5535: add a gen...
40
41
42
43
44
45
46
47
48
49
  static struct cs5535_mfgpt_timer *cs5535_event_clock;
  
  /* Selected from the table above */
  
  #define MFGPT_DIVISOR 16
  #define MFGPT_SCALE  4     /* divisor = 2^(scale) */
  #define MFGPT_HZ  (32768 / MFGPT_DIVISOR)
  #define MFGPT_PERIODIC (MFGPT_HZ / HZ)
  
  /*
61e01be22   Jens Rottmann   cs5535-clockevt: ...
50
   * The MFGPT timers on the CS5536 provide us with suitable timers to use
c30d7d2b9   Andres Salomon   cs5535: add a gen...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
   * as clock event sources - not as good as a HPET or APIC, but certainly
   * better than the PIT.  This isn't a general purpose MFGPT driver, but
   * a simplified one designed specifically to act as a clock event source.
   * For full details about the MFGPT, please consult the CS5536 data sheet.
   */
  
  static void disable_timer(struct cs5535_mfgpt_timer *timer)
  {
  	/* avoid races by clearing CMP1 and CMP2 unconditionally */
  	cs5535_mfgpt_write(timer, MFGPT_REG_SETUP,
  			(uint16_t) ~MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP1 |
  				MFGPT_SETUP_CMP2);
  }
  
  static void start_timer(struct cs5535_mfgpt_timer *timer, uint16_t delta)
  {
  	cs5535_mfgpt_write(timer, MFGPT_REG_CMP2, delta);
  	cs5535_mfgpt_write(timer, MFGPT_REG_COUNTER, 0);
  
  	cs5535_mfgpt_write(timer, MFGPT_REG_SETUP,
  			MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
  }
8f9327cbb   Viresh Kumar   clockevents/drive...
73
  static int mfgpt_shutdown(struct clock_event_device *evt)
c30d7d2b9   Andres Salomon   cs5535: add a gen...
74
75
  {
  	disable_timer(cs5535_event_clock);
8f9327cbb   Viresh Kumar   clockevents/drive...
76
77
  	return 0;
  }
c30d7d2b9   Andres Salomon   cs5535: add a gen...
78

8f9327cbb   Viresh Kumar   clockevents/drive...
79
80
81
82
83
  static int mfgpt_set_periodic(struct clock_event_device *evt)
  {
  	disable_timer(cs5535_event_clock);
  	start_timer(cs5535_event_clock, MFGPT_PERIODIC);
  	return 0;
c30d7d2b9   Andres Salomon   cs5535: add a gen...
84
85
86
87
88
89
90
91
92
93
94
  }
  
  static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
  {
  	start_timer(cs5535_event_clock, delta);
  	return 0;
  }
  
  static struct clock_event_device cs5535_clockevent = {
  	.name = DRV_NAME,
  	.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
8f9327cbb   Viresh Kumar   clockevents/drive...
95
96
97
98
  	.set_state_shutdown = mfgpt_shutdown,
  	.set_state_periodic = mfgpt_set_periodic,
  	.set_state_oneshot = mfgpt_shutdown,
  	.tick_resume = mfgpt_shutdown,
c30d7d2b9   Andres Salomon   cs5535: add a gen...
99
100
  	.set_next_event = mfgpt_next_event,
  	.rating = 250,
c30d7d2b9   Andres Salomon   cs5535: add a gen...
101
102
103
104
105
106
107
108
109
110
111
112
  };
  
  static irqreturn_t mfgpt_tick(int irq, void *dev_id)
  {
  	uint16_t val = cs5535_mfgpt_read(cs5535_event_clock, MFGPT_REG_SETUP);
  
  	/* See if the interrupt was for us */
  	if (!(val & (MFGPT_SETUP_SETUP | MFGPT_SETUP_CMP2 | MFGPT_SETUP_CMP1)))
  		return IRQ_NONE;
  
  	/* Turn off the clock (and clear the event) */
  	disable_timer(cs5535_event_clock);
eb39a7c03   David Kozub   clockevents/drive...
113
114
  	if (clockevent_state_detached(&cs5535_clockevent) ||
  	    clockevent_state_shutdown(&cs5535_clockevent))
c30d7d2b9   Andres Salomon   cs5535: add a gen...
115
116
117
118
119
120
  		return IRQ_HANDLED;
  
  	/* Clear the counter */
  	cs5535_mfgpt_write(cs5535_event_clock, MFGPT_REG_COUNTER, 0);
  
  	/* Restart the clock in periodic mode */
8f9327cbb   Viresh Kumar   clockevents/drive...
121
  	if (clockevent_state_periodic(&cs5535_clockevent))
c30d7d2b9   Andres Salomon   cs5535: add a gen...
122
123
124
125
126
127
  		cs5535_mfgpt_write(cs5535_event_clock, MFGPT_REG_SETUP,
  				MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
  
  	cs5535_clockevent.event_handler(&cs5535_clockevent);
  	return IRQ_HANDLED;
  }
c30d7d2b9   Andres Salomon   cs5535: add a gen...
128
129
  static int __init cs5535_mfgpt_init(void)
  {
470cf1c28   afzal mohammed   clocksource/drive...
130
  	unsigned long flags = IRQF_NOBALANCING | IRQF_TIMER | IRQF_SHARED;
c30d7d2b9   Andres Salomon   cs5535: add a gen...
131
132
133
134
135
136
  	struct cs5535_mfgpt_timer *timer;
  	int ret;
  	uint16_t val;
  
  	timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
  	if (!timer) {
61e01be22   Jens Rottmann   cs5535-clockevt: ...
137
138
  		printk(KERN_ERR DRV_NAME ": Could not allocate MFGPT timer
  ");
c30d7d2b9   Andres Salomon   cs5535: add a gen...
139
140
141
142
143
144
145
146
147
  		return -ENODEV;
  	}
  	cs5535_event_clock = timer;
  
  	/* Set up the IRQ on the MFGPT side */
  	if (cs5535_mfgpt_setup_irq(timer, MFGPT_CMP2, &timer_irq)) {
  		printk(KERN_ERR DRV_NAME ": Could not set up IRQ %d
  ",
  				timer_irq);
fdb19a6cb   Jens Rottmann   cs5535-clockevt: ...
148
  		goto err_timer;
c30d7d2b9   Andres Salomon   cs5535: add a gen...
149
150
151
  	}
  
  	/* And register it with the kernel */
470cf1c28   afzal mohammed   clocksource/drive...
152
  	ret = request_irq(timer_irq, mfgpt_tick, flags, DRV_NAME, timer);
c30d7d2b9   Andres Salomon   cs5535: add a gen...
153
154
155
  	if (ret) {
  		printk(KERN_ERR DRV_NAME ": Unable to set up the interrupt.
  ");
fdb19a6cb   Jens Rottmann   cs5535-clockevt: ...
156
  		goto err_irq;
c30d7d2b9   Andres Salomon   cs5535: add a gen...
157
158
159
160
161
162
163
164
  	}
  
  	/* Set the clock scale and enable the event mode for CMP2 */
  	val = MFGPT_SCALE | (3 << 8);
  
  	cs5535_mfgpt_write(cs5535_event_clock, MFGPT_REG_SETUP, val);
  
  	/* Set up the clock event */
c30d7d2b9   Andres Salomon   cs5535: add a gen...
165
166
167
168
  	printk(KERN_INFO DRV_NAME
  		": Registering MFGPT timer as a clock event, using IRQ %d
  ",
  		timer_irq);
77cc982f6   Shawn Guo   clocksource: use ...
169
170
  	clockevents_config_and_register(&cs5535_clockevent, MFGPT_HZ,
  					0xF, 0xFFFE);
c30d7d2b9   Andres Salomon   cs5535: add a gen...
171
172
  
  	return 0;
fdb19a6cb   Jens Rottmann   cs5535-clockevt: ...
173
  err_irq:
c30d7d2b9   Andres Salomon   cs5535: add a gen...
174
  	cs5535_mfgpt_release_irq(cs5535_event_clock, MFGPT_CMP2, &timer_irq);
fdb19a6cb   Jens Rottmann   cs5535-clockevt: ...
175
176
  err_timer:
  	cs5535_mfgpt_free_timer(cs5535_event_clock);
c30d7d2b9   Andres Salomon   cs5535: add a gen...
177
178
179
180
181
182
  	printk(KERN_ERR DRV_NAME ": Unable to set up the MFGPT clock source
  ");
  	return -EIO;
  }
  
  module_init(cs5535_mfgpt_init);
d45840d9f   Andres Salomon   Andres has moved
183
  MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
c30d7d2b9   Andres Salomon   cs5535: add a gen...
184
185
  MODULE_DESCRIPTION("CS5535/CS5536 MFGPT clock event driver");
  MODULE_LICENSE("GPL");