cs5535-mfgpt.c 10 KB

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