[PATCH] tick-management: dyntick / highres functionality
[linux-2.6-block.git] / arch / i386 / kernel / hpet.c
CommitLineData
5d0cf410 1#include <linux/clocksource.h>
2#include <linux/errno.h>
3#include <linux/hpet.h>
4#include <linux/init.h>
5
6#include <asm/hpet.h>
7#include <asm/io.h>
8
7f9f303a 9#define HPET_MASK CLOCKSOURCE_MASK(32)
5d0cf410 10#define HPET_SHIFT 22
11
12/* FSEC = 10^-15 NSEC = 10^-9 */
13#define FSEC_PER_NSEC 1000000
14
5b71bddb 15static void __iomem *hpet_ptr;
5d0cf410 16
17static cycle_t read_hpet(void)
18{
19 return (cycle_t)readl(hpet_ptr);
20}
21
22static struct clocksource clocksource_hpet = {
23 .name = "hpet",
24 .rating = 250,
25 .read = read_hpet,
7f9f303a 26 .mask = HPET_MASK,
5d0cf410 27 .mult = 0, /* set below */
28 .shift = HPET_SHIFT,
73b08d2a 29 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
5d0cf410 30};
31
32static int __init init_hpet_clocksource(void)
33{
34 unsigned long hpet_period;
35 void __iomem* hpet_base;
36 u64 tmp;
fa5cecd1 37 int err;
5d0cf410 38
30f3174d 39 if (!is_hpet_enabled())
5d0cf410 40 return -ENODEV;
41
42 /* calculate the hpet address: */
5b71bddb 43 hpet_base = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
5d0cf410 44 hpet_ptr = hpet_base + HPET_COUNTER;
45
46 /* calculate the frequency: */
47 hpet_period = readl(hpet_base + HPET_PERIOD);
48
49 /*
50 * hpet period is in femto seconds per cycle
51 * so we need to convert this to ns/cyc units
52 * aproximated by mult/2^shift
53 *
54 * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
55 * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
56 * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
57 * (fsec/cyc << shift)/1000000 = mult
58 * (hpet_period << shift)/FSEC_PER_NSEC = mult
59 */
60 tmp = (u64)hpet_period << HPET_SHIFT;
61 do_div(tmp, FSEC_PER_NSEC);
62 clocksource_hpet.mult = (u32)tmp;
63
fa5cecd1
AL
64 err = clocksource_register(&clocksource_hpet);
65 if (err)
66 iounmap(hpet_base);
67
68 return err;
5d0cf410 69}
70
71module_init(init_hpet_clocksource);