4 #include <linux/export.h>
6 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/hpet.h>
15 #include <asm/fixmap.h>
19 #define HPET_MASK CLOCKSOURCE_MASK(32)
23 #define FSEC_PER_NSEC 1000000L
25 #define HPET_DEV_USED_BIT 2
26 #define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
27 #define HPET_DEV_VALID 0x8
28 #define HPET_DEV_FSB_CAP 0x1000
29 #define HPET_DEV_PERI_CAP 0x2000
31 #define HPET_MIN_CYCLES 128
32 #define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1))
42 static unsigned long hpet_num_timers;
44 static void __iomem *hpet_virt_address;
47 struct clock_event_device
evt;
62 return readl(hpet_virt_address + a);
65 static inline void hpet_writel(
unsigned int d,
unsigned int a)
67 writel(d, hpet_virt_address + a);
71 #include <asm/pgtable.h>
74 static inline void hpet_set_mapping(
void)
82 static inline void hpet_clear_mapping(
void)
85 hpet_virt_address =
NULL;
91 static int boot_hpet_disable;
93 static int hpet_verbose;
102 if (!
strncmp(
"disable", str, 7))
103 boot_hpet_disable = 1;
106 if (!
strncmp(
"verbose", str, 7))
114 static int __init disable_hpet(
char *
str)
116 boot_hpet_disable = 1;
119 __setup(
"nohpet", disable_hpet);
121 static inline int is_hpet_capable(
void)
129 static int hpet_legacy_int_enabled;
136 return is_hpet_capable() && hpet_legacy_int_enabled;
140 static void _hpet_print_config(
const char *
function,
int line)
146 timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
155 for (i = 0; i < timers; i++) {
171 #define hpet_print_config() \
174 _hpet_print_config(__FUNCTION__, __LINE__); \
183 static void hpet_reserve_msi_timers(
struct hpet_data *hd);
185 static void hpet_reserve_platform_timers(
unsigned int id)
189 unsigned int nrtimers,
i;
192 nrtimers = ((
id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
194 memset(&hd, 0,
sizeof(hd));
196 hd.hd_address = hpet;
197 hd.hd_nirqs = nrtimers;
198 hpet_reserve_timer(&hd, 0);
200 #ifdef CONFIG_HPET_EMULATE_RTC
201 hpet_reserve_timer(&hd, 1);
209 hd.hd_irq[0] = HPET_LEGACY_8254;
210 hd.hd_irq[1] = HPET_LEGACY_RTC;
212 for (i = 2; i < nrtimers; timer++, i++) {
213 hd.hd_irq[
i] = (
readl(&timer->hpet_config) &
217 hpet_reserve_msi_timers(&hd);
223 static void hpet_reserve_platform_timers(
unsigned int id) { }
229 static unsigned long hpet_freq;
231 static void hpet_legacy_set_mode(
enum clock_event_mode
mode,
232 struct clock_event_device *
evt);
233 static int hpet_legacy_next_event(
unsigned long delta,
234 struct clock_event_device *
evt);
239 static struct clock_event_device hpet_clockevent = {
241 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
242 .set_mode = hpet_legacy_set_mode,
243 .set_next_event = hpet_legacy_next_event,
248 static void hpet_stop_counter(
void)
251 cfg &= ~HPET_CFG_ENABLE;
252 hpet_writel(cfg, HPET_CFG);
255 static void hpet_reset_counter(
void)
257 hpet_writel(0, HPET_COUNTER);
258 hpet_writel(0, HPET_COUNTER + 4);
261 static void hpet_start_counter(
void)
264 cfg |= HPET_CFG_ENABLE;
265 hpet_writel(cfg, HPET_CFG);
268 static void hpet_restart_counter(
void)
271 hpet_reset_counter();
272 hpet_start_counter();
275 static void hpet_resume_device(
void)
282 hpet_resume_device();
283 hpet_restart_counter();
286 static void hpet_enable_legacy_int(
void)
290 cfg |= HPET_CFG_LEGACY;
291 hpet_writel(cfg, HPET_CFG);
292 hpet_legacy_int_enabled = 1;
295 static void hpet_legacy_clockevent_register(
void)
298 hpet_enable_legacy_int();
311 static int hpet_setup_msi_irq(
unsigned int irq);
313 static void hpet_set_mode(
enum clock_event_mode
mode,
314 struct clock_event_device *
evt,
int timer)
316 unsigned int cfg, cmp, now;
320 case CLOCK_EVT_MODE_PERIODIC:
323 delta >>= evt->shift;
325 cmp = now + (
unsigned int) delta;
327 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
328 HPET_TN_SETVAL | HPET_TN_32BIT;
329 hpet_writel(cfg, HPET_Tn_CFG(timer));
330 hpet_writel(cmp, HPET_Tn_CMP(timer));
339 hpet_writel((
unsigned int) delta, HPET_Tn_CMP(timer));
340 hpet_start_counter();
344 case CLOCK_EVT_MODE_ONESHOT:
346 cfg &= ~HPET_TN_PERIODIC;
347 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
348 hpet_writel(cfg, HPET_Tn_CFG(timer));
351 case CLOCK_EVT_MODE_UNUSED:
352 case CLOCK_EVT_MODE_SHUTDOWN:
354 cfg &= ~HPET_TN_ENABLE;
355 hpet_writel(cfg, HPET_Tn_CFG(timer));
358 case CLOCK_EVT_MODE_RESUME:
360 hpet_enable_legacy_int();
363 hpet_setup_msi_irq(hdev->
irq);
373 static int hpet_next_event(
unsigned long delta,
374 struct clock_event_device *evt,
int timer)
381 hpet_writel(cnt, HPET_Tn_CMP(timer));
410 static void hpet_legacy_set_mode(
enum clock_event_mode mode,
411 struct clock_event_device *evt)
413 hpet_set_mode(mode, evt, 0);
416 static int hpet_legacy_next_event(
unsigned long delta,
417 struct clock_event_device *evt)
419 return hpet_next_event(delta, evt, 0);
425 #ifdef CONFIG_PCI_MSI
437 cfg |= HPET_TN_ENABLE | HPET_TN_FSB;
438 hpet_writel(cfg, HPET_Tn_CFG(hdev->
num));
441 void hpet_msi_mask(
struct irq_data *data)
448 cfg &= ~(HPET_TN_ENABLE | HPET_TN_FSB);
449 hpet_writel(cfg, HPET_Tn_CFG(hdev->
num));
454 hpet_writel(msg->
data, HPET_Tn_ROUTE(hdev->
num));
465 static void hpet_msi_set_mode(
enum clock_event_mode mode,
466 struct clock_event_device *evt)
469 hpet_set_mode(mode, evt, hdev->
num);
472 static int hpet_msi_next_event(
unsigned long delta,
473 struct clock_event_device *evt)
476 return hpet_next_event(delta, evt, hdev->
num);
479 static int hpet_setup_msi_irq(
unsigned int irq)
498 if (hpet_setup_msi_irq(irq))
505 static irqreturn_t hpet_interrupt_handler(
int irq,
void *data)
508 struct clock_event_device *hevt = &dev->
evt;
510 if (!hevt->event_handler) {
516 hevt->event_handler(hevt);
520 static int hpet_setup_irq(
struct hpet_dev *dev)
539 static void init_one_hpet_msi_clockevent(
struct hpet_dev *hdev,
int cpu)
541 struct clock_event_device *evt = &hdev->
evt;
547 if (hpet_setup_msi_irq(hdev->
irq))
551 per_cpu(cpu_hpet_dev, cpu) = hdev;
553 hpet_setup_irq(hdev);
554 evt->irq = hdev->
irq;
557 evt->features = CLOCK_EVT_FEAT_ONESHOT;
559 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
561 evt->set_mode = hpet_msi_set_mode;
562 evt->set_next_event = hpet_msi_next_event;
571 #define RESERVE_TIMERS 1
573 #define RESERVE_TIMERS 0
576 static void hpet_msi_capability_lookup(
unsigned int start_timer)
579 unsigned int num_timers;
580 unsigned int num_timers_used = 0;
590 num_timers = ((
id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
598 hpet_num_timers = num_timers;
600 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
601 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
602 unsigned int cfg =
hpet_readl(HPET_Tn_CFG(i));
605 if (!(cfg & HPET_TN_FSB_CAP))
609 if (cfg & HPET_TN_PERIODIC_CAP)
614 if (hpet_assign_irq(hdev))
624 printk(
KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
625 num_timers, num_timers_used);
629 static void hpet_reserve_msi_timers(
struct hpet_data *hd)
636 for (i = 0; i < hpet_num_timers; i++) {
643 hpet_reserve_timer(hd, hdev->
num);
648 static struct hpet_dev *hpet_get_unused_timer(
void)
655 for (i = 0; i < hpet_num_timers; i++) {
661 (
unsigned long *)&hdev->
flags))
668 struct hpet_work_struct {
677 struct hpet_work_struct *hpet_work;
681 hdev = hpet_get_unused_timer();
683 init_one_hpet_msi_clockevent(hdev, cpu);
689 unsigned long action,
void *hcpu)
691 unsigned long cpu = (
unsigned long)hcpu;
692 struct hpet_work_struct
work;
695 switch (action & 0xf) {
698 init_completion(&
work.complete);
702 destroy_timer_on_stack(&
work.work.timer);
716 static int hpet_setup_msi_irq(
unsigned int irq)
720 static void hpet_msi_capability_lookup(
unsigned int start_timer)
726 static void hpet_reserve_msi_timers(
struct hpet_data *hd)
733 unsigned long action,
void *hcpu)
754 .resume = hpet_resume_counter,
756 .archdata = { .vclock_mode = VCLOCK_HPET },
760 static int hpet_clocksource_register(
void)
766 hpet_restart_counter();
781 }
while ((now - start) < 200000
UL);
785 "HPET counter not counting. HPET disabled\n");
789 clocksource_register_hz(&clocksource_hpet, (
u32)hpet_freq);
793 static u32 *hpet_boot_cfg;
802 unsigned int i,
last;
804 if (!is_hpet_capable())
827 for (i = 0;
hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
830 "HPET config register value = 0xFFFFFFFF. "
836 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
844 do_div(freq, hpet_period);
854 last = (
id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
856 #ifdef CONFIG_HPET_EMULATE_RTC
866 hpet_boot_cfg =
kmalloc((last + 2) *
sizeof(*hpet_boot_cfg),
869 *hpet_boot_cfg =
cfg;
871 pr_warn(
"HPET initial state will not be saved\n");
872 cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
873 hpet_writel(cfg, HPET_CFG);
875 pr_warn(
"HPET: Unrecognized bits %#x set in global cfg\n",
878 for (i = 0; i <= last; ++
i) {
881 hpet_boot_cfg[i + 1] =
cfg;
882 cfg &= ~(HPET_TN_ENABLE | HPET_TN_LEVEL | HPET_TN_FSB);
883 hpet_writel(cfg, HPET_Tn_CFG(i));
884 cfg &= ~(HPET_TN_PERIODIC | HPET_TN_PERIODIC_CAP
885 | HPET_TN_64BIT_CAP | HPET_TN_32BIT | HPET_TN_ROUTE
886 | HPET_TN_FSB | HPET_TN_FSB_CAP);
888 pr_warn(
"HPET: Unrecognized bits %#x set in cfg#%u\n",
893 if (hpet_clocksource_register())
896 if (
id & HPET_ID_LEGSUP) {
897 hpet_legacy_clockevent_register();
903 hpet_clear_mapping();
914 static __init int hpet_late_init(
void)
918 if (boot_hpet_disable)
922 if (!force_hpet_address)
929 if (!hpet_virt_address)
933 hpet_msi_capability_lookup(2);
935 hpet_msi_capability_lookup(0);
937 hpet_reserve_platform_timers(
hpet_readl(HPET_ID));
959 if (is_hpet_capable() && hpet_virt_address) {
963 cfg = *hpet_boot_cfg;
964 else if (hpet_legacy_int_enabled) {
965 cfg &= ~HPET_CFG_LEGACY;
966 hpet_legacy_int_enabled = 0;
968 cfg &= ~HPET_CFG_ENABLE;
969 hpet_writel(cfg, HPET_CFG);
975 last = ((
id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
977 for (
id = 0;
id <= last; ++
id)
978 hpet_writel(hpet_boot_cfg[
id + 1], HPET_Tn_CFG(
id));
980 if (*hpet_boot_cfg & HPET_CFG_ENABLE)
981 hpet_writel(*hpet_boot_cfg, HPET_CFG);
985 #ifdef CONFIG_HPET_EMULATE_RTC
1002 #include <linux/rtc.h>
1003 #include <asm/rtc.h>
1005 #define DEFAULT_RTC_INT_FREQ 64
1006 #define DEFAULT_RTC_SHIFT 6
1007 #define RTC_NUM_INTS 1
1009 static unsigned long hpet_rtc_flags;
1010 static int hpet_prev_update_sec;
1011 static struct rtc_time hpet_alarm_time;
1012 static unsigned long hpet_pie_count;
1013 static u32 hpet_t1_cmp;
1014 static u32 hpet_default_delta;
1015 static u32 hpet_pie_delta;
1016 static unsigned long hpet_pie_limit;
1023 static inline int hpet_cnt_ahead(
u32 c1,
u32 c2)
1025 return (
s32)(c2 - c1) < 0;
1038 irq_handler = handler;
1067 unsigned long flags;
1072 if (!hpet_default_delta) {
1076 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
1077 hpet_default_delta = clc;
1080 if (!(hpet_rtc_flags &
RTC_PIE) || hpet_pie_limit)
1081 delta = hpet_default_delta;
1083 delta = hpet_pie_delta;
1088 hpet_writel(cnt, HPET_T1_CMP);
1092 cfg &= ~HPET_TN_PERIODIC;
1093 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1094 hpet_writel(cfg, HPET_T1_CFG);
1102 static void hpet_disable_rtc_channel(
void)
1106 cfg &= ~HPET_TN_ENABLE;
1107 hpet_writel(cfg, HPET_T1_CFG);
1120 hpet_rtc_flags &= ~bit_mask;
1122 hpet_disable_rtc_channel();
1130 unsigned long oldbits = hpet_rtc_flags;
1135 hpet_rtc_flags |= bit_mask;
1137 if ((bit_mask &
RTC_UIE) && !(oldbits & RTC_UIE))
1138 hpet_prev_update_sec = -1;
1153 hpet_alarm_time.tm_hour = hrs;
1154 hpet_alarm_time.tm_min =
min;
1155 hpet_alarm_time.tm_sec =
sec;
1168 if (freq <= DEFAULT_RTC_INT_FREQ)
1169 hpet_pie_limit = DEFAULT_RTC_INT_FREQ /
freq;
1173 clc >>= hpet_clockevent.shift;
1174 hpet_pie_delta = clc;
1187 static void hpet_rtc_timer_reinit(
void)
1193 hpet_disable_rtc_channel();
1195 if (!(hpet_rtc_flags &
RTC_PIE) || hpet_pie_limit)
1196 delta = hpet_default_delta;
1198 delta = hpet_pie_delta;
1205 hpet_t1_cmp +=
delta;
1206 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1208 }
while (!hpet_cnt_ahead(hpet_t1_cmp,
hpet_readl(HPET_COUNTER)));
1211 if (hpet_rtc_flags & RTC_PIE)
1212 hpet_pie_count += lost_ints;
1213 if (printk_ratelimit())
1222 unsigned long rtc_int_flag = 0;
1224 hpet_rtc_timer_reinit();
1227 if (hpet_rtc_flags & (RTC_UIE |
RTC_AIE))
1230 if (hpet_rtc_flags & RTC_UIE &&
1231 curr_time.tm_sec != hpet_prev_update_sec) {
1232 if (hpet_prev_update_sec >= 0)
1234 hpet_prev_update_sec = curr_time.tm_sec;
1237 if (hpet_rtc_flags & RTC_PIE &&
1238 ++hpet_pie_count >= hpet_pie_limit) {
1243 if (hpet_rtc_flags &
RTC_AIE &&
1244 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1245 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1246 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1250 rtc_int_flag |= (
RTC_IRQF | (RTC_NUM_INTS << 8));