scx200_hrt.c 2.5 KB

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