• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/hwmon/
1/*
2 * A hwmon driver for the Intel 5000 series chipset FB-DIMM AMB
3 * temperature sensors
4 * Copyright (C) 2007 IBM
5 *
6 * Author: Darrick J. Wong <djwong@us.ibm.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
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>
34
35#define DRVNAME "i5k_amb"
36
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
41
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
47
48#define AMB_CONFIG_SIZE			2048
49#define AMB_FUNC_3_OFFSET		768
50
51static unsigned long amb_reg_temp_status(unsigned int amb)
52{
53	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR +
54	       AMB_CONFIG_SIZE * amb;
55}
56
57static unsigned long amb_reg_temp_min(unsigned int amb)
58{
59	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR +
60	       AMB_CONFIG_SIZE * amb;
61}
62
63static unsigned long amb_reg_temp_mid(unsigned int amb)
64{
65	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR +
66	       AMB_CONFIG_SIZE * amb;
67}
68
69static unsigned long amb_reg_temp_max(unsigned int amb)
70{
71	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR +
72	       AMB_CONFIG_SIZE * amb;
73}
74
75static unsigned long amb_reg_temp(unsigned int amb)
76{
77	return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR +
78	       AMB_CONFIG_SIZE * amb;
79}
80
81#define MAX_MEM_CHANNELS		4
82#define MAX_AMBS_PER_CHANNEL		16
83#define MAX_AMBS			(MAX_MEM_CHANNELS * \
84					 MAX_AMBS_PER_CHANNEL)
85#define CHANNEL_SHIFT			4
86#define DIMM_MASK			0xF
87/*
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.
93 */
94#define REAL_MAX_AMBS_PER_CHANNEL	15
95#define KNOBS_PER_AMB			6
96
97static unsigned long amb_num_from_reg(unsigned int byte_num, unsigned int bit)
98{
99	return byte_num * MAX_AMBS_PER_CHANNEL + bit;
100}
101
102#define AMB_SYSFS_NAME_LEN		16
103struct i5k_device_attribute {
104	struct sensor_device_attribute s_attr;
105	char name[AMB_SYSFS_NAME_LEN];
106};
107
108struct i5k_amb_data {
109	struct device *hwmon_dev;
110
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;
117	unsigned long chipset_id;
118};
119
120static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
121			 char *buf)
122{
123	return sprintf(buf, "%s\n", DRVNAME);
124}
125
126
127static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
128
129static struct platform_device *amb_pdev;
130
131static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
132{
133	return ioread8(data->amb_mmio + offset);
134}
135
136static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
137			   u8 val)
138{
139	iowrite8(val, data->amb_mmio + offset);
140}
141
142static ssize_t show_amb_alarm(struct device *dev,
143			     struct device_attribute *devattr,
144			     char *buf)
145{
146	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
147	struct i5k_amb_data *data = dev_get_drvdata(dev);
148
149	if (!(amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x20) &&
150	     (amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x8))
151		return sprintf(buf, "1\n");
152	else
153		return sprintf(buf, "0\n");
154}
155
156static ssize_t store_amb_min(struct device *dev,
157			     struct device_attribute *devattr,
158			     const char *buf,
159			     size_t count)
160{
161	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
162	struct i5k_amb_data *data = dev_get_drvdata(dev);
163	unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
164
165	if (temp > 255)
166		temp = 255;
167
168	amb_write_byte(data, amb_reg_temp_min(attr->index), temp);
169	return count;
170}
171
172static ssize_t store_amb_mid(struct device *dev,
173			     struct device_attribute *devattr,
174			     const char *buf,
175			     size_t count)
176{
177	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
178	struct i5k_amb_data *data = dev_get_drvdata(dev);
179	unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
180
181	if (temp > 255)
182		temp = 255;
183
184	amb_write_byte(data, amb_reg_temp_mid(attr->index), temp);
185	return count;
186}
187
188static ssize_t store_amb_max(struct device *dev,
189			     struct device_attribute *devattr,
190			     const char *buf,
191			     size_t count)
192{
193	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
194	struct i5k_amb_data *data = dev_get_drvdata(dev);
195	unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
196
197	if (temp > 255)
198		temp = 255;
199
200	amb_write_byte(data, amb_reg_temp_max(attr->index), temp);
201	return count;
202}
203
204static ssize_t show_amb_min(struct device *dev,
205			     struct device_attribute *devattr,
206			     char *buf)
207{
208	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
209	struct i5k_amb_data *data = dev_get_drvdata(dev);
210	return sprintf(buf, "%d\n",
211		500 * amb_read_byte(data, amb_reg_temp_min(attr->index)));
212}
213
214static ssize_t show_amb_mid(struct device *dev,
215			     struct device_attribute *devattr,
216			     char *buf)
217{
218	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
219	struct i5k_amb_data *data = dev_get_drvdata(dev);
220	return sprintf(buf, "%d\n",
221		500 * amb_read_byte(data, amb_reg_temp_mid(attr->index)));
222}
223
224static ssize_t show_amb_max(struct device *dev,
225			     struct device_attribute *devattr,
226			     char *buf)
227{
228	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
229	struct i5k_amb_data *data = dev_get_drvdata(dev);
230	return sprintf(buf, "%d\n",
231		500 * amb_read_byte(data, amb_reg_temp_max(attr->index)));
232}
233
234static ssize_t show_amb_temp(struct device *dev,
235			     struct device_attribute *devattr,
236			     char *buf)
237{
238	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
239	struct i5k_amb_data *data = dev_get_drvdata(dev);
240	return sprintf(buf, "%d\n",
241		500 * amb_read_byte(data, amb_reg_temp(attr->index)));
242}
243
244static ssize_t show_label(struct device *dev,
245			  struct device_attribute *devattr,
246			  char *buf)
247{
248	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
249
250	return sprintf(buf, "Ch. %d DIMM %d\n", attr->index >> CHANNEL_SHIFT,
251		       attr->index & DIMM_MASK);
252}
253
254static int __devinit i5k_amb_hwmon_init(struct platform_device *pdev)
255{
256	int i, j, k, d = 0;
257	u16 c;
258	int res = 0;
259	int num_ambs = 0;
260	struct i5k_amb_data *data = platform_get_drvdata(pdev);
261
262	/* Count the number of AMBs found */
263	/* ignore the high-order bit, see "Ugly hack" comment above */
264	for (i = 0; i < MAX_MEM_CHANNELS; i++)
265		num_ambs += hweight16(data->amb_present[i] & 0x7fff);
266
267	/* Set up sysfs stuff */
268	data->attrs = kzalloc(sizeof(*data->attrs) * num_ambs * KNOBS_PER_AMB,
269				GFP_KERNEL);
270	if (!data->attrs)
271		return -ENOMEM;
272	data->num_attrs = 0;
273
274	for (i = 0; i < MAX_MEM_CHANNELS; i++) {
275		c = data->amb_present[i];
276		for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
277			struct i5k_device_attribute *iattr;
278
279			k = amb_num_from_reg(i, j);
280			if (!(c & 0x1))
281				continue;
282			d++;
283
284			/* sysfs label */
285			iattr = data->attrs + data->num_attrs;
286			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
287				 "temp%d_label", d);
288			iattr->s_attr.dev_attr.attr.name = iattr->name;
289			iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
290			iattr->s_attr.dev_attr.show = show_label;
291			iattr->s_attr.index = k;
292			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
293			res = device_create_file(&pdev->dev,
294						 &iattr->s_attr.dev_attr);
295			if (res)
296				goto exit_remove;
297			data->num_attrs++;
298
299			/* Temperature sysfs knob */
300			iattr = data->attrs + data->num_attrs;
301			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
302				 "temp%d_input", d);
303			iattr->s_attr.dev_attr.attr.name = iattr->name;
304			iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
305			iattr->s_attr.dev_attr.show = show_amb_temp;
306			iattr->s_attr.index = k;
307			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
308			res = device_create_file(&pdev->dev,
309						 &iattr->s_attr.dev_attr);
310			if (res)
311				goto exit_remove;
312			data->num_attrs++;
313
314			/* Temperature min sysfs knob */
315			iattr = data->attrs + data->num_attrs;
316			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
317				 "temp%d_min", d);
318			iattr->s_attr.dev_attr.attr.name = iattr->name;
319			iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
320			iattr->s_attr.dev_attr.show = show_amb_min;
321			iattr->s_attr.dev_attr.store = store_amb_min;
322			iattr->s_attr.index = k;
323			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
324			res = device_create_file(&pdev->dev,
325						 &iattr->s_attr.dev_attr);
326			if (res)
327				goto exit_remove;
328			data->num_attrs++;
329
330			/* Temperature mid sysfs knob */
331			iattr = data->attrs + data->num_attrs;
332			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
333				 "temp%d_mid", d);
334			iattr->s_attr.dev_attr.attr.name = iattr->name;
335			iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
336			iattr->s_attr.dev_attr.show = show_amb_mid;
337			iattr->s_attr.dev_attr.store = store_amb_mid;
338			iattr->s_attr.index = k;
339			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
340			res = device_create_file(&pdev->dev,
341						 &iattr->s_attr.dev_attr);
342			if (res)
343				goto exit_remove;
344			data->num_attrs++;
345
346			/* Temperature max sysfs knob */
347			iattr = data->attrs + data->num_attrs;
348			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
349				 "temp%d_max", d);
350			iattr->s_attr.dev_attr.attr.name = iattr->name;
351			iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
352			iattr->s_attr.dev_attr.show = show_amb_max;
353			iattr->s_attr.dev_attr.store = store_amb_max;
354			iattr->s_attr.index = k;
355			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
356			res = device_create_file(&pdev->dev,
357						 &iattr->s_attr.dev_attr);
358			if (res)
359				goto exit_remove;
360			data->num_attrs++;
361
362			/* Temperature alarm sysfs knob */
363			iattr = data->attrs + data->num_attrs;
364			snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
365				 "temp%d_alarm", d);
366			iattr->s_attr.dev_attr.attr.name = iattr->name;
367			iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
368			iattr->s_attr.dev_attr.show = show_amb_alarm;
369			iattr->s_attr.index = k;
370			sysfs_attr_init(&iattr->s_attr.dev_attr.attr);
371			res = device_create_file(&pdev->dev,
372						 &iattr->s_attr.dev_attr);
373			if (res)
374				goto exit_remove;
375			data->num_attrs++;
376		}
377	}
378
379	res = device_create_file(&pdev->dev, &dev_attr_name);
380	if (res)
381		goto exit_remove;
382
383	data->hwmon_dev = hwmon_device_register(&pdev->dev);
384	if (IS_ERR(data->hwmon_dev)) {
385		res = PTR_ERR(data->hwmon_dev);
386		goto exit_remove;
387	}
388
389	return res;
390
391exit_remove:
392	device_remove_file(&pdev->dev, &dev_attr_name);
393	for (i = 0; i < data->num_attrs; i++)
394		device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
395	kfree(data->attrs);
396
397	return res;
398}
399
400static int __devinit i5k_amb_add(void)
401{
402	int res = -ENODEV;
403
404	/* only ever going to be one of these */
405	amb_pdev = platform_device_alloc(DRVNAME, 0);
406	if (!amb_pdev)
407		return -ENOMEM;
408
409	res = platform_device_add(amb_pdev);
410	if (res)
411		goto err;
412	return 0;
413
414err:
415	platform_device_put(amb_pdev);
416	return res;
417}
418
419static int __devinit i5k_find_amb_registers(struct i5k_amb_data *data,
420					    unsigned long devid)
421{
422	struct pci_dev *pcidev;
423	u32 val32;
424	int res = -ENODEV;
425
426	/* Find AMB register memory space */
427	pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
428				devid,
429				NULL);
430	if (!pcidev)
431		return -ENODEV;
432
433	if (pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32))
434		goto out;
435	data->amb_base = val32;
436
437	if (pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32))
438		goto out;
439	data->amb_len = val32;
440
441	/* Is it big enough? */
442	if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
443		dev_err(&pcidev->dev, "AMB region too small!\n");
444		goto out;
445	}
446
447	data->chipset_id = devid;
448
449	res = 0;
450out:
451	pci_dev_put(pcidev);
452	return res;
453}
454
455static int __devinit i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
456{
457	struct pci_dev *pcidev;
458	u16 val16;
459	int res = -ENODEV;
460
461	/* Copy the DIMM presence map for these two channels */
462	pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
463	if (!pcidev)
464		return -ENODEV;
465
466	if (pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16))
467		goto out;
468	amb_present[0] = val16;
469
470	if (pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16))
471		goto out;
472	amb_present[1] = val16;
473
474	res = 0;
475
476out:
477	pci_dev_put(pcidev);
478	return res;
479}
480
481static unsigned long i5k_channel_pci_id(struct i5k_amb_data *data,
482					unsigned long channel)
483{
484	switch (data->chipset_id) {
485	case PCI_DEVICE_ID_INTEL_5000_ERR:
486		return PCI_DEVICE_ID_INTEL_5000_FBD0 + channel;
487	case PCI_DEVICE_ID_INTEL_5400_ERR:
488		return PCI_DEVICE_ID_INTEL_5400_FBD0 + channel;
489	default:
490		BUG();
491	}
492}
493
494static unsigned long chipset_ids[] = {
495	PCI_DEVICE_ID_INTEL_5000_ERR,
496	PCI_DEVICE_ID_INTEL_5400_ERR,
497	0
498};
499
500static 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) },
503	{ 0, }
504};
505MODULE_DEVICE_TABLE(pci, i5k_amb_ids);
506
507static int __devinit i5k_amb_probe(struct platform_device *pdev)
508{
509	struct i5k_amb_data *data;
510	struct resource *reso;
511	int i;
512	int res = -ENODEV;
513
514	data = kzalloc(sizeof(*data), GFP_KERNEL);
515	if (!data)
516		return -ENOMEM;
517
518	/* Figure out where the AMB registers live */
519	i = 0;
520	do {
521		res = i5k_find_amb_registers(data, chipset_ids[i]);
522		i++;
523	} while (res && chipset_ids[i]);
524
525	if (res)
526		goto err;
527
528	/* Copy the DIMM presence map for the first two channels */
529	res = i5k_channel_probe(&data->amb_present[0],
530				i5k_channel_pci_id(data, 0));
531	if (res)
532		goto err;
533
534	/* Copy the DIMM presence map for the optional second two channels */
535	i5k_channel_probe(&data->amb_present[2],
536			  i5k_channel_pci_id(data, 1));
537
538	/* Set up resource regions */
539	reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
540	if (!reso) {
541		res = -EBUSY;
542		goto err;
543	}
544
545	data->amb_mmio = ioremap_nocache(data->amb_base, data->amb_len);
546	if (!data->amb_mmio) {
547		res = -EBUSY;
548		goto err_map_failed;
549	}
550
551	platform_set_drvdata(pdev, data);
552
553	res = i5k_amb_hwmon_init(pdev);
554	if (res)
555		goto err_init_failed;
556
557	return res;
558
559err_init_failed:
560	iounmap(data->amb_mmio);
561	platform_set_drvdata(pdev, NULL);
562err_map_failed:
563	release_mem_region(data->amb_base, data->amb_len);
564err:
565	kfree(data);
566	return res;
567}
568
569static int __devexit i5k_amb_remove(struct platform_device *pdev)
570{
571	int i;
572	struct i5k_amb_data *data = platform_get_drvdata(pdev);
573
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);
578	kfree(data->attrs);
579	iounmap(data->amb_mmio);
580	release_mem_region(data->amb_base, data->amb_len);
581	platform_set_drvdata(pdev, NULL);
582	kfree(data);
583	return 0;
584}
585
586static struct platform_driver i5k_amb_driver = {
587	.driver = {
588		.owner = THIS_MODULE,
589		.name = DRVNAME,
590	},
591	.probe = i5k_amb_probe,
592	.remove = __devexit_p(i5k_amb_remove),
593};
594
595static int __init i5k_amb_init(void)
596{
597	int res;
598
599	res = platform_driver_register(&i5k_amb_driver);
600	if (res)
601		return res;
602
603	res = i5k_amb_add();
604	if (res)
605		platform_driver_unregister(&i5k_amb_driver);
606
607	return res;
608}
609
610static void __exit i5k_amb_exit(void)
611{
612	platform_device_unregister(amb_pdev);
613	platform_driver_unregister(&i5k_amb_driver);
614}
615
616MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
617MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
618MODULE_LICENSE("GPL");
619
620module_init(i5k_amb_init);
621module_exit(i5k_amb_exit);
622