mfgpt_32.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Driver/API for AMD Geode Multi-Function General Purpose Timers (MFGPT)
  3. *
  4. * Copyright (C) 2006, Advanced Micro Devices, Inc.
  5. * Copyright (C) 2007, Andres Salomon <dilinger@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of version 2 of the GNU General Public License
  9. * as published by the Free Software Foundation.
  10. *
  11. * The MFGPTs are documented in AMD Geode CS5536 Companion Device Data Book.
  12. */
  13. /*
  14. * We are using the 32.768kHz input clock - it's the only one that has the
  15. * ranges we find desirable. The following table lists the suitable
  16. * divisors and the associated Hz, minimum interval and the maximum interval:
  17. *
  18. * Divisor Hz Min Delta (s) Max Delta (s)
  19. * 1 32768 .00048828125 2.000
  20. * 2 16384 .0009765625 4.000
  21. * 4 8192 .001953125 8.000
  22. * 8 4096 .00390625 16.000
  23. * 16 2048 .0078125 32.000
  24. * 32 1024 .015625 64.000
  25. * 64 512 .03125 128.000
  26. * 128 256 .0625 256.000
  27. * 256 128 .125 512.000
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/module.h>
  32. #include <asm/geode.h>
  33. #define F_AVAIL 0x01
  34. static struct mfgpt_timer_t {
  35. int flags;
  36. struct module *owner;
  37. } mfgpt_timers[MFGPT_MAX_TIMERS];
  38. /* Selected from the table above */
  39. #define MFGPT_DIVISOR 16
  40. #define MFGPT_SCALE 4 /* divisor = 2^(scale) */
  41. #define MFGPT_HZ (32768 / MFGPT_DIVISOR)
  42. #define MFGPT_PERIODIC (MFGPT_HZ / HZ)
  43. #ifdef CONFIG_GEODE_MFGPT_TIMER
  44. static int __init mfgpt_timer_setup(void);
  45. #else
  46. #define mfgpt_timer_setup() (0)
  47. #endif
  48. /* Allow for disabling of MFGPTs */
  49. static int disable;
  50. static int __init mfgpt_disable(char *s)
  51. {
  52. disable = 1;
  53. return 1;
  54. }
  55. __setup("nomfgpt", mfgpt_disable);
  56. /* Reset the MFGPT timers. This is required by some broken BIOSes which already
  57. * do the same and leave the system in an unstable state. TinyBIOS 0.98 is
  58. * affected at least (0.99 is OK with MFGPT workaround left to off).
  59. */
  60. static int __init mfgpt_fix(char *s)
  61. {
  62. u32 val, dummy;
  63. /* The following udocumented bit resets the MFGPT timers */
  64. val = 0xFF; dummy = 0;
  65. wrmsr(0x5140002B, val, dummy);
  66. return 1;
  67. }
  68. __setup("mfgptfix", mfgpt_fix);
  69. /*
  70. * Check whether any MFGPTs are available for the kernel to use. In most
  71. * cases, firmware that uses AMD's VSA code will claim all timers during
  72. * bootup; we certainly don't want to take them if they're already in use.
  73. * In other cases (such as with VSAless OpenFirmware), the system firmware
  74. * leaves timers available for us to use.
  75. */
  76. int __init geode_mfgpt_detect(void)
  77. {
  78. int count = 0, i;
  79. u16 val;
  80. if (disable) {
  81. printk(KERN_INFO "geode-mfgpt: Skipping MFGPT setup\n");
  82. return 0;
  83. }
  84. for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
  85. val = geode_mfgpt_read(i, MFGPT_REG_SETUP);
  86. if (!(val & MFGPT_SETUP_SETUP)) {
  87. mfgpt_timers[i].flags = F_AVAIL;
  88. count++;
  89. }
  90. }
  91. /* set up clock event device, if desired */
  92. i = mfgpt_timer_setup();
  93. return count;
  94. }
  95. int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
  96. {
  97. u32 msr, mask, value, dummy;
  98. int shift = (cmp == MFGPT_CMP1) ? 0 : 8;
  99. if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
  100. return -EIO;
  101. /*
  102. * The register maps for these are described in sections 6.17.1.x of
  103. * the AMD Geode CS5536 Companion Device Data Book.
  104. */
  105. switch (event) {
  106. case MFGPT_EVENT_RESET:
  107. /*
  108. * XXX: According to the docs, we cannot reset timers above
  109. * 6; that is, resets for 7 and 8 will be ignored. Is this
  110. * a problem? -dilinger
  111. */
  112. msr = MFGPT_NR_MSR;
  113. mask = 1 << (timer + 24);
  114. break;
  115. case MFGPT_EVENT_NMI:
  116. msr = MFGPT_NR_MSR;
  117. mask = 1 << (timer + shift);
  118. break;
  119. case MFGPT_EVENT_IRQ:
  120. msr = MFGPT_IRQ_MSR;
  121. mask = 1 << (timer + shift);
  122. break;
  123. default:
  124. return -EIO;
  125. }
  126. rdmsr(msr, value, dummy);
  127. if (enable)
  128. value |= mask;
  129. else
  130. value &= ~mask;
  131. wrmsr(msr, value, dummy);
  132. return 0;
  133. }
  134. int geode_mfgpt_set_irq(int timer, int cmp, int irq, int enable)
  135. {
  136. u32 val, dummy;
  137. int offset;
  138. if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
  139. return -EIO;
  140. if (geode_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
  141. return -EIO;
  142. rdmsr(MSR_PIC_ZSEL_LOW, val, dummy);
  143. offset = (timer % 4) * 4;
  144. val &= ~((0xF << offset) | (0xF << (offset + 16)));
  145. if (enable) {
  146. val |= (irq & 0x0F) << (offset);
  147. val |= (irq & 0x0F) << (offset + 16);
  148. }
  149. wrmsr(MSR_PIC_ZSEL_LOW, val, dummy);
  150. return 0;
  151. }
  152. static int mfgpt_get(int timer, struct module *owner)
  153. {
  154. mfgpt_timers[timer].flags &= ~F_AVAIL;
  155. mfgpt_timers[timer].owner = owner;
  156. printk(KERN_INFO "geode-mfgpt: Registered timer %d\n", timer);
  157. return timer;
  158. }
  159. int geode_mfgpt_alloc_timer(int timer, int domain, struct module *owner)
  160. {
  161. int i;
  162. if (!geode_get_dev_base(GEODE_DEV_MFGPT))
  163. return -ENODEV;
  164. if (timer >= MFGPT_MAX_TIMERS)
  165. return -EIO;
  166. if (timer < 0) {
  167. /* Try to find an available timer */
  168. for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
  169. if (mfgpt_timers[i].flags & F_AVAIL)
  170. return mfgpt_get(i, owner);
  171. if (i == 5 && domain == MFGPT_DOMAIN_WORKING)
  172. break;
  173. }
  174. } else {
  175. /* If they requested a specific timer, try to honor that */
  176. if (mfgpt_timers[timer].flags & F_AVAIL)
  177. return mfgpt_get(timer, owner);
  178. }
  179. /* No timers available - too bad */
  180. return -1;
  181. }
  182. #ifdef CONFIG_GEODE_MFGPT_TIMER
  183. /*
  184. * The MFPGT timers on the CS5536 provide us with suitable timers to use
  185. * as clock event sources - not as good as a HPET or APIC, but certainly
  186. * better then the PIT. This isn't a general purpose MFGPT driver, but
  187. * a simplified one designed specifically to act as a clock event source.
  188. * For full details about the MFGPT, please consult the CS5536 data sheet.
  189. */
  190. #include <linux/clocksource.h>
  191. #include <linux/clockchips.h>
  192. static unsigned int mfgpt_tick_mode = CLOCK_EVT_MODE_SHUTDOWN;
  193. static u16 mfgpt_event_clock;
  194. static int irq = 7;
  195. static int __init mfgpt_setup(char *str)
  196. {
  197. get_option(&str, &irq);
  198. return 1;
  199. }
  200. __setup("mfgpt_irq=", mfgpt_setup);
  201. static void mfgpt_disable_timer(u16 clock)
  202. {
  203. u16 val = geode_mfgpt_read(clock, MFGPT_REG_SETUP);
  204. geode_mfgpt_write(clock, MFGPT_REG_SETUP, val & ~MFGPT_SETUP_CNTEN);
  205. }
  206. static int mfgpt_next_event(unsigned long, struct clock_event_device *);
  207. static void mfgpt_set_mode(enum clock_event_mode, struct clock_event_device *);
  208. static struct clock_event_device mfgpt_clockevent = {
  209. .name = "mfgpt-timer",
  210. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  211. .set_mode = mfgpt_set_mode,
  212. .set_next_event = mfgpt_next_event,
  213. .rating = 250,
  214. .cpumask = CPU_MASK_ALL,
  215. .shift = 32
  216. };
  217. static void mfgpt_start_timer(u16 delta)
  218. {
  219. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_CMP2, (u16) delta);
  220. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
  221. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
  222. MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
  223. }
  224. static void mfgpt_set_mode(enum clock_event_mode mode,
  225. struct clock_event_device *evt)
  226. {
  227. mfgpt_disable_timer(mfgpt_event_clock);
  228. if (mode == CLOCK_EVT_MODE_PERIODIC)
  229. mfgpt_start_timer(MFGPT_PERIODIC);
  230. mfgpt_tick_mode = mode;
  231. }
  232. static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
  233. {
  234. mfgpt_start_timer(delta);
  235. return 0;
  236. }
  237. /* Assume (foolishly?), that this interrupt was due to our tick */
  238. static irqreturn_t mfgpt_tick(int irq, void *dev_id)
  239. {
  240. /* Turn off the clock (and clear the event) */
  241. mfgpt_disable_timer(mfgpt_event_clock);
  242. if (mfgpt_tick_mode == CLOCK_EVT_MODE_SHUTDOWN)
  243. return IRQ_HANDLED;
  244. /* Clear the counter */
  245. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
  246. /* Restart the clock in periodic mode */
  247. if (mfgpt_tick_mode == CLOCK_EVT_MODE_PERIODIC) {
  248. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
  249. MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
  250. }
  251. mfgpt_clockevent.event_handler(&mfgpt_clockevent);
  252. return IRQ_HANDLED;
  253. }
  254. static struct irqaction mfgptirq = {
  255. .handler = mfgpt_tick,
  256. .flags = IRQF_DISABLED | IRQF_NOBALANCING,
  257. .mask = CPU_MASK_NONE,
  258. .name = "mfgpt-timer"
  259. };
  260. static int __init mfgpt_timer_setup(void)
  261. {
  262. int timer, ret;
  263. u16 val;
  264. timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING,
  265. THIS_MODULE);
  266. if (timer < 0) {
  267. printk(KERN_ERR
  268. "mfgpt-timer: Could not allocate a MFPGT timer\n");
  269. return -ENODEV;
  270. }
  271. mfgpt_event_clock = timer;
  272. /* Set up the IRQ on the MFGPT side */
  273. if (geode_mfgpt_setup_irq(mfgpt_event_clock, MFGPT_CMP2, irq)) {
  274. printk(KERN_ERR "mfgpt-timer: Could not set up IRQ %d\n", irq);
  275. return -EIO;
  276. }
  277. /* And register it with the kernel */
  278. ret = setup_irq(irq, &mfgptirq);
  279. if (ret) {
  280. printk(KERN_ERR
  281. "mfgpt-timer: Unable to set up the interrupt.\n");
  282. goto err;
  283. }
  284. /* Set the clock scale and enable the event mode for CMP2 */
  285. val = MFGPT_SCALE | (3 << 8);
  286. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP, val);
  287. /* Set up the clock event */
  288. mfgpt_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC, 32);
  289. mfgpt_clockevent.min_delta_ns = clockevent_delta2ns(0xF,
  290. &mfgpt_clockevent);
  291. mfgpt_clockevent.max_delta_ns = clockevent_delta2ns(0xFFFE,
  292. &mfgpt_clockevent);
  293. printk(KERN_INFO
  294. "mfgpt-timer: registering the MFGT timer as a clock event.\n");
  295. clockevents_register_device(&mfgpt_clockevent);
  296. return 0;
  297. err:
  298. geode_mfgpt_release_irq(mfgpt_event_clock, MFGPT_CMP2, irq);
  299. printk(KERN_ERR
  300. "mfgpt-timer: Unable to set up the MFGPT clock source\n");
  301. return -EIO;
  302. }
  303. #endif