1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the CS5535/CS5536 Multi-Function General Purpose Timers (MFGPT)
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>
9 * The MFGPTs are documented in AMD Geode CS5536 Companion Device Data Book.
12 #include <linux/kernel.h>
13 #include <linux/spinlock.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/cs5535.h>
18 #include <linux/slab.h>
20 #define DRV_NAME "cs5535-mfgpt"
22 static int mfgpt_reset_timers
;
23 module_param_named(mfgptfix
, mfgpt_reset_timers
, int, 0644);
24 MODULE_PARM_DESC(mfgptfix
, "Try to reset the MFGPT timers during init; "
25 "required by some broken BIOSes (ie, TinyBIOS < 0.99) or kexec "
26 "(1 = reset the MFGPT using an undocumented bit, "
27 "2 = perform a soft reset by unconfiguring all timers); "
28 "use what works best for you.");
30 struct cs5535_mfgpt_timer
{
31 struct cs5535_mfgpt_chip
*chip
;
35 static struct cs5535_mfgpt_chip
{
36 DECLARE_BITMAP(avail
, MFGPT_MAX_TIMERS
);
39 struct platform_device
*pdev
;
44 int cs5535_mfgpt_toggle_event(struct cs5535_mfgpt_timer
*timer
, int cmp
,
45 int event
, int enable
)
47 uint32_t msr
, mask
, value
, dummy
;
48 int shift
= (cmp
== MFGPT_CMP1
) ? 0 : 8;
56 * The register maps for these are described in sections 6.17.1.x of
57 * the AMD Geode CS5536 Companion Device Data Book.
60 case MFGPT_EVENT_RESET
:
62 * XXX: According to the docs, we cannot reset timers above
63 * 6; that is, resets for 7 and 8 will be ignored. Is this
64 * a problem? -dilinger
67 mask
= 1 << (timer
->nr
+ 24);
72 mask
= 1 << (timer
->nr
+ shift
);
77 mask
= 1 << (timer
->nr
+ shift
);
84 rdmsr(msr
, value
, dummy
);
91 wrmsr(msr
, value
, dummy
);
94 EXPORT_SYMBOL_GPL(cs5535_mfgpt_toggle_event
);
96 int cs5535_mfgpt_set_irq(struct cs5535_mfgpt_timer
*timer
, int cmp
, int *irq
,
99 uint32_t zsel
, lpc
, dummy
;
108 * Unfortunately, MFGPTs come in pairs sharing their IRQ lines. If VSA
109 * is using the same CMP of the timer's Siamese twin, the IRQ is set to
110 * 2, and we mustn't use nor change it.
111 * XXX: Likewise, 2 Linux drivers might clash if the 2nd overwrites the
112 * IRQ of the 1st. This can only happen if forcing an IRQ, calling this
113 * with *irq==0 is safe. Currently there _are_ no 2 drivers.
115 rdmsr(MSR_PIC_ZSEL_LOW
, zsel
, dummy
);
116 shift
= ((cmp
== MFGPT_CMP1
? 0 : 4) + timer
->nr
% 4) * 4;
117 if (((zsel
>> shift
) & 0xF) == 2)
120 /* Choose IRQ: if none supplied, keep IRQ already set or use default */
122 *irq
= (zsel
>> shift
) & 0xF;
124 *irq
= CONFIG_CS5535_MFGPT_DEFAULT_IRQ
;
126 /* Can't use IRQ if it's 0 (=disabled), 2, or routed to LPC */
127 if (*irq
< 1 || *irq
== 2 || *irq
> 15)
129 rdmsr(MSR_PIC_IRQM_LPC
, lpc
, dummy
);
130 if (lpc
& (1 << *irq
))
133 /* All chosen and checked - go for it */
134 if (cs5535_mfgpt_toggle_event(timer
, cmp
, MFGPT_EVENT_IRQ
, enable
))
137 zsel
= (zsel
& ~(0xF << shift
)) | (*irq
<< shift
);
138 wrmsr(MSR_PIC_ZSEL_LOW
, zsel
, dummy
);
143 EXPORT_SYMBOL_GPL(cs5535_mfgpt_set_irq
);
145 struct cs5535_mfgpt_timer
*cs5535_mfgpt_alloc_timer(int timer_nr
, int domain
)
147 struct cs5535_mfgpt_chip
*mfgpt
= &cs5535_mfgpt_chip
;
148 struct cs5535_mfgpt_timer
*timer
= NULL
;
152 if (!mfgpt
->initialized
)
155 /* only allocate timers from the working domain if requested */
156 if (domain
== MFGPT_DOMAIN_WORKING
)
159 max
= MFGPT_MAX_TIMERS
;
161 if (timer_nr
>= max
) {
162 /* programmer error. silly programmers! */
167 spin_lock_irqsave(&mfgpt
->lock
, flags
);
171 /* try to find any available timer */
172 t
= find_first_bit(mfgpt
->avail
, max
);
173 /* set timer_nr to -1 if no timers available */
174 timer_nr
= t
< max
? (int) t
: -1;
176 /* check if the requested timer's available */
177 if (!test_bit(timer_nr
, mfgpt
->avail
))
182 /* if timer_nr is not -1, it's an available timer */
183 __clear_bit(timer_nr
, mfgpt
->avail
);
184 spin_unlock_irqrestore(&mfgpt
->lock
, flags
);
189 timer
= kmalloc(sizeof(*timer
), GFP_KERNEL
);
192 spin_lock_irqsave(&mfgpt
->lock
, flags
);
193 __set_bit(timer_nr
, mfgpt
->avail
);
194 spin_unlock_irqrestore(&mfgpt
->lock
, flags
);
198 timer
->nr
= timer_nr
;
199 dev_info(&mfgpt
->pdev
->dev
, "registered timer %d\n", timer_nr
);
204 EXPORT_SYMBOL_GPL(cs5535_mfgpt_alloc_timer
);
207 * XXX: This frees the timer memory, but never resets the actual hardware
208 * timer. The old geode_mfgpt code did this; it would be good to figure
209 * out a way to actually release the hardware timer. See comments below.
211 void cs5535_mfgpt_free_timer(struct cs5535_mfgpt_timer
*timer
)
216 /* timer can be made available again only if never set up */
217 val
= cs5535_mfgpt_read(timer
, MFGPT_REG_SETUP
);
218 if (!(val
& MFGPT_SETUP_SETUP
)) {
219 spin_lock_irqsave(&timer
->chip
->lock
, flags
);
220 __set_bit(timer
->nr
, timer
->chip
->avail
);
221 spin_unlock_irqrestore(&timer
->chip
->lock
, flags
);
226 EXPORT_SYMBOL_GPL(cs5535_mfgpt_free_timer
);
228 uint16_t cs5535_mfgpt_read(struct cs5535_mfgpt_timer
*timer
, uint16_t reg
)
230 return inw(timer
->chip
->base
+ reg
+ (timer
->nr
* 8));
232 EXPORT_SYMBOL_GPL(cs5535_mfgpt_read
);
234 void cs5535_mfgpt_write(struct cs5535_mfgpt_timer
*timer
, uint16_t reg
,
237 outw(value
, timer
->chip
->base
+ reg
+ (timer
->nr
* 8));
239 EXPORT_SYMBOL_GPL(cs5535_mfgpt_write
);
242 * This is a sledgehammer that resets all MFGPT timers. This is required by
243 * some broken BIOSes which leave the system in an unstable state
244 * (TinyBIOS 0.98, for example; fixed in 0.99). It's uncertain as to
245 * whether or not this secret MSR can be used to release individual timers.
246 * Jordan tells me that he and Mitch once played w/ it, but it's unclear
247 * what the results of that were (and they experienced some instability).
249 static void reset_all_timers(void)
253 /* The following undocumented bit resets the MFGPT timers */
254 val
= 0xFF; dummy
= 0;
255 wrmsr(MSR_MFGPT_SETUP
, val
, dummy
);
259 * This is another sledgehammer to reset all MFGPT timers.
260 * Instead of using the undocumented bit method it clears
261 * IRQ, NMI and RESET settings.
263 static void soft_reset(void)
266 struct cs5535_mfgpt_timer t
;
268 for (i
= 0; i
< MFGPT_MAX_TIMERS
; i
++) {
271 cs5535_mfgpt_toggle_event(&t
, MFGPT_CMP1
, MFGPT_EVENT_RESET
, 0);
272 cs5535_mfgpt_toggle_event(&t
, MFGPT_CMP2
, MFGPT_EVENT_RESET
, 0);
273 cs5535_mfgpt_toggle_event(&t
, MFGPT_CMP1
, MFGPT_EVENT_NMI
, 0);
274 cs5535_mfgpt_toggle_event(&t
, MFGPT_CMP2
, MFGPT_EVENT_NMI
, 0);
275 cs5535_mfgpt_toggle_event(&t
, MFGPT_CMP1
, MFGPT_EVENT_IRQ
, 0);
276 cs5535_mfgpt_toggle_event(&t
, MFGPT_CMP2
, MFGPT_EVENT_IRQ
, 0);
281 * Check whether any MFGPTs are available for the kernel to use. In most
282 * cases, firmware that uses AMD's VSA code will claim all timers during
283 * bootup; we certainly don't want to take them if they're already in use.
284 * In other cases (such as with VSAless OpenFirmware), the system firmware
285 * leaves timers available for us to use.
287 static int scan_timers(struct cs5535_mfgpt_chip
*mfgpt
)
289 struct cs5535_mfgpt_timer timer
= { .chip
= mfgpt
};
295 /* bios workaround */
296 if (mfgpt_reset_timers
== 1)
298 else if (mfgpt_reset_timers
== 2)
301 /* just to be safe, protect this section w/ lock */
302 spin_lock_irqsave(&mfgpt
->lock
, flags
);
303 for (i
= 0; i
< MFGPT_MAX_TIMERS
; i
++) {
305 val
= cs5535_mfgpt_read(&timer
, MFGPT_REG_SETUP
);
306 if (!(val
& MFGPT_SETUP_SETUP
) || mfgpt_reset_timers
== 2) {
307 __set_bit(i
, mfgpt
->avail
);
311 spin_unlock_irqrestore(&mfgpt
->lock
, flags
);
316 static int cs5535_mfgpt_probe(struct platform_device
*pdev
)
318 struct resource
*res
;
321 if (mfgpt_reset_timers
< 0 || mfgpt_reset_timers
> 2) {
322 dev_err(&pdev
->dev
, "Bad mfgpt_reset_timers value: %i\n",
327 /* There are two ways to get the MFGPT base address; one is by
328 * fetching it from MSR_LBAR_MFGPT, the other is by reading the
329 * PCI BAR info. The latter method is easier (especially across
330 * different architectures), so we'll stick with that for now. If
331 * it turns out to be unreliable in the face of crappy BIOSes, we
332 * can always go back to using MSRs.. */
334 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
336 dev_err(&pdev
->dev
, "can't fetch device resource info\n");
340 if (!request_region(res
->start
, resource_size(res
), pdev
->name
)) {
341 dev_err(&pdev
->dev
, "can't request region\n");
345 /* set up the driver-specific struct */
346 cs5535_mfgpt_chip
.base
= res
->start
;
347 cs5535_mfgpt_chip
.pdev
= pdev
;
348 spin_lock_init(&cs5535_mfgpt_chip
.lock
);
350 dev_info(&pdev
->dev
, "reserved resource region %pR\n", res
);
352 /* detect the available timers */
353 t
= scan_timers(&cs5535_mfgpt_chip
);
354 dev_info(&pdev
->dev
, "%d MFGPT timers available\n", t
);
355 cs5535_mfgpt_chip
.initialized
= 1;
362 static struct platform_driver cs5535_mfgpt_driver
= {
366 .probe
= cs5535_mfgpt_probe
,
370 static int __init
cs5535_mfgpt_init(void)
372 return platform_driver_register(&cs5535_mfgpt_driver
);
375 module_init(cs5535_mfgpt_init
);
377 MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
378 MODULE_DESCRIPTION("CS5535/CS5536 MFGPT timer driver");
379 MODULE_LICENSE("GPL");
380 MODULE_ALIAS("platform:" DRV_NAME
);