hwmon: (k8temp) Warn about fam F rev F errata
[linux-2.6] / drivers / hwmon / k8temp.c
1 /*
2  * k8temp.c - Linux kernel module for hardware monitoring
3  *
4  * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
5  *
6  * Inspired from the w83785 and amd756 drivers.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/pci.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 #include <asm/processor.h>
35
36 #define TEMP_FROM_REG(val)      (((((val) >> 16) & 0xff) - 49) * 1000)
37 #define REG_TEMP        0xe4
38 #define SEL_PLACE       0x40
39 #define SEL_CORE        0x04
40
41 struct k8temp_data {
42         struct device *hwmon_dev;
43         struct mutex update_lock;
44         const char *name;
45         char valid;             /* zero until following fields are valid */
46         unsigned long last_updated;     /* in jiffies */
47
48         /* registers values */
49         u8 sensorsp;            /* sensor presence bits - SEL_CORE & SEL_PLACE */
50         u32 temp[2][2];         /* core, place */
51 };
52
53 static struct k8temp_data *k8temp_update_device(struct device *dev)
54 {
55         struct k8temp_data *data = dev_get_drvdata(dev);
56         struct pci_dev *pdev = to_pci_dev(dev);
57         u8 tmp;
58
59         mutex_lock(&data->update_lock);
60
61         if (!data->valid
62             || time_after(jiffies, data->last_updated + HZ)) {
63                 pci_read_config_byte(pdev, REG_TEMP, &tmp);
64                 tmp &= ~(SEL_PLACE | SEL_CORE);         /* Select sensor 0, core0 */
65                 pci_write_config_byte(pdev, REG_TEMP, tmp);
66                 pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
67
68                 if (data->sensorsp & SEL_PLACE) {
69                         tmp |= SEL_PLACE;       /* Select sensor 1, core0 */
70                         pci_write_config_byte(pdev, REG_TEMP, tmp);
71                         pci_read_config_dword(pdev, REG_TEMP,
72                                               &data->temp[0][1]);
73                 }
74
75                 if (data->sensorsp & SEL_CORE) {
76                         tmp &= ~SEL_PLACE;      /* Select sensor 0, core1 */
77                         tmp |= SEL_CORE;
78                         pci_write_config_byte(pdev, REG_TEMP, tmp);
79                         pci_read_config_dword(pdev, REG_TEMP,
80                                               &data->temp[1][0]);
81
82                         if (data->sensorsp & SEL_PLACE) {
83                                 tmp |= SEL_PLACE;       /* Select sensor 1, core1 */
84                                 pci_write_config_byte(pdev, REG_TEMP, tmp);
85                                 pci_read_config_dword(pdev, REG_TEMP,
86                                                       &data->temp[1][1]);
87                         }
88                 }
89
90                 data->last_updated = jiffies;
91                 data->valid = 1;
92         }
93
94         mutex_unlock(&data->update_lock);
95         return data;
96 }
97
98 /*
99  * Sysfs stuff
100  */
101
102 static ssize_t show_name(struct device *dev, struct device_attribute
103                          *devattr, char *buf)
104 {
105         struct k8temp_data *data = dev_get_drvdata(dev);
106
107         return sprintf(buf, "%s\n", data->name);
108 }
109
110
111 static ssize_t show_temp(struct device *dev,
112                          struct device_attribute *devattr, char *buf)
113 {
114         struct sensor_device_attribute_2 *attr =
115             to_sensor_dev_attr_2(devattr);
116         int core = attr->nr;
117         int place = attr->index;
118         struct k8temp_data *data = k8temp_update_device(dev);
119
120         return sprintf(buf, "%d\n",
121                        TEMP_FROM_REG(data->temp[core][place]));
122 }
123
124 /* core, place */
125
126 static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
127 static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
128 static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0);
129 static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1);
130 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
131
132 static struct pci_device_id k8temp_ids[] = {
133         { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
134         { 0 },
135 };
136
137 MODULE_DEVICE_TABLE(pci, k8temp_ids);
138
139 static int __devinit k8temp_probe(struct pci_dev *pdev,
140                                   const struct pci_device_id *id)
141 {
142         int err;
143         u8 scfg;
144         u32 temp;
145         u8 model, stepping;
146         struct k8temp_data *data;
147
148         if (!(data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL))) {
149                 err = -ENOMEM;
150                 goto exit;
151         }
152
153         model = boot_cpu_data.x86_model;
154         stepping = boot_cpu_data.x86_mask;
155
156         switch (boot_cpu_data.x86) {
157         case 0xf:
158                 /* feature available since SH-C0, exclude older revisions */
159                 if (((model == 4) && (stepping == 0)) ||
160                     ((model == 5) && (stepping <= 1))) {
161                         err = -ENODEV;
162                         goto exit_free;
163                 }
164
165                 if (model >= 0x40) {
166                         dev_warn(&pdev->dev, "Temperature readouts might be "
167                                  "wrong - check erratum #141\n");
168                 }
169
170                 break;
171         }
172
173         pci_read_config_byte(pdev, REG_TEMP, &scfg);
174         scfg &= ~(SEL_PLACE | SEL_CORE);                /* Select sensor 0, core0 */
175         pci_write_config_byte(pdev, REG_TEMP, scfg);
176         pci_read_config_byte(pdev, REG_TEMP, &scfg);
177
178         if (scfg & (SEL_PLACE | SEL_CORE)) {
179                 dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
180                 err = -ENODEV;
181                 goto exit_free;
182         }
183
184         scfg |= (SEL_PLACE | SEL_CORE);
185         pci_write_config_byte(pdev, REG_TEMP, scfg);
186
187         /* now we know if we can change core and/or sensor */
188         pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
189
190         if (data->sensorsp & SEL_PLACE) {
191                 scfg &= ~SEL_CORE;      /* Select sensor 1, core0 */
192                 pci_write_config_byte(pdev, REG_TEMP, scfg);
193                 pci_read_config_dword(pdev, REG_TEMP, &temp);
194                 scfg |= SEL_CORE;       /* prepare for next selection */
195                 if (!((temp >> 16) & 0xff))     /* if temp is 0 -49C is not likely */
196                         data->sensorsp &= ~SEL_PLACE;
197         }
198
199         if (data->sensorsp & SEL_CORE) {
200                 scfg &= ~SEL_PLACE;     /* Select sensor 0, core1 */
201                 pci_write_config_byte(pdev, REG_TEMP, scfg);
202                 pci_read_config_dword(pdev, REG_TEMP, &temp);
203                 if (!((temp >> 16) & 0xff))     /* if temp is 0 -49C is not likely */
204                         data->sensorsp &= ~SEL_CORE;
205         }
206
207         data->name = "k8temp";
208         mutex_init(&data->update_lock);
209         dev_set_drvdata(&pdev->dev, data);
210
211         /* Register sysfs hooks */
212         err = device_create_file(&pdev->dev,
213                            &sensor_dev_attr_temp1_input.dev_attr);
214         if (err)
215                 goto exit_remove;
216
217         /* sensor can be changed and reports something */
218         if (data->sensorsp & SEL_PLACE) {
219                 err = device_create_file(&pdev->dev,
220                                    &sensor_dev_attr_temp2_input.dev_attr);
221                 if (err)
222                         goto exit_remove;
223         }
224
225         /* core can be changed and reports something */
226         if (data->sensorsp & SEL_CORE) {
227                 err = device_create_file(&pdev->dev,
228                                    &sensor_dev_attr_temp3_input.dev_attr);
229                 if (err)
230                         goto exit_remove;
231                 if (data->sensorsp & SEL_PLACE)
232                         err = device_create_file(&pdev->dev,
233                                            &sensor_dev_attr_temp4_input.
234                                            dev_attr);
235                         if (err)
236                                 goto exit_remove;
237         }
238
239         err = device_create_file(&pdev->dev, &dev_attr_name);
240         if (err)
241                 goto exit_remove;
242
243         data->hwmon_dev = hwmon_device_register(&pdev->dev);
244
245         if (IS_ERR(data->hwmon_dev)) {
246                 err = PTR_ERR(data->hwmon_dev);
247                 goto exit_remove;
248         }
249
250         return 0;
251
252 exit_remove:
253         device_remove_file(&pdev->dev,
254                            &sensor_dev_attr_temp1_input.dev_attr);
255         device_remove_file(&pdev->dev,
256                            &sensor_dev_attr_temp2_input.dev_attr);
257         device_remove_file(&pdev->dev,
258                            &sensor_dev_attr_temp3_input.dev_attr);
259         device_remove_file(&pdev->dev,
260                            &sensor_dev_attr_temp4_input.dev_attr);
261         device_remove_file(&pdev->dev, &dev_attr_name);
262 exit_free:
263         dev_set_drvdata(&pdev->dev, NULL);
264         kfree(data);
265 exit:
266         return err;
267 }
268
269 static void __devexit k8temp_remove(struct pci_dev *pdev)
270 {
271         struct k8temp_data *data = dev_get_drvdata(&pdev->dev);
272
273         hwmon_device_unregister(data->hwmon_dev);
274         device_remove_file(&pdev->dev,
275                            &sensor_dev_attr_temp1_input.dev_attr);
276         device_remove_file(&pdev->dev,
277                            &sensor_dev_attr_temp2_input.dev_attr);
278         device_remove_file(&pdev->dev,
279                            &sensor_dev_attr_temp3_input.dev_attr);
280         device_remove_file(&pdev->dev,
281                            &sensor_dev_attr_temp4_input.dev_attr);
282         device_remove_file(&pdev->dev, &dev_attr_name);
283         dev_set_drvdata(&pdev->dev, NULL);
284         kfree(data);
285 }
286
287 static struct pci_driver k8temp_driver = {
288         .name = "k8temp",
289         .id_table = k8temp_ids,
290         .probe = k8temp_probe,
291         .remove = __devexit_p(k8temp_remove),
292 };
293
294 static int __init k8temp_init(void)
295 {
296         return pci_register_driver(&k8temp_driver);
297 }
298
299 static void __exit k8temp_exit(void)
300 {
301         pci_unregister_driver(&k8temp_driver);
302 }
303
304 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
305 MODULE_DESCRIPTION("AMD K8 core temperature monitor");
306 MODULE_LICENSE("GPL");
307
308 module_init(k8temp_init)
309 module_exit(k8temp_exit)