scx200_hrt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2006 Jim Cromie
  3. *
  4. * This is a clocksource driver for the Geode SCx200's 1 or 27 MHz
  5. * high-resolution timer. The Geode SC-1100 (at least) has a buggy
  6. * time stamp counter (TSC), which loses time unless 'idle=poll' is
  7. * given as a boot-arg. In its absence, the Generic Timekeeping code
  8. * will detect and de-rate the bad TSC, allowing this timer to take
  9. * over timekeeping duties.
  10. *
  11. * Based on work by John Stultz, and Ted Phelps (in a 2.6.12-rc6 patch)
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of the
  16. * License, or (at your option) any later version.
  17. */
  18. #include <linux/clocksource.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/ioport.h>
  22. #include <linux/scx200.h>
  23. #define NAME "scx200_hrt"
  24. static int mhz27;
  25. module_param(mhz27, int, 0); /* load time only */
  26. MODULE_PARM_DESC(mhz27, "count at 27.0 MHz (default is 1.0 MHz)");
  27. static int ppm;
  28. module_param(ppm, int, 0); /* load time only */
  29. MODULE_PARM_DESC(ppm, "+-adjust to actual XO freq (ppm)");
  30. /* HiRes Timer configuration register address */
  31. #define SCx200_TMCNFG_OFFSET (SCx200_TIMER_OFFSET + 5)
  32. /* and config settings */
  33. #define HR_TMEN (1 << 0) /* timer interrupt enable */
  34. #define HR_TMCLKSEL (1 << 1) /* 1|0 counts at 27|1 MHz */
  35. #define HR_TM27MPD (1 << 2) /* 1 turns off input clock (power-down) */
  36. /* The base timer frequency, * 27 if selected */
  37. #define HRT_FREQ 1000000
  38. static cycle_t read_hrt(struct clocksource *cs)
  39. {
  40. /* Read the timer value */
  41. return (cycle_t) inl(scx200_cb_base + SCx200_TIMER_OFFSET);
  42. }
  43. #define HRT_SHIFT_1 22
  44. #define HRT_SHIFT_27 26
  45. static struct clocksource cs_hrt = {
  46. .name = "scx200_hrt",
  47. .rating = 250,
  48. .read = read_hrt,
  49. .mask = CLOCKSOURCE_MASK(32),
  50. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  51. /* mult, shift are set based on mhz27 flag */
  52. };
  53. static int __init init_hrt_clocksource(void)
  54. {
  55. /* Make sure scx200 has initialized the configuration block */
  56. if (!scx200_cb_present())
  57. return -ENODEV;
  58. /* Reserve the timer's ISA io-region for ourselves */
  59. if (!request_region(scx200_cb_base + SCx200_TIMER_OFFSET,
  60. SCx200_TIMER_SIZE,
  61. "NatSemi SCx200 High-Resolution Timer")) {
  62. printk(KERN_WARNING NAME ": unable to lock timer region\n");
  63. return -ENODEV;
  64. }
  65. /* write timer config */
  66. outb(HR_TMEN | (mhz27 ? HR_TMCLKSEL : 0),
  67. scx200_cb_base + SCx200_TMCNFG_OFFSET);
  68. if (mhz27) {
  69. cs_hrt.shift = HRT_SHIFT_27;
  70. cs_hrt.mult = clocksource_hz2mult((HRT_FREQ + ppm) * 27,
  71. cs_hrt.shift);
  72. } else {
  73. cs_hrt.shift = HRT_SHIFT_1;
  74. cs_hrt.mult = clocksource_hz2mult(HRT_FREQ + ppm,
  75. cs_hrt.shift);
  76. }
  77. printk(KERN_INFO "enabling scx200 high-res timer (%s MHz +%d ppm)\n",
  78. mhz27 ? "27":"1", ppm);
  79. return clocksource_register(&cs_hrt);
  80. }
  81. module_init(init_hrt_clocksource);
  82. MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
  83. MODULE_DESCRIPTION("clocksource on SCx200 HiRes Timer");
  84. MODULE_LICENSE("GPL");