cs5535-mfgpt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/pci.h>
  19. #include <linux/cs5535.h>
  20. #include <linux/slab.h>
  21. #define DRV_NAME "cs5535-mfgpt"
  22. #define MFGPT_BAR 2
  23. static int mfgpt_reset_timers;
  24. module_param_named(mfgptfix, mfgpt_reset_timers, int, 0644);
  25. MODULE_PARM_DESC(mfgptfix, "Reset the MFGPT timers during init; "
  26. "required by some broken BIOSes (ie, TinyBIOS < 0.99).");
  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 pci_dev *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(sizeof(*timer), GFP_KERNEL);
  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. kfree(timer);
  182. }
  183. EXPORT_SYMBOL_GPL(cs5535_mfgpt_free_timer);
  184. uint16_t cs5535_mfgpt_read(struct cs5535_mfgpt_timer *timer, uint16_t reg)
  185. {
  186. return inw(timer->chip->base + reg + (timer->nr * 8));
  187. }
  188. EXPORT_SYMBOL_GPL(cs5535_mfgpt_read);
  189. void cs5535_mfgpt_write(struct cs5535_mfgpt_timer *timer, uint16_t reg,
  190. uint16_t value)
  191. {
  192. outw(value, timer->chip->base + reg + (timer->nr * 8));
  193. }
  194. EXPORT_SYMBOL_GPL(cs5535_mfgpt_write);
  195. /*
  196. * This is a sledgehammer that resets all MFGPT timers. This is required by
  197. * some broken BIOSes which leave the system in an unstable state
  198. * (TinyBIOS 0.98, for example; fixed in 0.99). It's uncertain as to
  199. * whether or not this secret MSR can be used to release individual timers.
  200. * Jordan tells me that he and Mitch once played w/ it, but it's unclear
  201. * what the results of that were (and they experienced some instability).
  202. */
  203. static void __init reset_all_timers(void)
  204. {
  205. uint32_t val, dummy;
  206. /* The following undocumented bit resets the MFGPT timers */
  207. val = 0xFF; dummy = 0;
  208. wrmsr(MSR_MFGPT_SETUP, val, dummy);
  209. }
  210. /*
  211. * Check whether any MFGPTs are available for the kernel to use. In most
  212. * cases, firmware that uses AMD's VSA code will claim all timers during
  213. * bootup; we certainly don't want to take them if they're already in use.
  214. * In other cases (such as with VSAless OpenFirmware), the system firmware
  215. * leaves timers available for us to use.
  216. */
  217. static int __init scan_timers(struct cs5535_mfgpt_chip *mfgpt)
  218. {
  219. struct cs5535_mfgpt_timer timer = { .chip = mfgpt };
  220. unsigned long flags;
  221. int timers = 0;
  222. uint16_t val;
  223. int i;
  224. /* bios workaround */
  225. if (mfgpt_reset_timers)
  226. reset_all_timers();
  227. /* just to be safe, protect this section w/ lock */
  228. spin_lock_irqsave(&mfgpt->lock, flags);
  229. for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
  230. timer.nr = i;
  231. val = cs5535_mfgpt_read(&timer, MFGPT_REG_SETUP);
  232. if (!(val & MFGPT_SETUP_SETUP)) {
  233. __set_bit(i, mfgpt->avail);
  234. timers++;
  235. }
  236. }
  237. spin_unlock_irqrestore(&mfgpt->lock, flags);
  238. return timers;
  239. }
  240. static int __init cs5535_mfgpt_probe(struct pci_dev *pdev,
  241. const struct pci_device_id *pci_id)
  242. {
  243. int err, t;
  244. /* There are two ways to get the MFGPT base address; one is by
  245. * fetching it from MSR_LBAR_MFGPT, the other is by reading the
  246. * PCI BAR info. The latter method is easier (especially across
  247. * different architectures), so we'll stick with that for now. If
  248. * it turns out to be unreliable in the face of crappy BIOSes, we
  249. * can always go back to using MSRs.. */
  250. err = pci_enable_device_io(pdev);
  251. if (err) {
  252. dev_err(&pdev->dev, "can't enable device IO\n");
  253. goto done;
  254. }
  255. err = pci_request_region(pdev, MFGPT_BAR, DRV_NAME);
  256. if (err) {
  257. dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", MFGPT_BAR);
  258. goto done;
  259. }
  260. /* set up the driver-specific struct */
  261. cs5535_mfgpt_chip.base = pci_resource_start(pdev, MFGPT_BAR);
  262. cs5535_mfgpt_chip.pdev = pdev;
  263. spin_lock_init(&cs5535_mfgpt_chip.lock);
  264. dev_info(&pdev->dev, "allocated PCI BAR #%d: base 0x%llx\n", MFGPT_BAR,
  265. (unsigned long long) cs5535_mfgpt_chip.base);
  266. /* detect the available timers */
  267. t = scan_timers(&cs5535_mfgpt_chip);
  268. dev_info(&pdev->dev, DRV_NAME ": %d MFGPT timers available\n", t);
  269. cs5535_mfgpt_chip.initialized = 1;
  270. return 0;
  271. done:
  272. return err;
  273. }
  274. static struct pci_device_id cs5535_mfgpt_pci_tbl[] = {
  275. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
  276. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
  277. { 0, },
  278. };
  279. MODULE_DEVICE_TABLE(pci, cs5535_mfgpt_pci_tbl);
  280. /*
  281. * Just like with the cs5535-gpio driver, we can't use the standard PCI driver
  282. * registration stuff. It only allows only one driver to bind to each PCI
  283. * device, and we want the GPIO and MFGPT drivers to be able to share a PCI
  284. * device. Instead, we manually scan for the PCI device, request a single
  285. * region, and keep track of the devices that we're using.
  286. */
  287. static int __init cs5535_mfgpt_scan_pci(void)
  288. {
  289. struct pci_dev *pdev;
  290. int err = -ENODEV;
  291. int i;
  292. for (i = 0; i < ARRAY_SIZE(cs5535_mfgpt_pci_tbl); i++) {
  293. pdev = pci_get_device(cs5535_mfgpt_pci_tbl[i].vendor,
  294. cs5535_mfgpt_pci_tbl[i].device, NULL);
  295. if (pdev) {
  296. err = cs5535_mfgpt_probe(pdev,
  297. &cs5535_mfgpt_pci_tbl[i]);
  298. if (err)
  299. pci_dev_put(pdev);
  300. /* we only support a single CS5535/6 southbridge */
  301. break;
  302. }
  303. }
  304. return err;
  305. }
  306. static int __init cs5535_mfgpt_init(void)
  307. {
  308. return cs5535_mfgpt_scan_pci();
  309. }
  310. module_init(cs5535_mfgpt_init);
  311. MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
  312. MODULE_DESCRIPTION("CS5535/CS5536 MFGPT timer driver");
  313. MODULE_LICENSE("GPL");