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>
33 #include <linux/slab.h>
35 #define DRVNAME "i5k_amb"
37 #define I5K_REG_AMB_BASE_ADDR 0x48
38 #define I5K_REG_AMB_LEN_ADDR 0x50
39 #define I5K_REG_CHAN0_PRESENCE_ADDR 0x64
40 #define I5K_REG_CHAN1_PRESENCE_ADDR 0x66
42 #define AMB_REG_TEMP_MIN_ADDR 0x80
43 #define AMB_REG_TEMP_MID_ADDR 0x81
44 #define AMB_REG_TEMP_MAX_ADDR 0x82
45 #define AMB_REG_TEMP_STATUS_ADDR 0x84
46 #define AMB_REG_TEMP_ADDR 0x85
48 #define AMB_CONFIG_SIZE 2048
49 #define AMB_FUNC_3_OFFSET 768
51 static unsigned long amb_reg_temp_status(unsigned int amb)
53 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR +
54 AMB_CONFIG_SIZE * amb;
57 static unsigned long amb_reg_temp_min(unsigned int amb)
59 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR +
60 AMB_CONFIG_SIZE * amb;
63 static unsigned long amb_reg_temp_mid(unsigned int amb)
65 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR +
66 AMB_CONFIG_SIZE * amb;
69 static unsigned long amb_reg_temp_max(unsigned int amb)
71 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR +
72 AMB_CONFIG_SIZE * amb;
75 static unsigned long amb_reg_temp(unsigned int amb)
77 return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR +
78 AMB_CONFIG_SIZE * amb;
81 #define MAX_MEM_CHANNELS 4
82 #define MAX_AMBS_PER_CHANNEL 16
83 #define MAX_AMBS (MAX_MEM_CHANNELS * \
85 #define CHANNEL_SHIFT 4
88 * Ugly hack: For some reason the highest bit is set if there
89 * are _any_ DIMMs in the channel. Attempting to read from
90 * this "high-order" AMB results in a memory bus error, so
91 * for now we'll just ignore that top bit, even though that
92 * might prevent us from seeing the 16th DIMM in the channel.
94 #define REAL_MAX_AMBS_PER_CHANNEL 15
95 #define KNOBS_PER_AMB 6
97 static unsigned long amb_num_from_reg(unsigned int byte_num, unsigned int bit)
99 return byte_num * MAX_AMBS_PER_CHANNEL + bit;
102 #define AMB_SYSFS_NAME_LEN 16
103 struct i5k_device_attribute {
104 struct sensor_device_attribute s_attr;
105 char name[AMB_SYSFS_NAME_LEN];
108 struct i5k_amb_data {
109 struct device *hwmon_dev;
111 unsigned long amb_base;
112 unsigned long amb_len;
113 u16 amb_present[MAX_MEM_CHANNELS];
114 void __iomem *amb_mmio;
115 struct i5k_device_attribute *attrs;
116 unsigned int num_attrs;
119 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
122 return sprintf(buf, "%s\n", DRVNAME);
126 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
128 static struct platform_device *amb_pdev;
130 static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
132 return ioread8(data->amb_mmio + offset);
135 static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
138 iowrite8(val, data->amb_mmio + offset);
141 static ssize_t show_amb_alarm(struct device *dev,
142 struct device_attribute *devattr,
145 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
146 struct i5k_amb_data *data = dev_get_drvdata(dev);
148 if (!(amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x20) &&
149 (amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x8))
150 return sprintf(buf, "1\n");
152 return sprintf(buf, "0\n");
155 static ssize_t store_amb_min(struct device *dev,
156 struct device_attribute *devattr,
160 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
161 struct i5k_amb_data *data = dev_get_drvdata(dev);
163 int ret = kstrtoul(buf, 10, &temp);
171 amb_write_byte(data, amb_reg_temp_min(attr->index), temp);
175 static ssize_t store_amb_mid(struct device *dev,
176 struct device_attribute *devattr,
180 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
181 struct i5k_amb_data *data = dev_get_drvdata(dev);
183 int ret = kstrtoul(buf, 10, &temp);
191 amb_write_byte(data, amb_reg_temp_mid(attr->index), temp);
195 static ssize_t store_amb_max(struct device *dev,
196 struct device_attribute *devattr,
200 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
201 struct i5k_amb_data *data = dev_get_drvdata(dev);
203 int ret = kstrtoul(buf, 10, &temp);
211 amb_write_byte(data, amb_reg_temp_max(attr->index), temp);
215 static ssize_t show_amb_min(struct device *dev,
216 struct device_attribute *devattr,
219 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
220 struct i5k_amb_data *data = dev_get_drvdata(dev);
221 return sprintf(buf, "%d\n",
222 500 * amb_read_byte(data, amb_reg_temp_min(attr->index)));
225 static ssize_t show_amb_mid(struct device *dev,
226 struct device_attribute *devattr,
229 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
230 struct i5k_amb_data *data = dev_get_drvdata(dev);
231 return sprintf(buf, "%d\n",
232 500 * amb_read_byte(data, amb_reg_temp_mid(attr->index)));
235 static ssize_t show_amb_max(struct device *dev,
236 struct device_attribute *devattr,
239 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
240 struct i5k_amb_data *data = dev_get_drvdata(dev);
241 return sprintf(buf, "%d\n",
242 500 * amb_read_byte(data, amb_reg_temp_max(attr->index)));
245 static ssize_t show_amb_temp(struct device *dev,
246 struct device_attribute *devattr,
249 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
250 struct i5k_amb_data *data = dev_get_drvdata(dev);
251 return sprintf(buf, "%d\n",
252 500 * amb_read_byte(data, amb_reg_temp(attr->index)));
255 static ssize_t show_label(struct device *dev,
256 struct device_attribute *devattr,
259 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
261 return sprintf(buf, "Ch. %d DIMM %d\n", attr->index >> CHANNEL_SHIFT,
262 attr->index & DIMM_MASK);
265 static int __devinit i5k_amb_hwmon_init(struct platform_device *pdev)
271 struct i5k_amb_data *data = platform_get_drvdata(pdev);
273 /* Count the number of AMBs found */
274 /* ignore the high-order bit, see "Ugly hack" comment above */
275 for (i = 0; i < MAX_MEM_CHANNELS; i++)
276 num_ambs += hweight16(data->amb_present[i] & 0x7fff);
278 /* Set up sysfs stuff */
279 data->attrs = kzalloc(sizeof(*data->attrs) * num_ambs * KNOBS_PER_AMB,
285 for (i = 0; i < MAX_MEM_CHANNELS; i++) {
286 c = data->amb_present[i];
287 for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
288 struct i5k_device_attribute *iattr;
290 k = amb_num_from_reg(i, j);
296 iattr = data->attrs + data->num_attrs;
297 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
299 iattr->s_attr.dev_attr.attr.name = iattr->name;
300 iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
301 iattr->s_attr.dev_attr.show = show_label;
302 iattr->s_attr.index = k;
303 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
304 res = device_create_file(&pdev->dev,
305 &iattr->s_attr.dev_attr);
310 /* Temperature sysfs knob */
311 iattr = data->attrs + data->num_attrs;
312 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
314 iattr->s_attr.dev_attr.attr.name = iattr->name;
315 iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
316 iattr->s_attr.dev_attr.show = show_amb_temp;
317 iattr->s_attr.index = k;
318 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
319 res = device_create_file(&pdev->dev,
320 &iattr->s_attr.dev_attr);
325 /* Temperature min sysfs knob */
326 iattr = data->attrs + data->num_attrs;
327 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
329 iattr->s_attr.dev_attr.attr.name = iattr->name;
330 iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
331 iattr->s_attr.dev_attr.show = show_amb_min;
332 iattr->s_attr.dev_attr.store = store_amb_min;
333 iattr->s_attr.index = k;
334 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
335 res = device_create_file(&pdev->dev,
336 &iattr->s_attr.dev_attr);
341 /* Temperature mid sysfs knob */
342 iattr = data->attrs + data->num_attrs;
343 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
345 iattr->s_attr.dev_attr.attr.name = iattr->name;
346 iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
347 iattr->s_attr.dev_attr.show = show_amb_mid;
348 iattr->s_attr.dev_attr.store = store_amb_mid;
349 iattr->s_attr.index = k;
350 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
351 res = device_create_file(&pdev->dev,
352 &iattr->s_attr.dev_attr);
357 /* Temperature max sysfs knob */
358 iattr = data->attrs + data->num_attrs;
359 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
361 iattr->s_attr.dev_attr.attr.name = iattr->name;
362 iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
363 iattr->s_attr.dev_attr.show = show_amb_max;
364 iattr->s_attr.dev_attr.store = store_amb_max;
365 iattr->s_attr.index = k;
366 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
367 res = device_create_file(&pdev->dev,
368 &iattr->s_attr.dev_attr);
373 /* Temperature alarm sysfs knob */
374 iattr = data->attrs + data->num_attrs;
375 snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
377 iattr->s_attr.dev_attr.attr.name = iattr->name;
378 iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
379 iattr->s_attr.dev_attr.show = show_amb_alarm;
380 iattr->s_attr.index = k;
381 sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
382 res = device_create_file(&pdev->dev,
383 &iattr->s_attr.dev_attr);
390 res = device_create_file(&pdev->dev, &dev_attr_name);
394 data->hwmon_dev = hwmon_device_register(&pdev->dev);
395 if (IS_ERR(data->hwmon_dev)) {
396 res = PTR_ERR(data->hwmon_dev);
403 device_remove_file(&pdev->dev, &dev_attr_name);
404 for (i = 0; i < data->num_attrs; i++)
405 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
411 static int __devinit i5k_amb_add(void)
415 /* only ever going to be one of these */
416 amb_pdev = platform_device_alloc(DRVNAME, 0);
420 res = platform_device_add(amb_pdev);
426 platform_device_put(amb_pdev);
430 static int __devinit i5k_find_amb_registers(struct i5k_amb_data *data,
433 struct pci_dev *pcidev;
437 /* Find AMB register memory space */
438 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
444 if (pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32))
446 data->amb_base = val32;
448 if (pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32))
450 data->amb_len = val32;
452 /* Is it big enough? */
453 if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
454 dev_err(&pcidev->dev, "AMB region too small!\n");
464 static int __devinit i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
466 struct pci_dev *pcidev;
470 /* Copy the DIMM presence map for these two channels */
471 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
475 if (pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16))
477 amb_present[0] = val16;
479 if (pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16))
481 amb_present[1] = val16;
493 } chipset_ids[] __devinitdata = {
494 { PCI_DEVICE_ID_INTEL_5000_ERR, PCI_DEVICE_ID_INTEL_5000_FBD0 },
495 { PCI_DEVICE_ID_INTEL_5400_ERR, PCI_DEVICE_ID_INTEL_5400_FBD0 },
500 static struct pci_device_id i5k_amb_ids[] __devinitdata = {
501 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5000_ERR) },
502 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_5400_ERR) },
505 MODULE_DEVICE_TABLE(pci, i5k_amb_ids);
508 static int __devinit i5k_amb_probe(struct platform_device *pdev)
510 struct i5k_amb_data *data;
511 struct resource *reso;
514 data = kzalloc(sizeof(*data), GFP_KERNEL);
518 /* Figure out where the AMB registers live */
521 res = i5k_find_amb_registers(data, chipset_ids[i].err);
525 } while (chipset_ids[i].err);
530 /* Copy the DIMM presence map for the first two channels */
531 res = i5k_channel_probe(&data->amb_present[0], chipset_ids[i].fbd0);
535 /* Copy the DIMM presence map for the optional second two channels */
536 i5k_channel_probe(&data->amb_present[2], chipset_ids[i].fbd0 + 1);
538 /* Set up resource regions */
539 reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
545 data->amb_mmio = ioremap_nocache(data->amb_base, data->amb_len);
546 if (!data->amb_mmio) {
551 platform_set_drvdata(pdev, data);
553 res = i5k_amb_hwmon_init(pdev);
555 goto err_init_failed;
560 iounmap(data->amb_mmio);
561 platform_set_drvdata(pdev, NULL);
563 release_mem_region(data->amb_base, data->amb_len);
569 static int __devexit i5k_amb_remove(struct platform_device *pdev)
572 struct i5k_amb_data *data = platform_get_drvdata(pdev);
574 hwmon_device_unregister(data->hwmon_dev);
575 device_remove_file(&pdev->dev, &dev_attr_name);
576 for (i = 0; i < data->num_attrs; i++)
577 device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
579 iounmap(data->amb_mmio);
580 release_mem_region(data->amb_base, data->amb_len);
581 platform_set_drvdata(pdev, NULL);
586 static struct platform_driver i5k_amb_driver = {
588 .owner = THIS_MODULE,
591 .probe = i5k_amb_probe,
592 .remove = __devexit_p(i5k_amb_remove),
595 static int __init i5k_amb_init(void)
599 res = platform_driver_register(&i5k_amb_driver);
605 platform_driver_unregister(&i5k_amb_driver);
610 static void __exit i5k_amb_exit(void)
612 platform_device_unregister(amb_pdev);
613 platform_driver_unregister(&i5k_amb_driver);
616 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
617 MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
618 MODULE_LICENSE("GPL");
620 module_init(i5k_amb_init);
621 module_exit(i5k_amb_exit);