mfgpt_32.c 10 KB

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