1 /* Driver/API for AMD Geode Multi-Function General Purpose Timers (MFGPT)
2  *
3  * Copyright (C) 2006, Advanced Micro Devices, Inc.
4  * Copyright (C) 2007, Andres Salomon <dilinger@debian.org>
5  * Backported to 2.4 by Willy Tarreau <w@1wt.eu>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version
10  * 2 of the License, or (at your option) any later version.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/pci.h>
16 #include <linux/pci_ids.h>
17 #include <linux/interrupt.h>
18 #include <asm/geode-mfgpt.h>
19 #include <asm/io.h>
20 #include <asm/msr.h>
21 
22 /* for pci_iomap/pci_iounmap */
23 #include "mfgpt-compat.h"
24 
25 static void *mfgpt_iobase;
26 
27 static struct mfgpt_timer_t {
28 	unsigned int avail:1;
29 } mfgpt_timers[MFGPT_MAX_TIMERS];
30 
geode_mfgpt_write(int i,u16 r,u16 v)31 void geode_mfgpt_write(int i, u16 r, u16 v)
32 {
33 	outw(v, (unsigned long)(mfgpt_iobase + (r + (i * 8))));
34 }
35 EXPORT_SYMBOL(geode_mfgpt_write);
36 
geode_mfgpt_read(int i,u16 r)37 u16 geode_mfgpt_read(int i, u16 r)
38 {
39 	return inw((unsigned long)(mfgpt_iobase + (r + (i * 8))));
40 }
41 EXPORT_SYMBOL(geode_mfgpt_read);
42 
geode_mfgpt_toggle_event(int timer,int cmp,int event,int setup)43 int geode_mfgpt_toggle_event(int timer, int cmp, int event, int setup)
44 {
45 	u32 msr, mask, value, dummy;
46 	int shift = (cmp == MFGPT_CMP1) ? 0 : 8;
47 
48 	if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
49 		return -EIO;
50 
51 	switch(event) {
52 	case MFGPT_EVENT_RESET:
53 		msr = MSR_MFGPT_NR;
54 		mask = 1 << (timer + 24);
55 		break;
56 
57 	case MFGPT_EVENT_NMI:
58 		msr = MSR_MFGPT_NR;
59 		mask = 1 << (timer + shift);
60 		break;
61 
62 	case MFGPT_EVENT_IRQ:
63 		msr = MSR_MFGPT_IRQ;
64 		mask = 1 << (timer + shift);
65 		break;
66 
67 	default:
68 		return -EIO;
69 	}
70 
71 	rdmsr(msr, value, dummy);
72 
73 	if (setup)
74 		value |= mask;
75 	else
76 		value &= ~mask;
77 
78 	wrmsr(msr, value, dummy);
79 	return 0;
80 }
81 
82 EXPORT_SYMBOL(geode_mfgpt_toggle_event);
83 
84 /* Allow for disabling of MFGPTs */
85 static int disable;
mfgpt_disable(char * s)86 static int __init mfgpt_disable(char *s)
87 {
88 	disable = 1;
89 	return 1;
90 }
91 __setup("nomfgpt", mfgpt_disable);
92 
93 /* Reset the MFGPT timers. This is required by some broken BIOSes which already
94  * do the same and leave the system in an unstable state. TinyBIOS 0.98 is
95  * affected at least (0.99 is OK with MFGPT workaround left to off).
96  */
mfgpt_fix(char * s)97 static int __init mfgpt_fix(char *s)
98 {
99 	u32 val, dummy;
100 
101 	/* The following udocumented bit resets the MFGPT timers */
102 	val = 0xFF; dummy = 0;
103 	wrmsr(MSR_MFGPT_SETUP, val, dummy);
104 	return 1;
105 }
106 __setup("mfgptfix", mfgpt_fix);
107 
108 
109 /*
110  * Check whether any MFGPTs are available for the kernel to use.  In most
111  * cases, firmware that uses AMD's VSA code will claim all timers during
112  * bootup; we certainly don't want to take them if they're already in use.
113  * In other cases (such as with VSAless OpenFirmware), the system firmware
114  * leaves timers available for us to use.
115  */
116 
117 static struct pci_device_id geode_sbdevs[] = {
118 	{ PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
119 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) }
120 };
121 
122 static int timers = -1;
123 
geode_mfgpt_detect(void)124 static void geode_mfgpt_detect(void)
125 {
126 	struct pci_dev *pdev = NULL;
127 	int i, ret, dev, count = 0;
128 	u16 val;
129 
130 	timers = 0;
131 
132 	if (disable) {
133 		printk(KERN_INFO "geode-mfgpt:  MFGPT support is disabled\n");
134 		goto done;
135 	}
136 
137 	if (!is_geode()) {
138 		printk(KERN_INFO "geode-mfgpt:  Not a Geode GX/LX processor\n");
139 		goto done;
140 	}
141 
142 	for (dev = 0; dev < ARRAY_SIZE(geode_sbdevs); dev++) {
143 		pdev = pci_find_device(geode_sbdevs[dev].vendor,
144 				       geode_sbdevs[dev].device, NULL);
145 
146 		if (pdev != NULL)
147 			break;
148 	}
149 
150 	if (pdev == NULL) {
151 		printk(KERN_ERR "geode-mfgpt:  No PCI devices found\n");
152 		goto done;
153 	}
154 
155 	if ((ret = pci_enable_device_bars(pdev, 1 << MFGPT_PCI_BAR)))
156 		goto err;
157 
158 	if ((ret = pci_request_region(pdev, MFGPT_PCI_BAR, "geode-mfgpt")))
159 		goto err;
160 
161 	mfgpt_iobase = pci_iomap(pdev, MFGPT_PCI_BAR, 64);
162 
163 	if (mfgpt_iobase == NULL)
164 		goto ereq;
165 
166 	for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
167 		val = geode_mfgpt_read(i, MFGPT_REG_SETUP);
168 		if (!(val & MFGPT_SETUP_SETUP)) {
169 			mfgpt_timers[i].avail = 1;
170 			timers++;
171 		}
172 	}
173 
174  done:
175 	printk(KERN_INFO "geode-mfgpt:  %d MFGPT timers available.\n", timers);
176 	return;
177  ereq:
178 	pci_release_region(pdev, MFGPT_PCI_BAR);
179  err:
180 	printk(KERN_ERR  "geode-mfgpt:  Error initalizing the timers\n");
181 	return;
182 }
183 
mfgpt_get(int timer)184 static int mfgpt_get(int timer)
185 {
186 	mfgpt_timers[timer].avail = 0;
187 	printk(KERN_INFO "geode-mfgpt:  Registered timer %d\n", timer);
188 	return timer;
189 }
190 
geode_mfgpt_alloc_timer(int timer,int domain)191 int geode_mfgpt_alloc_timer(int timer, int domain)
192 {
193 	int i;
194 
195 	if (timers == -1) {
196 		/* timers haven't been detected yet */
197 		geode_mfgpt_detect();
198 	}
199 
200 	if (!timers)
201 		return -1;
202 
203 	if (timer >= MFGPT_MAX_TIMERS)
204 		return -1;
205 
206 	if (timer < 0) {
207 		/* Try to find an available timer */
208 		for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
209 			if (mfgpt_timers[i].avail)
210 				return mfgpt_get(i);
211 
212 			if (i == 5 && domain == MFGPT_DOMAIN_WORKING)
213 				break;
214 		}
215 	} else {
216 		/* If they requested a specific timer, try to honor that */
217 		if (mfgpt_timers[timer].avail)
218 			return mfgpt_get(timer);
219 	}
220 
221 	/* No timers available - too bad */
222 	return -1;
223 }
224 EXPORT_SYMBOL(geode_mfgpt_alloc_timer);
225