cs5535-mfgpt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Driver for the CS5535/CS5536 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. * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of version 2 of the GNU General Public License
  10. * as published by the Free Software Foundation.
  11. *
  12. * The MFGPTs are documented in AMD Geode CS5536 Companion Device Data Book.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/cs5535.h>
  20. #include <linux/slab.h>
  21. #define DRV_NAME "cs5535-mfgpt"
  22. static int mfgpt_reset_timers;
  23. module_param_named(mfgptfix, mfgpt_reset_timers, int, 0644);
  24. MODULE_PARM_DESC(mfgptfix, "Try to reset the MFGPT timers during init; "
  25. "required by some broken BIOSes (ie, TinyBIOS < 0.99) or kexec "
  26. "(1 = reset the MFGPT using an undocumented bit, "
  27. "2 = perform a soft reset by unconfiguring all timers); "
  28. "use what works best for you.");
  29. struct cs5535_mfgpt_timer {
  30. struct cs5535_mfgpt_chip *chip;
  31. int nr;
  32. };
  33. static struct cs5535_mfgpt_chip {
  34. DECLARE_BITMAP(avail, MFGPT_MAX_TIMERS);
  35. resource_size_t base;
  36. struct platform_device *pdev;
  37. spinlock_t lock;
  38. int initialized;
  39. } cs5535_mfgpt_chip;
  40. int cs5535_mfgpt_toggle_event(struct cs5535_mfgpt_timer *timer, int cmp,
  41. int event, int enable)
  42. {
  43. uint32_t msr, mask, value, dummy;
  44. int shift = (cmp == MFGPT_CMP1) ? 0 : 8;
  45. if (!timer) {
  46. WARN_ON(1);
  47. return -EIO;
  48. }
  49. /*
  50. * The register maps for these are described in sections 6.17.1.x of
  51. * the AMD Geode CS5536 Companion Device Data Book.
  52. */
  53. switch (event) {
  54. case MFGPT_EVENT_RESET:
  55. /*
  56. * XXX: According to the docs, we cannot reset timers above
  57. * 6; that is, resets for 7 and 8 will be ignored. Is this
  58. * a problem? -dilinger
  59. */
  60. msr = MSR_MFGPT_NR;
  61. mask = 1 << (timer->nr + 24);
  62. break;
  63. case MFGPT_EVENT_NMI:
  64. msr = MSR_MFGPT_NR;
  65. mask = 1 << (timer->nr + shift);
  66. break;
  67. case MFGPT_EVENT_IRQ:
  68. msr = MSR_MFGPT_IRQ;
  69. mask = 1 << (timer->nr + shift);
  70. break;
  71. default:
  72. return -EIO;
  73. }
  74. rdmsr(msr, value, dummy);
  75. if (enable)
  76. value |= mask;
  77. else
  78. value &= ~mask;
  79. wrmsr(msr, value, dummy);
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(cs5535_mfgpt_toggle_event);
  83. int cs5535_mfgpt_set_irq(struct cs5535_mfgpt_timer *timer, int cmp, int *irq,
  84. int enable)
  85. {
  86. uint32_t zsel, lpc, dummy;
  87. int shift;
  88. if (!timer) {
  89. WARN_ON(1);
  90. return -EIO;
  91. }
  92. /*
  93. * Unfortunately, MFGPTs come in pairs sharing their IRQ lines. If VSA
  94. * is using the same CMP of the timer's Siamese twin, the IRQ is set to
  95. * 2, and we mustn't use nor change it.
  96. * XXX: Likewise, 2 Linux drivers might clash if the 2nd overwrites the
  97. * IRQ of the 1st. This can only happen if forcing an IRQ, calling this
  98. * with *irq==0 is safe. Currently there _are_ no 2 drivers.
  99. */
  100. rdmsr(MSR_PIC_ZSEL_LOW, zsel, dummy);
  101. shift = ((cmp == MFGPT_CMP1 ? 0 : 4) + timer->nr % 4) * 4;
  102. if (((zsel >> shift) & 0xF) == 2)
  103. return -EIO;
  104. /* Choose IRQ: if none supplied, keep IRQ already set or use default */
  105. if (!*irq)
  106. *irq = (zsel >> shift) & 0xF;
  107. if (!*irq)
  108. *irq = CONFIG_CS5535_MFGPT_DEFAULT_IRQ;
  109. /* Can't use IRQ if it's 0 (=disabled), 2, or routed to LPC */
  110. if (*irq < 1 || *irq == 2 || *irq > 15)
  111. return -EIO;
  112. rdmsr(MSR_PIC_IRQM_LPC, lpc, dummy);
  113. if (lpc & (1 << *irq))
  114. return -EIO;
  115. /* All chosen and checked - go for it */
  116. if (cs5535_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
  117. return -EIO;
  118. if (enable) {
  119. zsel = (zsel & ~(0xF << shift)) | (*irq << shift);
  120. wrmsr(MSR_PIC_ZSEL_LOW, zsel, dummy);
  121. }
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(cs5535_mfgpt_set_irq);
  125. struct cs5535_mfgpt_timer *cs5535_mfgpt_alloc_timer(int timer_nr, int domain)
  126. {
  127. struct cs5535_mfgpt_chip *mfgpt = &cs5535_mfgpt_chip;
  128. struct cs5535_mfgpt_timer *timer = NULL;
  129. unsigned long flags;
  130. int max;
  131. if (!mfgpt->initialized)
  132. goto done;
  133. /* only allocate timers from the working domain if requested */
  134. if (domain == MFGPT_DOMAIN_WORKING)
  135. max = 6;
  136. else
  137. max = MFGPT_MAX_TIMERS;
  138. if (timer_nr >= max) {
  139. /* programmer error. silly programmers! */
  140. WARN_ON(1);
  141. goto done;
  142. }
  143. spin_lock_irqsave(&mfgpt->lock, flags);
  144. if (timer_nr < 0) {
  145. unsigned long t;
  146. /* try to find any available timer */
  147. t = find_first_bit(mfgpt->avail, max);
  148. /* set timer_nr to -1 if no timers available */
  149. timer_nr = t < max ? (int) t : -1;
  150. } else {
  151. /* check if the requested timer's available */
  152. if (!test_bit(timer_nr, mfgpt->avail))
  153. timer_nr = -1;
  154. }
  155. if (timer_nr >= 0)
  156. /* if timer_nr is not -1, it's an available timer */
  157. __clear_bit(timer_nr, mfgpt->avail);
  158. spin_unlock_irqrestore(&mfgpt->lock, flags);
  159. if (timer_nr < 0)
  160. goto done;
  161. timer = kmalloc(sizeof(*timer), GFP_KERNEL);
  162. if (!timer) {
  163. /* aw hell */
  164. spin_lock_irqsave(&mfgpt->lock, flags);
  165. __set_bit(timer_nr, mfgpt->avail);
  166. spin_unlock_irqrestore(&mfgpt->lock, flags);
  167. goto done;
  168. }
  169. timer->chip = mfgpt;
  170. timer->nr = timer_nr;
  171. dev_info(&mfgpt->pdev->dev, "registered timer %d\n", timer_nr);
  172. done:
  173. return timer;
  174. }
  175. EXPORT_SYMBOL_GPL(cs5535_mfgpt_alloc_timer);
  176. /*
  177. * XXX: This frees the timer memory, but never resets the actual hardware
  178. * timer. The old geode_mfgpt code did this; it would be good to figure
  179. * out a way to actually release the hardware timer. See comments below.
  180. */
  181. void cs5535_mfgpt_free_timer(struct cs5535_mfgpt_timer *timer)
  182. {
  183. unsigned long flags;
  184. uint16_t val;
  185. /* timer can be made available again only if never set up */
  186. val = cs5535_mfgpt_read(timer, MFGPT_REG_SETUP);
  187. if (!(val & MFGPT_SETUP_SETUP)) {
  188. spin_lock_irqsave(&timer->chip->lock, flags);
  189. __set_bit(timer->nr, timer->chip->avail);
  190. spin_unlock_irqrestore(&timer->chip->lock, flags);
  191. }
  192. kfree(timer);
  193. }
  194. EXPORT_SYMBOL_GPL(cs5535_mfgpt_free_timer);
  195. uint16_t cs5535_mfgpt_read(struct cs5535_mfgpt_timer *timer, uint16_t reg)
  196. {
  197. return inw(timer->chip->base + reg + (timer->nr * 8));
  198. }
  199. EXPORT_SYMBOL_GPL(cs5535_mfgpt_read);
  200. void cs5535_mfgpt_write(struct cs5535_mfgpt_timer *timer, uint16_t reg,
  201. uint16_t value)
  202. {
  203. outw(value, timer->chip->base + reg + (timer->nr * 8));
  204. }
  205. EXPORT_SYMBOL_GPL(cs5535_mfgpt_write);
  206. /*
  207. * This is a sledgehammer that resets all MFGPT timers. This is required by
  208. * some broken BIOSes which leave the system in an unstable state
  209. * (TinyBIOS 0.98, for example; fixed in 0.99). It's uncertain as to
  210. * whether or not this secret MSR can be used to release individual timers.
  211. * Jordan tells me that he and Mitch once played w/ it, but it's unclear
  212. * what the results of that were (and they experienced some instability).
  213. */
  214. static void reset_all_timers(void)
  215. {
  216. uint32_t val, dummy;
  217. /* The following undocumented bit resets the MFGPT timers */
  218. val = 0xFF; dummy = 0;
  219. wrmsr(MSR_MFGPT_SETUP, val, dummy);
  220. }
  221. /*
  222. * This is another sledgehammer to reset all MFGPT timers.
  223. * Instead of using the undocumented bit method it clears
  224. * IRQ, NMI and RESET settings.
  225. */
  226. static void soft_reset(void)
  227. {
  228. int i;
  229. struct cs5535_mfgpt_timer t;
  230. for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
  231. t.nr = i;
  232. cs5535_mfgpt_toggle_event(&t, MFGPT_CMP1, MFGPT_EVENT_RESET, 0);
  233. cs5535_mfgpt_toggle_event(&t, MFGPT_CMP2, MFGPT_EVENT_RESET, 0);
  234. cs5535_mfgpt_toggle_event(&t, MFGPT_CMP1, MFGPT_EVENT_NMI, 0);
  235. cs5535_mfgpt_toggle_event(&t, MFGPT_CMP2, MFGPT_EVENT_NMI, 0);
  236. cs5535_mfgpt_toggle_event(&t, MFGPT_CMP1, MFGPT_EVENT_IRQ, 0);
  237. cs5535_mfgpt_toggle_event(&t, MFGPT_CMP2, MFGPT_EVENT_IRQ, 0);
  238. }
  239. }
  240. /*
  241. * Check whether any MFGPTs are available for the kernel to use. In most
  242. * cases, firmware that uses AMD's VSA code will claim all timers during
  243. * bootup; we certainly don't want to take them if they're already in use.
  244. * In other cases (such as with VSAless OpenFirmware), the system firmware
  245. * leaves timers available for us to use.
  246. */
  247. static int scan_timers(struct cs5535_mfgpt_chip *mfgpt)
  248. {
  249. struct cs5535_mfgpt_timer timer = { .chip = mfgpt };
  250. unsigned long flags;
  251. int timers = 0;
  252. uint16_t val;
  253. int i;
  254. /* bios workaround */
  255. if (mfgpt_reset_timers == 1)
  256. reset_all_timers();
  257. else if (mfgpt_reset_timers == 2)
  258. soft_reset();
  259. /* just to be safe, protect this section w/ lock */
  260. spin_lock_irqsave(&mfgpt->lock, flags);
  261. for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
  262. timer.nr = i;
  263. val = cs5535_mfgpt_read(&timer, MFGPT_REG_SETUP);
  264. if (!(val & MFGPT_SETUP_SETUP) || mfgpt_reset_timers == 2) {
  265. __set_bit(i, mfgpt->avail);
  266. timers++;
  267. }
  268. }
  269. spin_unlock_irqrestore(&mfgpt->lock, flags);
  270. return timers;
  271. }
  272. static int cs5535_mfgpt_probe(struct platform_device *pdev)
  273. {
  274. struct resource *res;
  275. int err = -EIO, t;
  276. if (mfgpt_reset_timers < 0 || mfgpt_reset_timers > 2) {
  277. dev_err(&pdev->dev, "Bad mfgpt_reset_timers value: %i\n",
  278. mfgpt_reset_timers);
  279. goto done;
  280. }
  281. /* There are two ways to get the MFGPT base address; one is by
  282. * fetching it from MSR_LBAR_MFGPT, the other is by reading the
  283. * PCI BAR info. The latter method is easier (especially across
  284. * different architectures), so we'll stick with that for now. If
  285. * it turns out to be unreliable in the face of crappy BIOSes, we
  286. * can always go back to using MSRs.. */
  287. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  288. if (!res) {
  289. dev_err(&pdev->dev, "can't fetch device resource info\n");
  290. goto done;
  291. }
  292. if (!request_region(res->start, resource_size(res), pdev->name)) {
  293. dev_err(&pdev->dev, "can't request region\n");
  294. goto done;
  295. }
  296. /* set up the driver-specific struct */
  297. cs5535_mfgpt_chip.base = res->start;
  298. cs5535_mfgpt_chip.pdev = pdev;
  299. spin_lock_init(&cs5535_mfgpt_chip.lock);
  300. dev_info(&pdev->dev, "reserved resource region %pR\n", res);
  301. /* detect the available timers */
  302. t = scan_timers(&cs5535_mfgpt_chip);
  303. dev_info(&pdev->dev, "%d MFGPT timers available\n", t);
  304. cs5535_mfgpt_chip.initialized = 1;
  305. return 0;
  306. done:
  307. return err;
  308. }
  309. static struct platform_driver cs5535_mfgpt_driver = {
  310. .driver = {
  311. .name = DRV_NAME,
  312. },
  313. .probe = cs5535_mfgpt_probe,
  314. };
  315. static int __init cs5535_mfgpt_init(void)
  316. {
  317. return platform_driver_register(&cs5535_mfgpt_driver);
  318. }
  319. module_init(cs5535_mfgpt_init);
  320. MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
  321. MODULE_DESCRIPTION("CS5535/CS5536 MFGPT timer driver");
  322. MODULE_LICENSE("GPL");
  323. MODULE_ALIAS("platform:" DRV_NAME);