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