1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/clocksource.h>
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/ioport.h>
23#include <linux/scx200.h>
24
25#define NAME "scx200_hrt"
26
27static int mhz27;
28module_param(mhz27, int, 0);
29MODULE_PARM_DESC(mhz27, "count at 27.0 MHz (default is 1.0 MHz)");
30
31static int ppm;
32module_param(ppm, int, 0);
33MODULE_PARM_DESC(ppm, "+-adjust to actual XO freq (ppm)");
34
35
36#define SCx200_TMCNFG_OFFSET (SCx200_TIMER_OFFSET + 5)
37
38
39#define HR_TMEN (1 << 0)
40#define HR_TMCLKSEL (1 << 1)
41#define HR_TM27MPD (1 << 2)
42
43
44#define HRT_FREQ 1000000
45
46static cycle_t read_hrt(struct clocksource *cs)
47{
48
49 return (cycle_t) inl(scx200_cb_base + SCx200_TIMER_OFFSET);
50}
51
52static struct clocksource cs_hrt = {
53 .name = "scx200_hrt",
54 .rating = 250,
55 .read = read_hrt,
56 .mask = CLOCKSOURCE_MASK(32),
57 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
58
59};
60
61static int __init init_hrt_clocksource(void)
62{
63 u32 freq;
64
65 if (!scx200_cb_present())
66 return -ENODEV;
67
68
69 if (!request_region(scx200_cb_base + SCx200_TIMER_OFFSET,
70 SCx200_TIMER_SIZE,
71 "NatSemi SCx200 High-Resolution Timer")) {
72 pr_warn("unable to lock timer region\n");
73 return -ENODEV;
74 }
75
76
77 outb(HR_TMEN | (mhz27 ? HR_TMCLKSEL : 0),
78 scx200_cb_base + SCx200_TMCNFG_OFFSET);
79
80 freq = (HRT_FREQ + ppm);
81 if (mhz27)
82 freq *= 27;
83
84 pr_info("enabling scx200 high-res timer (%s MHz +%d ppm)\n", mhz27 ? "27":"1", ppm);
85
86 return clocksource_register_hz(&cs_hrt, freq);
87}
88
89module_init(init_hrt_clocksource);
90
91MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
92MODULE_DESCRIPTION("clocksource on SCx200 HiRes Timer");
93MODULE_LICENSE("GPL");
94