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
);
162 unsigned long temp
= simple_strtoul(buf
, NULL
, 10) / 500;
167 amb_write_byte(data
, amb_reg_temp_min(attr
->index
), temp
);
171 static ssize_t
store_amb_mid(struct device
*dev
,
172 struct device_attribute
*devattr
,
176 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
177 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
178 unsigned long temp
= simple_strtoul(buf
, NULL
, 10) / 500;
183 amb_write_byte(data
, amb_reg_temp_mid(attr
->index
), temp
);
187 static ssize_t
store_amb_max(struct device
*dev
,
188 struct device_attribute
*devattr
,
192 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
193 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
194 unsigned long temp
= simple_strtoul(buf
, NULL
, 10) / 500;
199 amb_write_byte(data
, amb_reg_temp_max(attr
->index
), temp
);
203 static ssize_t
show_amb_min(struct device
*dev
,
204 struct device_attribute
*devattr
,
207 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
208 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
209 return sprintf(buf
, "%d\n",
210 500 * amb_read_byte(data
, amb_reg_temp_min(attr
->index
)));
213 static ssize_t
show_amb_mid(struct device
*dev
,
214 struct device_attribute
*devattr
,
217 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
218 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
219 return sprintf(buf
, "%d\n",
220 500 * amb_read_byte(data
, amb_reg_temp_mid(attr
->index
)));
223 static ssize_t
show_amb_max(struct device
*dev
,
224 struct device_attribute
*devattr
,
227 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
228 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
229 return sprintf(buf
, "%d\n",
230 500 * amb_read_byte(data
, amb_reg_temp_max(attr
->index
)));
233 static ssize_t
show_amb_temp(struct device
*dev
,
234 struct device_attribute
*devattr
,
237 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
238 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
239 return sprintf(buf
, "%d\n",
240 500 * amb_read_byte(data
, amb_reg_temp(attr
->index
)));
243 static ssize_t
show_label(struct device
*dev
,
244 struct device_attribute
*devattr
,
247 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
249 return sprintf(buf
, "Ch. %d DIMM %d\n", attr
->index
>> CHANNEL_SHIFT
,
250 attr
->index
& DIMM_MASK
);
253 static int __devinit
i5k_amb_hwmon_init(struct platform_device
*pdev
)
259 struct i5k_amb_data
*data
= platform_get_drvdata(pdev
);
261 /* Count the number of AMBs found */
262 /* ignore the high-order bit, see "Ugly hack" comment above */
263 for (i
= 0; i
< MAX_MEM_CHANNELS
; i
++)
264 num_ambs
+= hweight16(data
->amb_present
[i
] & 0x7fff);
266 /* Set up sysfs stuff */
267 data
->attrs
= kzalloc(sizeof(*data
->attrs
) * num_ambs
* KNOBS_PER_AMB
,
273 for (i
= 0; i
< MAX_MEM_CHANNELS
; i
++) {
274 c
= data
->amb_present
[i
];
275 for (j
= 0; j
< REAL_MAX_AMBS_PER_CHANNEL
; j
++, c
>>= 1) {
276 struct i5k_device_attribute
*iattr
;
278 k
= amb_num_from_reg(i
, j
);
284 iattr
= data
->attrs
+ data
->num_attrs
;
285 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
287 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
288 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IRUGO
;
289 iattr
->s_attr
.dev_attr
.show
= show_label
;
290 iattr
->s_attr
.index
= k
;
291 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
292 res
= device_create_file(&pdev
->dev
,
293 &iattr
->s_attr
.dev_attr
);
298 /* Temperature sysfs knob */
299 iattr
= data
->attrs
+ data
->num_attrs
;
300 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
302 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
303 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IRUGO
;
304 iattr
->s_attr
.dev_attr
.show
= show_amb_temp
;
305 iattr
->s_attr
.index
= k
;
306 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
307 res
= device_create_file(&pdev
->dev
,
308 &iattr
->s_attr
.dev_attr
);
313 /* Temperature min sysfs knob */
314 iattr
= data
->attrs
+ data
->num_attrs
;
315 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
317 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
318 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
319 iattr
->s_attr
.dev_attr
.show
= show_amb_min
;
320 iattr
->s_attr
.dev_attr
.store
= store_amb_min
;
321 iattr
->s_attr
.index
= k
;
322 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
323 res
= device_create_file(&pdev
->dev
,
324 &iattr
->s_attr
.dev_attr
);
329 /* Temperature mid sysfs knob */
330 iattr
= data
->attrs
+ data
->num_attrs
;
331 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
333 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
334 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
335 iattr
->s_attr
.dev_attr
.show
= show_amb_mid
;
336 iattr
->s_attr
.dev_attr
.store
= store_amb_mid
;
337 iattr
->s_attr
.index
= k
;
338 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
339 res
= device_create_file(&pdev
->dev
,
340 &iattr
->s_attr
.dev_attr
);
345 /* Temperature max sysfs knob */
346 iattr
= data
->attrs
+ data
->num_attrs
;
347 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
349 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
350 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
351 iattr
->s_attr
.dev_attr
.show
= show_amb_max
;
352 iattr
->s_attr
.dev_attr
.store
= store_amb_max
;
353 iattr
->s_attr
.index
= k
;
354 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
355 res
= device_create_file(&pdev
->dev
,
356 &iattr
->s_attr
.dev_attr
);
361 /* Temperature alarm sysfs knob */
362 iattr
= data
->attrs
+ data
->num_attrs
;
363 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
365 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
366 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IRUGO
;
367 iattr
->s_attr
.dev_attr
.show
= show_amb_alarm
;
368 iattr
->s_attr
.index
= k
;
369 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
370 res
= device_create_file(&pdev
->dev
,
371 &iattr
->s_attr
.dev_attr
);
378 res
= device_create_file(&pdev
->dev
, &dev_attr_name
);
382 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
383 if (IS_ERR(data
->hwmon_dev
)) {
384 res
= PTR_ERR(data
->hwmon_dev
);
391 device_remove_file(&pdev
->dev
, &dev_attr_name
);
392 for (i
= 0; i
< data
->num_attrs
; i
++)
393 device_remove_file(&pdev
->dev
, &data
->attrs
[i
].s_attr
.dev_attr
);
399 static int __devinit
i5k_amb_add(void)
403 /* only ever going to be one of these */
404 amb_pdev
= platform_device_alloc(DRVNAME
, 0);
408 res
= platform_device_add(amb_pdev
);
414 platform_device_put(amb_pdev
);
418 static int __devinit
i5k_find_amb_registers(struct i5k_amb_data
*data
,
421 struct pci_dev
*pcidev
;
425 /* Find AMB register memory space */
426 pcidev
= pci_get_device(PCI_VENDOR_ID_INTEL
,
432 if (pci_read_config_dword(pcidev
, I5K_REG_AMB_BASE_ADDR
, &val32
))
434 data
->amb_base
= val32
;
436 if (pci_read_config_dword(pcidev
, I5K_REG_AMB_LEN_ADDR
, &val32
))
438 data
->amb_len
= val32
;
440 /* Is it big enough? */
441 if (data
->amb_len
< AMB_CONFIG_SIZE
* MAX_AMBS
) {
442 dev_err(&pcidev
->dev
, "AMB region too small!\n");
452 static int __devinit
i5k_channel_probe(u16
*amb_present
, unsigned long dev_id
)
454 struct pci_dev
*pcidev
;
458 /* Copy the DIMM presence map for these two channels */
459 pcidev
= pci_get_device(PCI_VENDOR_ID_INTEL
, dev_id
, NULL
);
463 if (pci_read_config_word(pcidev
, I5K_REG_CHAN0_PRESENCE_ADDR
, &val16
))
465 amb_present
[0] = val16
;
467 if (pci_read_config_word(pcidev
, I5K_REG_CHAN1_PRESENCE_ADDR
, &val16
))
469 amb_present
[1] = val16
;
481 } chipset_ids
[] __devinitdata
= {
482 { PCI_DEVICE_ID_INTEL_5000_ERR
, PCI_DEVICE_ID_INTEL_5000_FBD0
},
483 { PCI_DEVICE_ID_INTEL_5400_ERR
, PCI_DEVICE_ID_INTEL_5400_FBD0
},
488 static struct pci_device_id i5k_amb_ids
[] __devinitdata
= {
489 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_5000_ERR
) },
490 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_5400_ERR
) },
493 MODULE_DEVICE_TABLE(pci
, i5k_amb_ids
);
496 static int __devinit
i5k_amb_probe(struct platform_device
*pdev
)
498 struct i5k_amb_data
*data
;
499 struct resource
*reso
;
502 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
506 /* Figure out where the AMB registers live */
509 res
= i5k_find_amb_registers(data
, chipset_ids
[i
].err
);
513 } while (chipset_ids
[i
].err
);
518 /* Copy the DIMM presence map for the first two channels */
519 res
= i5k_channel_probe(&data
->amb_present
[0], chipset_ids
[i
].fbd0
);
523 /* Copy the DIMM presence map for the optional second two channels */
524 i5k_channel_probe(&data
->amb_present
[2], chipset_ids
[i
].fbd0
+ 1);
526 /* Set up resource regions */
527 reso
= request_mem_region(data
->amb_base
, data
->amb_len
, DRVNAME
);
533 data
->amb_mmio
= ioremap_nocache(data
->amb_base
, data
->amb_len
);
534 if (!data
->amb_mmio
) {
539 platform_set_drvdata(pdev
, data
);
541 res
= i5k_amb_hwmon_init(pdev
);
543 goto err_init_failed
;
548 iounmap(data
->amb_mmio
);
549 platform_set_drvdata(pdev
, NULL
);
551 release_mem_region(data
->amb_base
, data
->amb_len
);
557 static int __devexit
i5k_amb_remove(struct platform_device
*pdev
)
560 struct i5k_amb_data
*data
= platform_get_drvdata(pdev
);
562 hwmon_device_unregister(data
->hwmon_dev
);
563 device_remove_file(&pdev
->dev
, &dev_attr_name
);
564 for (i
= 0; i
< data
->num_attrs
; i
++)
565 device_remove_file(&pdev
->dev
, &data
->attrs
[i
].s_attr
.dev_attr
);
567 iounmap(data
->amb_mmio
);
568 release_mem_region(data
->amb_base
, data
->amb_len
);
569 platform_set_drvdata(pdev
, NULL
);
574 static struct platform_driver i5k_amb_driver
= {
576 .owner
= THIS_MODULE
,
579 .probe
= i5k_amb_probe
,
580 .remove
= __devexit_p(i5k_amb_remove
),
583 static int __init
i5k_amb_init(void)
587 res
= platform_driver_register(&i5k_amb_driver
);
593 platform_driver_unregister(&i5k_amb_driver
);
598 static void __exit
i5k_amb_exit(void)
600 platform_device_unregister(amb_pdev
);
601 platform_driver_unregister(&i5k_amb_driver
);
604 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
605 MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
606 MODULE_LICENSE("GPL");
608 module_init(i5k_amb_init
);
609 module_exit(i5k_amb_exit
);