1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB
5 * Copyright (C) 2007 IBM
7 * Author: Darrick J. Wong <darrick.wong@oracle.com>
10 #include <linux/module.h>
11 #include <linux/hwmon.h>
12 #include <linux/hwmon-sysfs.h>
13 #include <linux/err.h>
14 #include <linux/mutex.h>
15 #include <linux/log2.h>
16 #include <linux/pci.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
20 #define DRVNAME "i5k_amb"
22 #define I5K_REG_AMB_BASE_ADDR 0x48
23 #define I5K_REG_AMB_LEN_ADDR 0x50
24 #define I5K_REG_CHAN0_PRESENCE_ADDR 0x64
25 #define I5K_REG_CHAN1_PRESENCE_ADDR 0x66
27 #define AMB_REG_TEMP_MIN_ADDR 0x80
28 #define AMB_REG_TEMP_MID_ADDR 0x81
29 #define AMB_REG_TEMP_MAX_ADDR 0x82
30 #define AMB_REG_TEMP_STATUS_ADDR 0x84
31 #define AMB_REG_TEMP_ADDR 0x85
33 #define AMB_CONFIG_SIZE 2048
34 #define AMB_FUNC_3_OFFSET 768
36 static unsigned long amb_reg_temp_status(unsigned int amb
)
38 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_STATUS_ADDR
+
39 AMB_CONFIG_SIZE
* amb
;
42 static unsigned long amb_reg_temp_min(unsigned int amb
)
44 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_MIN_ADDR
+
45 AMB_CONFIG_SIZE
* amb
;
48 static unsigned long amb_reg_temp_mid(unsigned int amb
)
50 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_MID_ADDR
+
51 AMB_CONFIG_SIZE
* amb
;
54 static unsigned long amb_reg_temp_max(unsigned int amb
)
56 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_MAX_ADDR
+
57 AMB_CONFIG_SIZE
* amb
;
60 static unsigned long amb_reg_temp(unsigned int amb
)
62 return AMB_FUNC_3_OFFSET
+ AMB_REG_TEMP_ADDR
+
63 AMB_CONFIG_SIZE
* amb
;
66 #define MAX_MEM_CHANNELS 4
67 #define MAX_AMBS_PER_CHANNEL 16
68 #define MAX_AMBS (MAX_MEM_CHANNELS * \
70 #define CHANNEL_SHIFT 4
73 * Ugly hack: For some reason the highest bit is set if there
74 * are _any_ DIMMs in the channel. Attempting to read from
75 * this "high-order" AMB results in a memory bus error, so
76 * for now we'll just ignore that top bit, even though that
77 * might prevent us from seeing the 16th DIMM in the channel.
79 #define REAL_MAX_AMBS_PER_CHANNEL 15
80 #define KNOBS_PER_AMB 6
82 static unsigned long amb_num_from_reg(unsigned int byte_num
, unsigned int bit
)
84 return byte_num
* MAX_AMBS_PER_CHANNEL
+ bit
;
87 #define AMB_SYSFS_NAME_LEN 16
88 struct i5k_device_attribute
{
89 struct sensor_device_attribute s_attr
;
90 char name
[AMB_SYSFS_NAME_LEN
];
94 struct device
*hwmon_dev
;
96 unsigned long amb_base
;
97 unsigned long amb_len
;
98 u16 amb_present
[MAX_MEM_CHANNELS
];
99 void __iomem
*amb_mmio
;
100 struct i5k_device_attribute
*attrs
;
101 unsigned int num_attrs
;
104 static DEVICE_STRING_ATTR_RO(name
, 0444, DRVNAME
);
106 static struct platform_device
*amb_pdev
;
108 static u8
amb_read_byte(struct i5k_amb_data
*data
, unsigned long offset
)
110 return ioread8(data
->amb_mmio
+ offset
);
113 static void amb_write_byte(struct i5k_amb_data
*data
, unsigned long offset
,
116 iowrite8(val
, data
->amb_mmio
+ offset
);
119 static ssize_t
show_amb_alarm(struct device
*dev
,
120 struct device_attribute
*devattr
,
123 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
124 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
126 if (!(amb_read_byte(data
, amb_reg_temp_status(attr
->index
)) & 0x20) &&
127 (amb_read_byte(data
, amb_reg_temp_status(attr
->index
)) & 0x8))
128 return sprintf(buf
, "1\n");
130 return sprintf(buf
, "0\n");
133 static ssize_t
store_amb_min(struct device
*dev
,
134 struct device_attribute
*devattr
,
138 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
139 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
141 int ret
= kstrtoul(buf
, 10, &temp
);
149 amb_write_byte(data
, amb_reg_temp_min(attr
->index
), temp
);
153 static ssize_t
store_amb_mid(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_mid(attr
->index
), temp
);
173 static ssize_t
store_amb_max(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_max(attr
->index
), temp
);
193 static ssize_t
show_amb_min(struct device
*dev
,
194 struct device_attribute
*devattr
,
197 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
198 struct i5k_amb_data
*data
= dev_get_drvdata(dev
);
199 return sprintf(buf
, "%d\n",
200 500 * amb_read_byte(data
, amb_reg_temp_min(attr
->index
)));
203 static ssize_t
show_amb_mid(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_mid(attr
->index
)));
213 static ssize_t
show_amb_max(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_max(attr
->index
)));
223 static ssize_t
show_amb_temp(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(attr
->index
)));
233 static ssize_t
show_label(struct device
*dev
,
234 struct device_attribute
*devattr
,
237 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
239 return sprintf(buf
, "Ch. %d DIMM %d\n", attr
->index
>> CHANNEL_SHIFT
,
240 attr
->index
& DIMM_MASK
);
243 static int i5k_amb_hwmon_init(struct platform_device
*pdev
)
249 struct i5k_amb_data
*data
= platform_get_drvdata(pdev
);
251 /* Count the number of AMBs found */
252 /* ignore the high-order bit, see "Ugly hack" comment above */
253 for (i
= 0; i
< MAX_MEM_CHANNELS
; i
++)
254 num_ambs
+= hweight16(data
->amb_present
[i
] & 0x7fff);
256 /* Set up sysfs stuff */
257 data
->attrs
= kzalloc(array3_size(num_ambs
, KNOBS_PER_AMB
,
258 sizeof(*data
->attrs
)),
264 for (i
= 0; i
< MAX_MEM_CHANNELS
; i
++) {
265 c
= data
->amb_present
[i
];
266 for (j
= 0; j
< REAL_MAX_AMBS_PER_CHANNEL
; j
++, c
>>= 1) {
267 struct i5k_device_attribute
*iattr
;
269 k
= amb_num_from_reg(i
, j
);
275 iattr
= data
->attrs
+ data
->num_attrs
;
276 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
278 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
279 iattr
->s_attr
.dev_attr
.attr
.mode
= 0444;
280 iattr
->s_attr
.dev_attr
.show
= show_label
;
281 iattr
->s_attr
.index
= k
;
282 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
283 res
= device_create_file(&pdev
->dev
,
284 &iattr
->s_attr
.dev_attr
);
289 /* Temperature sysfs knob */
290 iattr
= data
->attrs
+ data
->num_attrs
;
291 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
293 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
294 iattr
->s_attr
.dev_attr
.attr
.mode
= 0444;
295 iattr
->s_attr
.dev_attr
.show
= show_amb_temp
;
296 iattr
->s_attr
.index
= k
;
297 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
298 res
= device_create_file(&pdev
->dev
,
299 &iattr
->s_attr
.dev_attr
);
304 /* Temperature min sysfs knob */
305 iattr
= data
->attrs
+ data
->num_attrs
;
306 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
308 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
309 iattr
->s_attr
.dev_attr
.attr
.mode
= 0644;
310 iattr
->s_attr
.dev_attr
.show
= show_amb_min
;
311 iattr
->s_attr
.dev_attr
.store
= store_amb_min
;
312 iattr
->s_attr
.index
= k
;
313 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
314 res
= device_create_file(&pdev
->dev
,
315 &iattr
->s_attr
.dev_attr
);
320 /* Temperature mid sysfs knob */
321 iattr
= data
->attrs
+ data
->num_attrs
;
322 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
324 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
325 iattr
->s_attr
.dev_attr
.attr
.mode
= 0644;
326 iattr
->s_attr
.dev_attr
.show
= show_amb_mid
;
327 iattr
->s_attr
.dev_attr
.store
= store_amb_mid
;
328 iattr
->s_attr
.index
= k
;
329 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
330 res
= device_create_file(&pdev
->dev
,
331 &iattr
->s_attr
.dev_attr
);
336 /* Temperature max sysfs knob */
337 iattr
= data
->attrs
+ data
->num_attrs
;
338 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
340 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
341 iattr
->s_attr
.dev_attr
.attr
.mode
= 0644;
342 iattr
->s_attr
.dev_attr
.show
= show_amb_max
;
343 iattr
->s_attr
.dev_attr
.store
= store_amb_max
;
344 iattr
->s_attr
.index
= k
;
345 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
346 res
= device_create_file(&pdev
->dev
,
347 &iattr
->s_attr
.dev_attr
);
352 /* Temperature alarm sysfs knob */
353 iattr
= data
->attrs
+ data
->num_attrs
;
354 snprintf(iattr
->name
, AMB_SYSFS_NAME_LEN
,
356 iattr
->s_attr
.dev_attr
.attr
.name
= iattr
->name
;
357 iattr
->s_attr
.dev_attr
.attr
.mode
= 0444;
358 iattr
->s_attr
.dev_attr
.show
= show_amb_alarm
;
359 iattr
->s_attr
.index
= k
;
360 sysfs_attr_init(&iattr
->s_attr
.dev_attr
.attr
);
361 res
= device_create_file(&pdev
->dev
,
362 &iattr
->s_attr
.dev_attr
);
369 res
= device_create_file(&pdev
->dev
, &dev_attr_name
.attr
);
373 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
374 if (IS_ERR(data
->hwmon_dev
)) {
375 res
= PTR_ERR(data
->hwmon_dev
);
382 device_remove_file(&pdev
->dev
, &dev_attr_name
.attr
);
383 for (i
= 0; i
< data
->num_attrs
; i
++)
384 device_remove_file(&pdev
->dev
, &data
->attrs
[i
].s_attr
.dev_attr
);
390 static int i5k_amb_add(void)
394 /* only ever going to be one of these */
395 amb_pdev
= platform_device_alloc(DRVNAME
, 0);
399 res
= platform_device_add(amb_pdev
);
405 platform_device_put(amb_pdev
);
409 static int i5k_find_amb_registers(struct i5k_amb_data
*data
,
412 struct pci_dev
*pcidev
;
416 /* Find AMB register memory space */
417 pcidev
= pci_get_device(PCI_VENDOR_ID_INTEL
,
423 pci_read_config_dword(pcidev
, I5K_REG_AMB_BASE_ADDR
, &val32
);
424 if (val32
== (u32
)~0)
426 data
->amb_base
= val32
;
428 pci_read_config_dword(pcidev
, I5K_REG_AMB_LEN_ADDR
, &val32
);
429 if (val32
== (u32
)~0)
431 data
->amb_len
= val32
;
433 /* Is it big enough? */
434 if (data
->amb_len
< AMB_CONFIG_SIZE
* MAX_AMBS
) {
435 dev_err(&pcidev
->dev
, "AMB region too small!\n");
445 static int i5k_channel_probe(u16
*amb_present
, unsigned long dev_id
)
447 struct pci_dev
*pcidev
;
451 /* Copy the DIMM presence map for these two channels */
452 pcidev
= pci_get_device(PCI_VENDOR_ID_INTEL
, dev_id
, NULL
);
456 pci_read_config_word(pcidev
, I5K_REG_CHAN0_PRESENCE_ADDR
, &val16
);
457 if (val16
== (u16
)~0)
459 amb_present
[0] = val16
;
461 pci_read_config_word(pcidev
, I5K_REG_CHAN1_PRESENCE_ADDR
, &val16
);
462 if (val16
== (u16
)~0)
464 amb_present
[1] = val16
;
477 { PCI_DEVICE_ID_INTEL_5000_ERR
, PCI_DEVICE_ID_INTEL_5000_FBD0
},
478 { PCI_DEVICE_ID_INTEL_5400_ERR
, PCI_DEVICE_ID_INTEL_5400_FBD0
},
483 static const struct pci_device_id i5k_amb_ids
[] = {
484 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_5000_ERR
) },
485 { PCI_DEVICE(PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_5400_ERR
) },
488 MODULE_DEVICE_TABLE(pci
, i5k_amb_ids
);
491 static int i5k_amb_probe(struct platform_device
*pdev
)
493 struct i5k_amb_data
*data
;
494 struct resource
*reso
;
497 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
501 /* Figure out where the AMB registers live */
504 res
= i5k_find_amb_registers(data
, chipset_ids
[i
].err
);
508 } while (chipset_ids
[i
].err
);
513 /* Copy the DIMM presence map for the first two channels */
514 res
= i5k_channel_probe(&data
->amb_present
[0], chipset_ids
[i
].fbd0
);
518 /* Copy the DIMM presence map for the optional second two channels */
519 i5k_channel_probe(&data
->amb_present
[2], chipset_ids
[i
].fbd0
+ 1);
521 /* Set up resource regions */
522 reso
= request_mem_region(data
->amb_base
, data
->amb_len
, DRVNAME
);
528 data
->amb_mmio
= ioremap(data
->amb_base
, data
->amb_len
);
529 if (!data
->amb_mmio
) {
534 platform_set_drvdata(pdev
, data
);
536 res
= i5k_amb_hwmon_init(pdev
);
538 goto err_init_failed
;
543 iounmap(data
->amb_mmio
);
545 release_mem_region(data
->amb_base
, data
->amb_len
);
551 static void i5k_amb_remove(struct platform_device
*pdev
)
554 struct i5k_amb_data
*data
= platform_get_drvdata(pdev
);
556 hwmon_device_unregister(data
->hwmon_dev
);
557 device_remove_file(&pdev
->dev
, &dev_attr_name
.attr
);
558 for (i
= 0; i
< data
->num_attrs
; i
++)
559 device_remove_file(&pdev
->dev
, &data
->attrs
[i
].s_attr
.dev_attr
);
561 iounmap(data
->amb_mmio
);
562 release_mem_region(data
->amb_base
, data
->amb_len
);
566 static struct platform_driver i5k_amb_driver
= {
570 .probe
= i5k_amb_probe
,
571 .remove
= i5k_amb_remove
,
574 static int __init
i5k_amb_init(void)
578 res
= platform_driver_register(&i5k_amb_driver
);
584 platform_driver_unregister(&i5k_amb_driver
);
589 static void __exit
i5k_amb_exit(void)
591 platform_device_unregister(amb_pdev
);
592 platform_driver_unregister(&i5k_amb_driver
);
595 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
596 MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
597 MODULE_LICENSE("GPL");
599 module_init(i5k_amb_init
);
600 module_exit(i5k_amb_exit
);