2 * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB
4 * Copyright (C) 2007 IBM
6 * Author: Darrick J. Wong <djwong@us.ibm.com>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/jiffies.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
30 #include <linux/log2.h>
31 #include <linux/pci.h>
32 #include <linux/platform_device.h>
34 #define DRVNAME "i5k_amb"
36 #define I5K_REG_AMB_BASE_ADDR 0x48
37 #define I5K_REG_AMB_LEN_ADDR 0x50
38 #define I5K_REG_CHAN0_PRESENCE_ADDR 0x64
39 #define I5K_REG_CHAN1_PRESENCE_ADDR 0x66
41 #define AMB_REG_TEMP_MIN_ADDR 0x80
42 #define AMB_REG_TEMP_MID_ADDR 0x81
43 #define AMB_REG_TEMP_MAX_ADDR 0x82
44 #define AMB_REG_TEMP_STATUS_ADDR 0x84
45 #define AMB_REG_TEMP_ADDR 0x85
47 #define AMB_CONFIG_SIZE 2048
48 #define AMB_FUNC_3_OFFSET 768
50 #define AMB_REG_TEMP_STATUS(amb) ((amb) * AMB_CONFIG_SIZE + \
51 AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR)
52 #define AMB_REG_TEMP_MIN(amb) ((amb) * AMB_CONFIG_SIZE + \
53 AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR)
54 #define AMB_REG_TEMP_MID(amb) ((amb) * AMB_CONFIG_SIZE + \
55 AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR)
56 #define AMB_REG_TEMP_MAX(amb) ((amb) * AMB_CONFIG_SIZE + \
57 AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR)
58 #define AMB_REG_TEMP(amb) ((amb) * AMB_CONFIG_SIZE + \
59 AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR)
61 #define MAX_MEM_CHANNELS 4
62 #define MAX_AMBS_PER_CHANNEL 16
63 #define MAX_AMBS (MAX_MEM_CHANNELS * \
66 * Ugly hack: For some reason the highest bit is set if there
67 * are _any_ DIMMs in the channel. Attempting to read from
68 * this "high-order" AMB results in a memory bus error, so
69 * for now we'll just ignore that top bit, even though that
70 * might prevent us from seeing the 16th DIMM in the channel.
72 #define REAL_MAX_AMBS_PER_CHANNEL 15
73 #define KNOBS_PER_AMB 5
75 #define AMB_NUM_FROM_REG(byte_num, bit_num) ((byte_num) * \
76 MAX_AMBS_PER_CHANNEL) + (bit_num)
78 #define AMB_SYSFS_NAME_LEN 16
79 struct i5k_device_attribute {
80 struct sensor_device_attribute s_attr;
81 char name[AMB_SYSFS_NAME_LEN];
85 struct device *hwmon_dev;
87 unsigned long amb_base;
88 unsigned long amb_len;
89 u16 amb_present[MAX_MEM_CHANNELS];
90 void __iomem *amb_mmio;
91 struct i5k_device_attribute *attrs;
92 unsigned int num_attrs;
95 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
98 return sprintf(buf, "%s\n", DRVNAME);
102 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
104 static struct platform_device *amb_pdev;
106 static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
108 return ioread8(data->amb_mmio + offset);
111 static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
114 iowrite8(val, data->amb_mmio + offset);
117 static ssize_t show_amb_alarm(struct device *dev,
118 struct device_attribute *devattr,
121 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
122 struct i5k_amb_data *data = dev_get_drvdata(dev);
124 if (!(amb_read_byte(data, AMB_REG_TEMP_STATUS(attr->index)) & 0x20) &&
125 (amb_read_byte(data, AMB_REG_TEMP_STATUS(attr->index)) & 0x8))
126 return sprintf(buf, "1\n");
128 return sprintf(buf, "0\n");
131 static ssize_t store_amb_min(struct device *dev,
132 struct device_attribute *devattr,
136 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
137 struct i5k_amb_data *data = dev_get_drvdata(dev);
138 unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
143 amb_write_byte(data, AMB_REG_TEMP_MIN(attr->index), temp);
147 static ssize_t store_amb_mid(struct device *dev,
148 struct device_attribute *devattr,
152 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
153 struct i5k_amb_data *data = dev_get_drvdata(dev);
154 unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
159 amb_write_byte(data, AMB_REG_TEMP_MID(attr->index), temp);
163 static ssize_t store_amb_max(struct device *dev,
164 struct device_attribute *devattr,
168 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
169 struct i5k_amb_data *data = dev_get_drvdata(dev);
170 unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
175 amb_write_byte(data, AMB_REG_TEMP_MAX(attr->index), temp);
179 static ssize_t show_amb_min(struct device *dev,
180 struct device_attribute *devattr,
183 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
184 struct i5k_amb_data *data = dev_get_drvdata(dev);
185 return sprintf(buf, "%d\n",
186 500 * amb_read_byte(data, AMB_REG_TEMP_MIN(attr->index)));
189 static ssize_t show_amb_mid(struct device *dev,
190 struct device_attribute *devattr,
193 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
194 struct i5k_amb_data *data = dev_get_drvdata(dev);
195 return sprintf(buf, "%d\n",
196 500 * amb_read_byte(data, AMB_REG_TEMP_MID(attr->index)));
199 static ssize_t show_amb_max(struct device *dev,
200 struct device_attribute *devattr,
203 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
204 struct i5k_amb_data *data = dev_get_drvdata(dev);
205 return sprintf(buf, "%d\n",
206 500 * amb_read_byte(data, AMB_REG_TEMP_MAX(attr->index)));
209 static ssize_t show_amb_temp(struct device *dev,
210 struct device_attribute *devattr,
213 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
214 struct i5k_amb_data *data = dev_get_drvdata(dev);
215 return sprintf(buf, "%d\n",
216 500 * amb_read_byte(data, AMB_REG_TEMP(attr->index)));
219 static int __devinit i5k_amb_hwmon_init(struct platform_device *pdev)
225 struct i5k_amb_data *data = platform_get_drvdata(pdev);
227 /* Count the number of AMBs found */
228 /* ignore the high-order bit, see "Ugly hack" comment above */
229 for (i = 0; i < MAX_MEM_CHANNELS; i++)
230 num_ambs += hweight16(data->amb_present[i] & 0x7fff);
232 /* Set up sysfs stuff */
233 data->attrs = kzalloc(sizeof(*data->attrs) * num_ambs * KNOBS_PER_AMB,
239 for (i = 0; i < MAX_MEM_CHANNELS; i++) {
240 c = data->amb_present[i];
241 for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
242 struct i5k_device_attribute *iattr;
244 k = AMB_NUM_FROM_REG(i, j);
249 /* Temperature sysfs knob */
250 iattr = data->attrs + data->num_attrs;
251 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
253 iattr->s_attr.dev_attr.attr.name = iattr->name;
254 iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
255 iattr->s_attr.dev_attr.show = show_amb_temp;
256 iattr->s_attr.index = k;
257 res = device_create_file(&pdev->dev,
258 &iattr->s_attr.dev_attr);
263 /* Temperature min sysfs knob */
264 iattr = data->attrs + data->num_attrs;
265 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
267 iattr->s_attr.dev_attr.attr.name = iattr->name;
268 iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
269 iattr->s_attr.dev_attr.show = show_amb_min;
270 iattr->s_attr.dev_attr.store = store_amb_min;
271 iattr->s_attr.index = k;
272 res = device_create_file(&pdev->dev,
273 &iattr->s_attr.dev_attr);
278 /* Temperature mid sysfs knob */
279 iattr = data->attrs + data->num_attrs;
280 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
282 iattr->s_attr.dev_attr.attr.name = iattr->name;
283 iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
284 iattr->s_attr.dev_attr.show = show_amb_mid;
285 iattr->s_attr.dev_attr.store = store_amb_mid;
286 iattr->s_attr.index = k;
287 res = device_create_file(&pdev->dev,
288 &iattr->s_attr.dev_attr);
293 /* Temperature max sysfs knob */
294 iattr = data->attrs + data->num_attrs;
295 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
297 iattr->s_attr.dev_attr.attr.name = iattr->name;
298 iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
299 iattr->s_attr.dev_attr.show = show_amb_max;
300 iattr->s_attr.dev_attr.store = store_amb_max;
301 iattr->s_attr.index = k;
302 res = device_create_file(&pdev->dev,
303 &iattr->s_attr.dev_attr);
308 /* Temperature alarm sysfs knob */
309 iattr = data->attrs + data->num_attrs;
310 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
312 iattr->s_attr.dev_attr.attr.name = iattr->name;
313 iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
314 iattr->s_attr.dev_attr.show = show_amb_alarm;
315 iattr->s_attr.index = k;
316 res = device_create_file(&pdev->dev,
317 &iattr->s_attr.dev_attr);
324 res = device_create_file(&pdev->dev, &dev_attr_name);
328 data->hwmon_dev = hwmon_device_register(&pdev->dev);
329 if (IS_ERR(data->hwmon_dev)) {
330 res = PTR_ERR(data->hwmon_dev);
337 device_remove_file(&pdev->dev, &dev_attr_name);
338 for (i = 0; i < data->num_attrs; i++)
339 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
345 static int __devinit i5k_amb_add(void)
349 /* only ever going to be one of these */
350 amb_pdev = platform_device_alloc(DRVNAME, 0);
354 res = platform_device_add(amb_pdev);
360 platform_device_put(amb_pdev);
364 static int __devinit i5k_find_amb_registers(struct i5k_amb_data *data)
366 struct pci_dev *pcidev;
370 /* Find AMB register memory space */
371 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
372 PCI_DEVICE_ID_INTEL_5000_ERR,
377 if (pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32))
379 data->amb_base = val32;
381 if (pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32))
383 data->amb_len = val32;
385 /* Is it big enough? */
386 if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
387 dev_err(&pcidev->dev, "AMB region too small!\n");
397 static int __devinit i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
399 struct pci_dev *pcidev;
403 /* Copy the DIMM presence map for these two channels */
404 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
408 if (pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16))
410 amb_present[0] = val16;
412 if (pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16))
414 amb_present[1] = val16;
423 static int __devinit i5k_amb_probe(struct platform_device *pdev)
425 struct i5k_amb_data *data;
426 struct resource *reso;
429 data = kzalloc(sizeof(*data), GFP_KERNEL);
433 /* Figure out where the AMB registers live */
434 res = i5k_find_amb_registers(data);
438 /* Copy the DIMM presence map for the first two channels */
439 res = i5k_channel_probe(&data->amb_present[0],
440 PCI_DEVICE_ID_INTEL_5000_FBD0);
444 /* Copy the DIMM presence map for the optional second two channels */
445 i5k_channel_probe(&data->amb_present[2],
446 PCI_DEVICE_ID_INTEL_5000_FBD1);
448 /* Set up resource regions */
449 reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
455 data->amb_mmio = ioremap_nocache(data->amb_base, data->amb_len);
456 if (!data->amb_mmio) {
461 platform_set_drvdata(pdev, data);
463 res = i5k_amb_hwmon_init(pdev);
465 goto err_init_failed;
470 iounmap(data->amb_mmio);
471 platform_set_drvdata(pdev, NULL);
473 release_mem_region(data->amb_base, data->amb_len);
479 static int __devexit i5k_amb_remove(struct platform_device *pdev)
482 struct i5k_amb_data *data = platform_get_drvdata(pdev);
484 hwmon_device_unregister(data->hwmon_dev);
485 device_remove_file(&pdev->dev, &dev_attr_name);
486 for (i = 0; i < data->num_attrs; i++)
487 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
489 iounmap(data->amb_mmio);
490 release_mem_region(data->amb_base, data->amb_len);
491 platform_set_drvdata(pdev, NULL);
496 static struct platform_driver i5k_amb_driver = {
498 .owner = THIS_MODULE,
501 .probe = i5k_amb_probe,
502 .remove = __devexit_p(i5k_amb_remove),
505 static int __init i5k_amb_init(void)
509 res = platform_driver_register(&i5k_amb_driver);
515 platform_driver_unregister(&i5k_amb_driver);
520 static void __exit i5k_amb_exit(void)
522 platform_device_unregister(amb_pdev);
523 platform_driver_unregister(&i5k_amb_driver);
526 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
527 MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
528 MODULE_LICENSE("GPL");
530 module_init(i5k_amb_init);
531 module_exit(i5k_amb_exit);