mfgpt_32.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 32Khz input clock - its 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
  17. * and the maximum interval:
  18. *
  19. * Divisor Hz Min Delta (S) Max Delta (S)
  20. * 1 32000 .0005 2.048
  21. * 2 16000 .001 4.096
  22. * 4 8000 .002 8.192
  23. * 8 4000 .004 16.384
  24. * 16 2000 .008 32.768
  25. * 32 1000 .016 65.536
  26. * 64 500 .032 131.072
  27. * 128 250 .064 262.144
  28. * 256 125 .128 524.288
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/module.h>
  33. #include <asm/geode.h>
  34. #define F_AVAIL 0x01
  35. static struct mfgpt_timer_t {
  36. int flags;
  37. struct module *owner;
  38. } mfgpt_timers[MFGPT_MAX_TIMERS];
  39. /* Selected from the table above */
  40. #define MFGPT_DIVISOR 16
  41. #define MFGPT_SCALE 4 /* divisor = 2^(scale) */
  42. #define MFGPT_HZ (32000 / MFGPT_DIVISOR)
  43. #define MFGPT_PERIODIC (MFGPT_HZ / HZ)
  44. #ifdef CONFIG_GEODE_MFGPT_TIMER
  45. static int __init mfgpt_timer_setup(void);
  46. #else
  47. #define mfgpt_timer_setup() (0)
  48. #endif
  49. /* Allow for disabling of MFGPTs */
  50. static int disable;
  51. static int __init mfgpt_disable(char *s)
  52. {
  53. disable = 1;
  54. return 1;
  55. }
  56. __setup("nomfgpt", mfgpt_disable);
  57. /* Reset the MFGPT timers. This is required by some broken BIOSes which already
  58. * do the same and leave the system in an unstable state. TinyBIOS 0.98 is
  59. * affected at least (0.99 is OK with MFGPT workaround left to off).
  60. */
  61. static int __init mfgpt_fix(char *s)
  62. {
  63. u32 val, dummy;
  64. /* The following udocumented bit resets the MFGPT timers */
  65. val = 0xFF; dummy = 0;
  66. wrmsr(0x5140002B, val, dummy);
  67. return 1;
  68. }
  69. __setup("mfgptfix", mfgpt_fix);
  70. /*
  71. * Check whether any MFGPTs are available for the kernel to use. In most
  72. * cases, firmware that uses AMD's VSA code will claim all timers during
  73. * bootup; we certainly don't want to take them if they're already in use.
  74. * In other cases (such as with VSAless OpenFirmware), the system firmware
  75. * leaves timers available for us to use.
  76. */
  77. int __init geode_mfgpt_detect(void)
  78. {
  79. int count = 0, i;
  80. u16 val;
  81. if (disable) {
  82. printk(KERN_INFO "geode-mfgpt: Skipping MFGPT setup\n");
  83. return 0;
  84. }
  85. for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
  86. val = geode_mfgpt_read(i, MFGPT_REG_SETUP);
  87. if (!(val & MFGPT_SETUP_SETUP)) {
  88. mfgpt_timers[i].flags = F_AVAIL;
  89. count++;
  90. }
  91. }
  92. /* set up clock event device, if desired */
  93. i = mfgpt_timer_setup();
  94. return count;
  95. }
  96. int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
  97. {
  98. u32 msr, mask, value, dummy;
  99. int shift = (cmp == MFGPT_CMP1) ? 0 : 8;
  100. if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
  101. return -EIO;
  102. /*
  103. * The register maps for these are described in sections 6.17.1.x of
  104. * the AMD Geode CS5536 Companion Device Data Book.
  105. */
  106. switch (event) {
  107. case MFGPT_EVENT_RESET:
  108. /*
  109. * XXX: According to the docs, we cannot reset timers above
  110. * 6; that is, resets for 7 and 8 will be ignored. Is this
  111. * a problem? -dilinger
  112. */
  113. msr = MFGPT_NR_MSR;
  114. mask = 1 << (timer + 24);
  115. break;
  116. case MFGPT_EVENT_NMI:
  117. msr = MFGPT_NR_MSR;
  118. mask = 1 << (timer + shift);
  119. break;
  120. case MFGPT_EVENT_IRQ:
  121. msr = MFGPT_IRQ_MSR;
  122. mask = 1 << (timer + shift);
  123. break;
  124. default:
  125. return -EIO;
  126. }
  127. rdmsr(msr, value, dummy);
  128. if (enable)
  129. value |= mask;
  130. else
  131. value &= ~mask;
  132. wrmsr(msr, value, dummy);
  133. return 0;
  134. }
  135. int geode_mfgpt_set_irq(int timer, int cmp, int irq, int enable)
  136. {
  137. u32 val, dummy;
  138. int offset;
  139. if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
  140. return -EIO;
  141. if (geode_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
  142. return -EIO;
  143. rdmsr(MSR_PIC_ZSEL_LOW, val, dummy);
  144. offset = (timer % 4) * 4;
  145. val &= ~((0xF << offset) | (0xF << (offset + 16)));
  146. if (enable) {
  147. val |= (irq & 0x0F) << (offset);
  148. val |= (irq & 0x0F) << (offset + 16);
  149. }
  150. wrmsr(MSR_PIC_ZSEL_LOW, val, dummy);
  151. return 0;
  152. }
  153. static int mfgpt_get(int timer, struct module *owner)
  154. {
  155. mfgpt_timers[timer].flags &= ~F_AVAIL;
  156. mfgpt_timers[timer].owner = owner;
  157. printk(KERN_INFO "geode-mfgpt: Registered timer %d\n", timer);
  158. return timer;
  159. }
  160. int geode_mfgpt_alloc_timer(int timer, int domain, struct module *owner)
  161. {
  162. int i;
  163. if (!geode_get_dev_base(GEODE_DEV_MFGPT))
  164. return -ENODEV;
  165. if (timer >= MFGPT_MAX_TIMERS)
  166. return -EIO;
  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].flags & F_AVAIL)
  171. return mfgpt_get(i, owner);
  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].flags & F_AVAIL)
  178. return mfgpt_get(timer, owner);
  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 inline void mfgpt_disable_timer(u16 clock)
  203. {
  204. u16 val = geode_mfgpt_read(clock, MFGPT_REG_SETUP);
  205. geode_mfgpt_write(clock, MFGPT_REG_SETUP, val & ~MFGPT_SETUP_CNTEN);
  206. }
  207. static int mfgpt_next_event(unsigned long, struct clock_event_device *);
  208. static void mfgpt_set_mode(enum clock_event_mode, struct clock_event_device *);
  209. static struct clock_event_device mfgpt_clockevent = {
  210. .name = "mfgpt-timer",
  211. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  212. .set_mode = mfgpt_set_mode,
  213. .set_next_event = mfgpt_next_event,
  214. .rating = 250,
  215. .cpumask = CPU_MASK_ALL,
  216. .shift = 32
  217. };
  218. static inline void mfgpt_start_timer(u16 clock, u16 delta)
  219. {
  220. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_CMP2, (u16) delta);
  221. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
  222. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
  223. MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
  224. }
  225. static void mfgpt_set_mode(enum clock_event_mode mode,
  226. struct clock_event_device *evt)
  227. {
  228. mfgpt_disable_timer(mfgpt_event_clock);
  229. if (mode == CLOCK_EVT_MODE_PERIODIC)
  230. mfgpt_start_timer(mfgpt_event_clock, MFGPT_PERIODIC);
  231. mfgpt_tick_mode = mode;
  232. }
  233. static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
  234. {
  235. mfgpt_start_timer(mfgpt_event_clock, delta);
  236. return 0;
  237. }
  238. /* Assume (foolishly?), that this interrupt was due to our tick */
  239. static irqreturn_t mfgpt_tick(int irq, void *dev_id)
  240. {
  241. /* Turn off the clock (and clear the event) */
  242. mfgpt_disable_timer(mfgpt_event_clock);
  243. if (mfgpt_tick_mode == CLOCK_EVT_MODE_SHUTDOWN)
  244. return IRQ_HANDLED;
  245. /* Clear the counter */
  246. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
  247. /* Restart the clock in periodic mode */
  248. if (mfgpt_tick_mode == CLOCK_EVT_MODE_PERIODIC) {
  249. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
  250. MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
  251. }
  252. mfgpt_clockevent.event_handler(&mfgpt_clockevent);
  253. return IRQ_HANDLED;
  254. }
  255. static struct irqaction mfgptirq = {
  256. .handler = mfgpt_tick,
  257. .flags = IRQF_DISABLED | IRQF_NOBALANCING,
  258. .mask = CPU_MASK_NONE,
  259. .name = "mfgpt-timer"
  260. };
  261. static int __init mfgpt_timer_setup(void)
  262. {
  263. int timer, ret;
  264. u16 val;
  265. timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING,
  266. THIS_MODULE);
  267. if (timer < 0) {
  268. printk(KERN_ERR
  269. "mfgpt-timer: Could not allocate a MFPGT timer\n");
  270. return -ENODEV;
  271. }
  272. mfgpt_event_clock = timer;
  273. /* Set up the IRQ on the MFGPT side */
  274. if (geode_mfgpt_setup_irq(mfgpt_event_clock, MFGPT_CMP2, irq)) {
  275. printk(KERN_ERR "mfgpt-timer: Could not set up IRQ %d\n", irq);
  276. return -EIO;
  277. }
  278. /* And register it with the kernel */
  279. ret = setup_irq(irq, &mfgptirq);
  280. if (ret) {
  281. printk(KERN_ERR
  282. "mfgpt-timer: Unable to set up the interrupt.\n");
  283. goto err;
  284. }
  285. /* Set the clock scale and enable the event mode for CMP2 */
  286. val = MFGPT_SCALE | (3 << 8);
  287. geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP, val);
  288. /* Set up the clock event */
  289. mfgpt_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC, 32);
  290. mfgpt_clockevent.min_delta_ns = clockevent_delta2ns(0xF,
  291. &mfgpt_clockevent);
  292. mfgpt_clockevent.max_delta_ns = clockevent_delta2ns(0xFFFE,
  293. &mfgpt_clockevent);
  294. printk(KERN_INFO
  295. "mfgpt-timer: registering the MFGT timer as a clock event.\n");
  296. clockevents_register_device(&mfgpt_clockevent);
  297. return 0;
  298. err:
  299. geode_mfgpt_release_irq(mfgpt_event_clock, MFGPT_CMP2, irq);
  300. printk(KERN_ERR
  301. "mfgpt-timer: Unable to set up the MFGPT clock source\n");
  302. return -EIO;
  303. }
  304. #endif