cs5535-mfgpt.c 9.8 KB

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