mfgpt_32.c 9.4 KB

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