hpet.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <linux/clocksource.h>
  2. #include <linux/errno.h>
  3. #include <linux/hpet.h>
  4. #include <linux/init.h>
  5. #include <asm/hpet.h>
  6. #include <asm/io.h>
  7. #define HPET_MASK CLOCKSOURCE_MASK(32)
  8. #define HPET_SHIFT 22
  9. /* FSEC = 10^-15 NSEC = 10^-9 */
  10. #define FSEC_PER_NSEC 1000000
  11. static void *hpet_ptr;
  12. static cycle_t read_hpet(void)
  13. {
  14. return (cycle_t)readl(hpet_ptr);
  15. }
  16. static struct clocksource clocksource_hpet = {
  17. .name = "hpet",
  18. .rating = 250,
  19. .read = read_hpet,
  20. .mask = HPET_MASK,
  21. .mult = 0, /* set below */
  22. .shift = HPET_SHIFT,
  23. .is_continuous = 1,
  24. };
  25. static int __init init_hpet_clocksource(void)
  26. {
  27. unsigned long hpet_period;
  28. void __iomem* hpet_base;
  29. u64 tmp;
  30. if (!is_hpet_enabled())
  31. return -ENODEV;
  32. /* calculate the hpet address: */
  33. hpet_base =
  34. (void __iomem*)ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
  35. hpet_ptr = hpet_base + HPET_COUNTER;
  36. /* calculate the frequency: */
  37. hpet_period = readl(hpet_base + HPET_PERIOD);
  38. /*
  39. * hpet period is in femto seconds per cycle
  40. * so we need to convert this to ns/cyc units
  41. * aproximated by mult/2^shift
  42. *
  43. * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
  44. * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
  45. * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
  46. * (fsec/cyc << shift)/1000000 = mult
  47. * (hpet_period << shift)/FSEC_PER_NSEC = mult
  48. */
  49. tmp = (u64)hpet_period << HPET_SHIFT;
  50. do_div(tmp, FSEC_PER_NSEC);
  51. clocksource_hpet.mult = (u32)tmp;
  52. return clocksource_register(&clocksource_hpet);
  53. }
  54. module_init(init_hpet_clocksource);