cs5535-mfgpt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. 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 __init 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. * Check whether any MFGPTs are available for the kernel to use. In most
  221. * cases, firmware that uses AMD's VSA code will claim all timers during
  222. * bootup; we certainly don't want to take them if they're already in use.
  223. * In other cases (such as with VSAless OpenFirmware), the system firmware
  224. * leaves timers available for us to use.
  225. */
  226. static int __init scan_timers(struct cs5535_mfgpt_chip *mfgpt)
  227. {
  228. struct cs5535_mfgpt_timer timer = { .chip = mfgpt };
  229. unsigned long flags;
  230. int timers = 0;
  231. uint16_t val;
  232. int i;
  233. /* bios workaround */
  234. if (mfgpt_reset_timers)
  235. reset_all_timers();
  236. /* just to be safe, protect this section w/ lock */
  237. spin_lock_irqsave(&mfgpt->lock, flags);
  238. for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
  239. timer.nr = i;
  240. val = cs5535_mfgpt_read(&timer, MFGPT_REG_SETUP);
  241. if (!(val & MFGPT_SETUP_SETUP)) {
  242. __set_bit(i, mfgpt->avail);
  243. timers++;
  244. }
  245. }
  246. spin_unlock_irqrestore(&mfgpt->lock, flags);
  247. return timers;
  248. }
  249. static int __init cs5535_mfgpt_probe(struct pci_dev *pdev,
  250. const struct pci_device_id *pci_id)
  251. {
  252. int err, t;
  253. /* There are two ways to get the MFGPT base address; one is by
  254. * fetching it from MSR_LBAR_MFGPT, the other is by reading the
  255. * PCI BAR info. The latter method is easier (especially across
  256. * different architectures), so we'll stick with that for now. If
  257. * it turns out to be unreliable in the face of crappy BIOSes, we
  258. * can always go back to using MSRs.. */
  259. err = pci_enable_device_io(pdev);
  260. if (err) {
  261. dev_err(&pdev->dev, "can't enable device IO\n");
  262. goto done;
  263. }
  264. err = pci_request_region(pdev, MFGPT_BAR, DRV_NAME);
  265. if (err) {
  266. dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", MFGPT_BAR);
  267. goto done;
  268. }
  269. /* set up the driver-specific struct */
  270. cs5535_mfgpt_chip.base = pci_resource_start(pdev, MFGPT_BAR);
  271. cs5535_mfgpt_chip.pdev = pdev;
  272. spin_lock_init(&cs5535_mfgpt_chip.lock);
  273. dev_info(&pdev->dev, "allocated PCI BAR #%d: base 0x%llx\n", MFGPT_BAR,
  274. (unsigned long long) cs5535_mfgpt_chip.base);
  275. /* detect the available timers */
  276. t = scan_timers(&cs5535_mfgpt_chip);
  277. dev_info(&pdev->dev, DRV_NAME ": %d MFGPT timers available\n", t);
  278. cs5535_mfgpt_chip.initialized = 1;
  279. return 0;
  280. done:
  281. return err;
  282. }
  283. static struct pci_device_id cs5535_mfgpt_pci_tbl[] = {
  284. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
  285. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
  286. { 0, },
  287. };
  288. MODULE_DEVICE_TABLE(pci, cs5535_mfgpt_pci_tbl);
  289. /*
  290. * Just like with the cs5535-gpio driver, we can't use the standard PCI driver
  291. * registration stuff. It only allows only one driver to bind to each PCI
  292. * device, and we want the GPIO and MFGPT drivers to be able to share a PCI
  293. * device. Instead, we manually scan for the PCI device, request a single
  294. * region, and keep track of the devices that we're using.
  295. */
  296. static int __init cs5535_mfgpt_scan_pci(void)
  297. {
  298. struct pci_dev *pdev;
  299. int err = -ENODEV;
  300. int i;
  301. for (i = 0; i < ARRAY_SIZE(cs5535_mfgpt_pci_tbl); i++) {
  302. pdev = pci_get_device(cs5535_mfgpt_pci_tbl[i].vendor,
  303. cs5535_mfgpt_pci_tbl[i].device, NULL);
  304. if (pdev) {
  305. err = cs5535_mfgpt_probe(pdev,
  306. &cs5535_mfgpt_pci_tbl[i]);
  307. if (err)
  308. pci_dev_put(pdev);
  309. /* we only support a single CS5535/6 southbridge */
  310. break;
  311. }
  312. }
  313. return err;
  314. }
  315. static int __init cs5535_mfgpt_init(void)
  316. {
  317. return cs5535_mfgpt_scan_pci();
  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");