2 * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB
4 * Copyright (C) 2007 IBM
6 * Author: Darrick J. Wong <darrick.wong@oracle.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/hwmon.h>
25 #include <linux/hwmon-sysfs.h>
26 #include <linux/err.h>
27 #include <linux/mutex.h>
28 #include <linux/log2.h>
29 #include <linux/pci.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
33 #define DRVNAME "i5k_amb"
35 #define I5K_REG_AMB_BASE_ADDR 0x48
36 #define I5K_REG_AMB_LEN_ADDR 0x50
37 #define I5K_REG_CHAN0_PRESENCE_ADDR 0x64
38 #define I5K_REG_CHAN1_PRESENCE_ADDR 0x66
40 #define AMB_REG_TEMP_MIN_ADDR 0x80
41 #define AMB_REG_TEMP_MID_ADDR 0x81
42 #define AMB_REG_TEMP_MAX_ADDR 0x82
43 #define AMB_REG_TEMP_STATUS_ADDR 0x84
44 #define AMB_REG_TEMP_ADDR 0x85
46 #define AMB_CONFIG_SIZE 2048
47 #define AMB_FUNC_3_OFFSET 768
49 static unsigned long amb_reg_temp_status(unsigned int amb
)
51 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_STATUS_ADDR
+
52 AMB_CONFIG_SIZE
* amb
;
55 static unsigned long amb_reg_temp_min(unsigned int amb
)
57 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_MIN_ADDR
+
58 AMB_CONFIG_SIZE
* amb
;
61 static unsigned long amb_reg_temp_mid(unsigned int amb
)
63 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_MID_ADDR
+
64 AMB_CONFIG_SIZE
* amb
;
67 static unsigned long amb_reg_temp_max(unsigned int amb
)
69 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_MAX_ADDR
+
70 AMB_CONFIG_SIZE
* amb
;
73 static unsigned long amb_reg_temp(unsigned int amb
)
75 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_ADDR
+
76 AMB_CONFIG_SIZE
* amb
;
79 #define MAX_MEM_CHANNELS 4
80 #define MAX_AMBS_PER_CHANNEL 16
81 #define MAX_AMBS (MAX_MEM_CHANNELS * \
83 #define CHANNEL_SHIFT 4
86 * Ugly hack: For some reason the highest bit is set if there
87 * are _any_ DIMMs in the channel. Attempting to read from
88 * this "high-order" AMB results in a memory bus error, so
89 * for now we'll just ignore that top bit, even though that
90 * might prevent us from seeing the 16th DIMM in the channel.
92 #define REAL_MAX_AMBS_PER_CHANNEL 15
93 #define KNOBS_PER_AMB 6
95 static unsigned long amb_num_from_reg(unsigned int byte_num
, unsigned int bit
)
97 return byte_num
* MAX_AMBS_PER_CHANNEL
+ bit
;
100 #define AMB_SYSFS_NAME_LEN 16
101 struct i5k_device_attribute
{
102 struct sensor_device_attribute s_attr
;
103 char name
[AMB_SYSFS_NAME_LEN
];
106 struct i5k_amb_data
{
107 struct device
*hwmon_dev
;
109 unsigned long amb_base
;
110 unsigned long amb_len
;
111 u16 amb_present
[MAX_MEM_CHANNELS
];
112 void __iomem
*amb_mmio
;
113 struct i5k_device_attribute
*attrs
;
114 unsigned int num_attrs
;
117 static ssize_t
show_name(struct device
*dev
, struct device_attribute
*devattr
,
120 return sprintf(buf
, "%s\n", DRVNAME
);
124 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
126 static struct platform_device
*amb_pdev
;
128 static u8
amb_read_byte(struct i5k_amb_data
*data
, unsigned long offset
)
130 return ioread8(data
->amb_mmio
+ offset
);
133 static void amb_write_byte(struct i5k_amb_data
*data
, unsigned long offset
,
136 iowrite8(val
, data
->amb_mmio
+ offset
);
139 static ssize_t
show_amb_alarm(struct device
*dev
,
140 struct device_attribute
*devattr
,
143 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
144 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
146 if (!(amb_read_byte(data
, amb_reg_temp_status(attr
->index
)) & 0x20) &&
147 (amb_read_byte(data
, amb_reg_temp_status(attr
->index
)) & 0x8))
148 return sprintf(buf
, "1\n");
150 return sprintf(buf
, "0\n");
153 static ssize_t
store_amb_min(struct device
*dev
,
154 struct device_attribute
*devattr
,
158 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
159 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
161 int ret
= kstrtoul(buf
, 10, &temp
);
169 amb_write_byte(data
, amb_reg_temp_min(attr
->index
), temp
);
173 static ssize_t
store_amb_mid(struct device
*dev
,
174 struct device_attribute
*devattr
,
178 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
179 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
181 int ret
= kstrtoul(buf
, 10, &temp
);
189 amb_write_byte(data
, amb_reg_temp_mid(attr
->index
), temp
);
193 static ssize_t
store_amb_max(struct device
*dev
,
194 struct device_attribute
*devattr
,
198 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
199 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
201 int ret
= kstrtoul(buf
, 10, &temp
);
209 amb_write_byte(data
, amb_reg_temp_max(attr
->index
), temp
);
213 static ssize_t
show_amb_min(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_min(attr
->index
)));
223 static ssize_t
show_amb_mid(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_mid(attr
->index
)));
233 static ssize_t
show_amb_max(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_max(attr
->index
)));
243 static ssize_t
show_amb_temp(struct device
*dev
,
244 struct device_attribute
*devattr
,
247 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
248 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
249 return sprintf(buf
, "%d\n",
250 500 * amb_read_byte(data
, amb_reg_temp(attr
->index
)));
253 static ssize_t
show_label(struct device
*dev
,
254 struct device_attribute
*devattr
,
257 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
259 return sprintf(buf
, "Ch. %d DIMM %d\n", attr
->index
>> CHANNEL_SHIFT
,
260 attr
->index
& DIMM_MASK
);
263 static int i5k_amb_hwmon_init(struct platform_device
*pdev
)
269 struct i5k_amb_data
*data
= platform_get_drvdata(pdev
);
271 /* Count the number of AMBs found */
272 /* ignore the high-order bit, see "Ugly hack" comment above */
273 for (i
= 0; i
< MAX_MEM_CHANNELS
; i
++)
274 num_ambs
+= hweight16(data
->amb_present
[i
] & 0x7fff);
276 /* Set up sysfs stuff */
277 data
->attrs
= kzalloc(sizeof(*data
->attrs
) * num_ambs
* KNOBS_PER_AMB
,
283 for (i
= 0; i
< MAX_MEM_CHANNELS
; i
++) {
284 c
= data
->amb_present
[i
];
285 for (j
= 0; j
< REAL_MAX_AMBS_PER_CHANNEL
; j
++, c
>>= 1) {
286 struct i5k_device_attribute
*iattr
;
288 k
= amb_num_from_reg(i
, j
);
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_IRUGO
;
299 iattr
->s_attr
.dev_attr
.show
= show_label
;
300 iattr
->s_attr
.index
= k
;
301 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
302 res
= device_create_file(&pdev
->dev
,
303 &iattr
->s_attr
.dev_attr
);
308 /* Temperature 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_temp
;
315 iattr
->s_attr
.index
= k
;
316 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
317 res
= device_create_file(&pdev
->dev
,
318 &iattr
->s_attr
.dev_attr
);
323 /* Temperature min sysfs knob */
324 iattr
= data
->attrs
+ data
->num_attrs
;
325 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
327 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
328 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
329 iattr
->s_attr
.dev_attr
.show
= show_amb_min
;
330 iattr
->s_attr
.dev_attr
.store
= store_amb_min
;
331 iattr
->s_attr
.index
= k
;
332 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
333 res
= device_create_file(&pdev
->dev
,
334 &iattr
->s_attr
.dev_attr
);
339 /* Temperature mid sysfs knob */
340 iattr
= data
->attrs
+ data
->num_attrs
;
341 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
343 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
344 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
345 iattr
->s_attr
.dev_attr
.show
= show_amb_mid
;
346 iattr
->s_attr
.dev_attr
.store
= store_amb_mid
;
347 iattr
->s_attr
.index
= k
;
348 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
349 res
= device_create_file(&pdev
->dev
,
350 &iattr
->s_attr
.dev_attr
);
355 /* Temperature max sysfs knob */
356 iattr
= data
->attrs
+ data
->num_attrs
;
357 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
359 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
360 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
361 iattr
->s_attr
.dev_attr
.show
= show_amb_max
;
362 iattr
->s_attr
.dev_attr
.store
= store_amb_max
;
363 iattr
->s_attr
.index
= k
;
364 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
365 res
= device_create_file(&pdev
->dev
,
366 &iattr
->s_attr
.dev_attr
);
371 /* Temperature alarm sysfs knob */
372 iattr
= data
->attrs
+ data
->num_attrs
;
373 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
375 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
376 iattr
->s_attr
.dev_attr
.attr
.mode
= S_IRUGO
;
377 iattr
->s_attr
.dev_attr
.show
= show_amb_alarm
;
378 iattr
->s_attr
.index
= k
;
379 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
380 res
= device_create_file(&pdev
->dev
,
381 &iattr
->s_attr
.dev_attr
);
388 res
= device_create_file(&pdev
->dev
, &dev_attr_name
);
392 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
393 if (IS_ERR(data
->hwmon_dev
)) {
394 res
= PTR_ERR(data
->hwmon_dev
);
401 device_remove_file(&pdev
->dev
, &dev_attr_name
);
402 for (i
= 0; i
< data
->num_attrs
; i
++)
403 device_remove_file(&pdev
->dev
, &data
->attrs
[i
].s_attr
.dev_attr
);
409 static int i5k_amb_add(void)
413 /* only ever going to be one of these */
414 amb_pdev
= platform_device_alloc(DRVNAME
, 0);
418 res
= platform_device_add(amb_pdev
);
424 platform_device_put(amb_pdev
);
428 static int i5k_find_amb_registers(struct i5k_amb_data
*data
,
431 struct pci_dev
*pcidev
;
435 /* Find AMB register memory space */
436 pcidev
= pci_get_device(PCI_VENDOR_ID_INTEL
,
442 if (pci_read_config_dword(pcidev
, I5K_REG_AMB_BASE_ADDR
, &val32
))
444 data
->amb_base
= val32
;
446 if (pci_read_config_dword(pcidev
, I5K_REG_AMB_LEN_ADDR
, &val32
))
448 data
->amb_len
= val32
;
450 /* Is it big enough? */
451 if (data
->amb_len
< AMB_CONFIG_SIZE
* MAX_AMBS
) {
452 dev_err(&pcidev
->dev
, "AMB region too small!\n");
462 static int i5k_channel_probe(u16
*amb_present
, unsigned long dev_id
)
464 struct pci_dev
*pcidev
;
468 /* Copy the DIMM presence map for these two channels */
469 pcidev
= pci_get_device(PCI_VENDOR_ID_INTEL
, dev_id
, NULL
);
473 if (pci_read_config_word(pcidev
, I5K_REG_CHAN0_PRESENCE_ADDR
, &val16
))
475 amb_present
[0] = val16
;
477 if (pci_read_config_word(pcidev
, I5K_REG_CHAN1_PRESENCE_ADDR
, &val16
))
479 amb_present
[1] = val16
;
492 { PCI_DEVICE_ID_INTEL_5000_ERR
, PCI_DEVICE_ID_INTEL_5000_FBD0
},
493 { PCI_DEVICE_ID_INTEL_5400_ERR
, PCI_DEVICE_ID_INTEL_5400_FBD0
},
498 static struct pci_device_id i5k_amb_ids
[] = {
499 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_5000_ERR
) },
500 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_5400_ERR
) },
503 MODULE_DEVICE_TABLE(pci
, i5k_amb_ids
);
506 static int i5k_amb_probe(struct platform_device
*pdev
)
508 struct i5k_amb_data
*data
;
509 struct resource
*reso
;
512 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
516 /* Figure out where the AMB registers live */
519 res
= i5k_find_amb_registers(data
, chipset_ids
[i
].err
);
523 } while (chipset_ids
[i
].err
);
528 /* Copy the DIMM presence map for the first two channels */
529 res
= i5k_channel_probe(&data
->amb_present
[0], chipset_ids
[i
].fbd0
);
533 /* Copy the DIMM presence map for the optional second two channels */
534 i5k_channel_probe(&data
->amb_present
[2], chipset_ids
[i
].fbd0
+ 1);
536 /* Set up resource regions */
537 reso
= request_mem_region(data
->amb_base
, data
->amb_len
, DRVNAME
);
543 data
->amb_mmio
= ioremap_nocache(data
->amb_base
, data
->amb_len
);
544 if (!data
->amb_mmio
) {
549 platform_set_drvdata(pdev
, data
);
551 res
= i5k_amb_hwmon_init(pdev
);
553 goto err_init_failed
;
558 iounmap(data
->amb_mmio
);
560 release_mem_region(data
->amb_base
, data
->amb_len
);
566 static int i5k_amb_remove(struct platform_device
*pdev
)
569 struct i5k_amb_data
*data
= platform_get_drvdata(pdev
);
571 hwmon_device_unregister(data
->hwmon_dev
);
572 device_remove_file(&pdev
->dev
, &dev_attr_name
);
573 for (i
= 0; i
< data
->num_attrs
; i
++)
574 device_remove_file(&pdev
->dev
, &data
->attrs
[i
].s_attr
.dev_attr
);
576 iounmap(data
->amb_mmio
);
577 release_mem_region(data
->amb_base
, data
->amb_len
);
582 static struct platform_driver i5k_amb_driver
= {
586 .probe
= i5k_amb_probe
,
587 .remove
= i5k_amb_remove
,
590 static int __init
i5k_amb_init(void)
594 res
= platform_driver_register(&i5k_amb_driver
);
600 platform_driver_unregister(&i5k_amb_driver
);
605 static void __exit
i5k_amb_exit(void)
607 platform_device_unregister(amb_pdev
);
608 platform_driver_unregister(&i5k_amb_driver
);
611 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
612 MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
613 MODULE_LICENSE("GPL");
615 module_init(i5k_amb_init
);
616 module_exit(i5k_amb_exit
);