i5k_amb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. #include <linux/module.h>
  23. #include <linux/jiffies.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/delay.h>
  29. #include <linux/log2.h>
  30. #include <linux/pci.h>
  31. #include <linux/platform_device.h>
  32. #define DRVNAME "i5k_amb"
  33. #define I5K_REG_AMB_BASE_ADDR 0x48
  34. #define I5K_REG_AMB_LEN_ADDR 0x50
  35. #define I5K_REG_CHAN0_PRESENCE_ADDR 0x64
  36. #define I5K_REG_CHAN1_PRESENCE_ADDR 0x66
  37. #define AMB_REG_TEMP_MIN_ADDR 0x80
  38. #define AMB_REG_TEMP_MID_ADDR 0x81
  39. #define AMB_REG_TEMP_MAX_ADDR 0x82
  40. #define AMB_REG_TEMP_STATUS_ADDR 0x84
  41. #define AMB_REG_TEMP_ADDR 0x85
  42. #define AMB_CONFIG_SIZE 2048
  43. #define AMB_FUNC_3_OFFSET 768
  44. static unsigned long amb_reg_temp_status(unsigned int amb)
  45. {
  46. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_STATUS_ADDR +
  47. AMB_CONFIG_SIZE * amb;
  48. }
  49. static unsigned long amb_reg_temp_min(unsigned int amb)
  50. {
  51. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MIN_ADDR +
  52. AMB_CONFIG_SIZE * amb;
  53. }
  54. static unsigned long amb_reg_temp_mid(unsigned int amb)
  55. {
  56. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MID_ADDR +
  57. AMB_CONFIG_SIZE * amb;
  58. }
  59. static unsigned long amb_reg_temp_max(unsigned int amb)
  60. {
  61. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_MAX_ADDR +
  62. AMB_CONFIG_SIZE * amb;
  63. }
  64. static unsigned long amb_reg_temp(unsigned int amb)
  65. {
  66. return AMB_FUNC_3_OFFSET + AMB_REG_TEMP_ADDR +
  67. AMB_CONFIG_SIZE * amb;
  68. }
  69. #define MAX_MEM_CHANNELS 4
  70. #define MAX_AMBS_PER_CHANNEL 16
  71. #define MAX_AMBS (MAX_MEM_CHANNELS * \
  72. MAX_AMBS_PER_CHANNEL)
  73. /*
  74. * Ugly hack: For some reason the highest bit is set if there
  75. * are _any_ DIMMs in the channel. Attempting to read from
  76. * this "high-order" AMB results in a memory bus error, so
  77. * for now we'll just ignore that top bit, even though that
  78. * might prevent us from seeing the 16th DIMM in the channel.
  79. */
  80. #define REAL_MAX_AMBS_PER_CHANNEL 15
  81. #define KNOBS_PER_AMB 5
  82. static unsigned long amb_num_from_reg(unsigned int byte_num, unsigned int bit)
  83. {
  84. return byte_num * MAX_AMBS_PER_CHANNEL + bit;
  85. }
  86. #define AMB_SYSFS_NAME_LEN 16
  87. struct i5k_device_attribute {
  88. struct sensor_device_attribute s_attr;
  89. char name[AMB_SYSFS_NAME_LEN];
  90. };
  91. struct i5k_amb_data {
  92. struct device *hwmon_dev;
  93. unsigned long amb_base;
  94. unsigned long amb_len;
  95. u16 amb_present[MAX_MEM_CHANNELS];
  96. void __iomem *amb_mmio;
  97. struct i5k_device_attribute *attrs;
  98. unsigned int num_attrs;
  99. unsigned long chipset_id;
  100. };
  101. static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
  102. char *buf)
  103. {
  104. return sprintf(buf, "%s\n", DRVNAME);
  105. }
  106. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  107. static struct platform_device *amb_pdev;
  108. static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
  109. {
  110. return ioread8(data->amb_mmio + offset);
  111. }
  112. static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
  113. u8 val)
  114. {
  115. iowrite8(val, data->amb_mmio + offset);
  116. }
  117. static ssize_t show_amb_alarm(struct device *dev,
  118. struct device_attribute *devattr,
  119. char *buf)
  120. {
  121. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  122. struct i5k_amb_data *data = dev_get_drvdata(dev);
  123. if (!(amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x20) &&
  124. (amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x8))
  125. return sprintf(buf, "1\n");
  126. else
  127. return sprintf(buf, "0\n");
  128. }
  129. static ssize_t store_amb_min(struct device *dev,
  130. struct device_attribute *devattr,
  131. const char *buf,
  132. size_t count)
  133. {
  134. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  135. struct i5k_amb_data *data = dev_get_drvdata(dev);
  136. unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
  137. if (temp > 255)
  138. temp = 255;
  139. amb_write_byte(data, amb_reg_temp_min(attr->index), temp);
  140. return count;
  141. }
  142. static ssize_t store_amb_mid(struct device *dev,
  143. struct device_attribute *devattr,
  144. const char *buf,
  145. size_t count)
  146. {
  147. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  148. struct i5k_amb_data *data = dev_get_drvdata(dev);
  149. unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
  150. if (temp > 255)
  151. temp = 255;
  152. amb_write_byte(data, amb_reg_temp_mid(attr->index), temp);
  153. return count;
  154. }
  155. static ssize_t store_amb_max(struct device *dev,
  156. struct device_attribute *devattr,
  157. const char *buf,
  158. size_t count)
  159. {
  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;
  163. if (temp > 255)
  164. temp = 255;
  165. amb_write_byte(data, amb_reg_temp_max(attr->index), temp);
  166. return count;
  167. }
  168. static ssize_t show_amb_min(struct device *dev,
  169. struct device_attribute *devattr,
  170. char *buf)
  171. {
  172. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  173. struct i5k_amb_data *data = dev_get_drvdata(dev);
  174. return sprintf(buf, "%d\n",
  175. 500 * amb_read_byte(data, amb_reg_temp_min(attr->index)));
  176. }
  177. static ssize_t show_amb_mid(struct device *dev,
  178. struct device_attribute *devattr,
  179. char *buf)
  180. {
  181. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  182. struct i5k_amb_data *data = dev_get_drvdata(dev);
  183. return sprintf(buf, "%d\n",
  184. 500 * amb_read_byte(data, amb_reg_temp_mid(attr->index)));
  185. }
  186. static ssize_t show_amb_max(struct device *dev,
  187. struct device_attribute *devattr,
  188. char *buf)
  189. {
  190. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  191. struct i5k_amb_data *data = dev_get_drvdata(dev);
  192. return sprintf(buf, "%d\n",
  193. 500 * amb_read_byte(data, amb_reg_temp_max(attr->index)));
  194. }
  195. static ssize_t show_amb_temp(struct device *dev,
  196. struct device_attribute *devattr,
  197. char *buf)
  198. {
  199. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  200. struct i5k_amb_data *data = dev_get_drvdata(dev);
  201. return sprintf(buf, "%d\n",
  202. 500 * amb_read_byte(data, amb_reg_temp(attr->index)));
  203. }
  204. static int __devinit i5k_amb_hwmon_init(struct platform_device *pdev)
  205. {
  206. int i, j, k, d = 0;
  207. u16 c;
  208. int res = 0;
  209. int num_ambs = 0;
  210. struct i5k_amb_data *data = platform_get_drvdata(pdev);
  211. /* Count the number of AMBs found */
  212. /* ignore the high-order bit, see "Ugly hack" comment above */
  213. for (i = 0; i < MAX_MEM_CHANNELS; i++)
  214. num_ambs += hweight16(data->amb_present[i] & 0x7fff);
  215. /* Set up sysfs stuff */
  216. data->attrs = kzalloc(sizeof(*data->attrs) * num_ambs * KNOBS_PER_AMB,
  217. GFP_KERNEL);
  218. if (!data->attrs)
  219. return -ENOMEM;
  220. data->num_attrs = 0;
  221. for (i = 0; i < MAX_MEM_CHANNELS; i++) {
  222. c = data->amb_present[i];
  223. for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
  224. struct i5k_device_attribute *iattr;
  225. k = amb_num_from_reg(i, j);
  226. if (!(c & 0x1))
  227. continue;
  228. d++;
  229. /* Temperature sysfs knob */
  230. iattr = data->attrs + data->num_attrs;
  231. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  232. "temp%d_input", d);
  233. iattr->s_attr.dev_attr.attr.name = iattr->name;
  234. iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
  235. iattr->s_attr.dev_attr.show = show_amb_temp;
  236. iattr->s_attr.index = k;
  237. res = device_create_file(&pdev->dev,
  238. &iattr->s_attr.dev_attr);
  239. if (res)
  240. goto exit_remove;
  241. data->num_attrs++;
  242. /* Temperature min sysfs knob */
  243. iattr = data->attrs + data->num_attrs;
  244. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  245. "temp%d_min", d);
  246. iattr->s_attr.dev_attr.attr.name = iattr->name;
  247. iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  248. iattr->s_attr.dev_attr.show = show_amb_min;
  249. iattr->s_attr.dev_attr.store = store_amb_min;
  250. iattr->s_attr.index = k;
  251. res = device_create_file(&pdev->dev,
  252. &iattr->s_attr.dev_attr);
  253. if (res)
  254. goto exit_remove;
  255. data->num_attrs++;
  256. /* Temperature mid sysfs knob */
  257. iattr = data->attrs + data->num_attrs;
  258. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  259. "temp%d_mid", d);
  260. iattr->s_attr.dev_attr.attr.name = iattr->name;
  261. iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  262. iattr->s_attr.dev_attr.show = show_amb_mid;
  263. iattr->s_attr.dev_attr.store = store_amb_mid;
  264. iattr->s_attr.index = k;
  265. res = device_create_file(&pdev->dev,
  266. &iattr->s_attr.dev_attr);
  267. if (res)
  268. goto exit_remove;
  269. data->num_attrs++;
  270. /* Temperature max sysfs knob */
  271. iattr = data->attrs + data->num_attrs;
  272. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  273. "temp%d_max", d);
  274. iattr->s_attr.dev_attr.attr.name = iattr->name;
  275. iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  276. iattr->s_attr.dev_attr.show = show_amb_max;
  277. iattr->s_attr.dev_attr.store = store_amb_max;
  278. iattr->s_attr.index = k;
  279. res = device_create_file(&pdev->dev,
  280. &iattr->s_attr.dev_attr);
  281. if (res)
  282. goto exit_remove;
  283. data->num_attrs++;
  284. /* Temperature alarm sysfs knob */
  285. iattr = data->attrs + data->num_attrs;
  286. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  287. "temp%d_alarm", 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_amb_alarm;
  291. iattr->s_attr.index = k;
  292. res = device_create_file(&pdev->dev,
  293. &iattr->s_attr.dev_attr);
  294. if (res)
  295. goto exit_remove;
  296. data->num_attrs++;
  297. }
  298. }
  299. res = device_create_file(&pdev->dev, &dev_attr_name);
  300. if (res)
  301. goto exit_remove;
  302. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  303. if (IS_ERR(data->hwmon_dev)) {
  304. res = PTR_ERR(data->hwmon_dev);
  305. goto exit_remove;
  306. }
  307. return res;
  308. exit_remove:
  309. device_remove_file(&pdev->dev, &dev_attr_name);
  310. for (i = 0; i < data->num_attrs; i++)
  311. device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
  312. kfree(data->attrs);
  313. return res;
  314. }
  315. static int __devinit i5k_amb_add(void)
  316. {
  317. int res = -ENODEV;
  318. /* only ever going to be one of these */
  319. amb_pdev = platform_device_alloc(DRVNAME, 0);
  320. if (!amb_pdev)
  321. return -ENOMEM;
  322. res = platform_device_add(amb_pdev);
  323. if (res)
  324. goto err;
  325. return 0;
  326. err:
  327. platform_device_put(amb_pdev);
  328. return res;
  329. }
  330. static int __devinit i5k_find_amb_registers(struct i5k_amb_data *data,
  331. unsigned long devid)
  332. {
  333. struct pci_dev *pcidev;
  334. u32 val32;
  335. int res = -ENODEV;
  336. /* Find AMB register memory space */
  337. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  338. devid,
  339. NULL);
  340. if (!pcidev)
  341. return -ENODEV;
  342. if (pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32))
  343. goto out;
  344. data->amb_base = val32;
  345. if (pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32))
  346. goto out;
  347. data->amb_len = val32;
  348. /* Is it big enough? */
  349. if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
  350. dev_err(&pcidev->dev, "AMB region too small!\n");
  351. goto out;
  352. }
  353. data->chipset_id = devid;
  354. res = 0;
  355. out:
  356. pci_dev_put(pcidev);
  357. return res;
  358. }
  359. static int __devinit i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
  360. {
  361. struct pci_dev *pcidev;
  362. u16 val16;
  363. int res = -ENODEV;
  364. /* Copy the DIMM presence map for these two channels */
  365. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
  366. if (!pcidev)
  367. return -ENODEV;
  368. if (pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16))
  369. goto out;
  370. amb_present[0] = val16;
  371. if (pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16))
  372. goto out;
  373. amb_present[1] = val16;
  374. res = 0;
  375. out:
  376. pci_dev_put(pcidev);
  377. return res;
  378. }
  379. static unsigned long i5k_channel_pci_id(struct i5k_amb_data *data,
  380. unsigned long channel)
  381. {
  382. switch (data->chipset_id) {
  383. case PCI_DEVICE_ID_INTEL_5000_ERR:
  384. return PCI_DEVICE_ID_INTEL_5000_FBD0 + channel;
  385. case PCI_DEVICE_ID_INTEL_5400_ERR:
  386. return PCI_DEVICE_ID_INTEL_5400_FBD0 + channel;
  387. default:
  388. BUG();
  389. }
  390. }
  391. static unsigned long chipset_ids[] = {
  392. PCI_DEVICE_ID_INTEL_5000_ERR,
  393. PCI_DEVICE_ID_INTEL_5400_ERR,
  394. 0
  395. };
  396. static int __devinit i5k_amb_probe(struct platform_device *pdev)
  397. {
  398. struct i5k_amb_data *data;
  399. struct resource *reso;
  400. int i;
  401. int res = -ENODEV;
  402. data = kzalloc(sizeof(*data), GFP_KERNEL);
  403. if (!data)
  404. return -ENOMEM;
  405. /* Figure out where the AMB registers live */
  406. i = 0;
  407. do {
  408. res = i5k_find_amb_registers(data, chipset_ids[i]);
  409. i++;
  410. } while (res && chipset_ids[i]);
  411. if (res)
  412. goto err;
  413. /* Copy the DIMM presence map for the first two channels */
  414. res = i5k_channel_probe(&data->amb_present[0],
  415. i5k_channel_pci_id(data, 0));
  416. if (res)
  417. goto err;
  418. /* Copy the DIMM presence map for the optional second two channels */
  419. i5k_channel_probe(&data->amb_present[2],
  420. i5k_channel_pci_id(data, 1));
  421. /* Set up resource regions */
  422. reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
  423. if (!reso) {
  424. res = -EBUSY;
  425. goto err;
  426. }
  427. data->amb_mmio = ioremap_nocache(data->amb_base, data->amb_len);
  428. if (!data->amb_mmio) {
  429. res = -EBUSY;
  430. goto err_map_failed;
  431. }
  432. platform_set_drvdata(pdev, data);
  433. res = i5k_amb_hwmon_init(pdev);
  434. if (res)
  435. goto err_init_failed;
  436. return res;
  437. err_init_failed:
  438. iounmap(data->amb_mmio);
  439. platform_set_drvdata(pdev, NULL);
  440. err_map_failed:
  441. release_mem_region(data->amb_base, data->amb_len);
  442. err:
  443. kfree(data);
  444. return res;
  445. }
  446. static int __devexit i5k_amb_remove(struct platform_device *pdev)
  447. {
  448. int i;
  449. struct i5k_amb_data *data = platform_get_drvdata(pdev);
  450. hwmon_device_unregister(data->hwmon_dev);
  451. device_remove_file(&pdev->dev, &dev_attr_name);
  452. for (i = 0; i < data->num_attrs; i++)
  453. device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
  454. kfree(data->attrs);
  455. iounmap(data->amb_mmio);
  456. release_mem_region(data->amb_base, data->amb_len);
  457. platform_set_drvdata(pdev, NULL);
  458. kfree(data);
  459. return 0;
  460. }
  461. static struct platform_driver i5k_amb_driver = {
  462. .driver = {
  463. .owner = THIS_MODULE,
  464. .name = DRVNAME,
  465. },
  466. .probe = i5k_amb_probe,
  467. .remove = __devexit_p(i5k_amb_remove),
  468. };
  469. static int __init i5k_amb_init(void)
  470. {
  471. int res;
  472. res = platform_driver_register(&i5k_amb_driver);
  473. if (res)
  474. return res;
  475. res = i5k_amb_add();
  476. if (res)
  477. platform_driver_unregister(&i5k_amb_driver);
  478. return res;
  479. }
  480. static void __exit i5k_amb_exit(void)
  481. {
  482. platform_device_unregister(amb_pdev);
  483. platform_driver_unregister(&i5k_amb_driver);
  484. }
  485. MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
  486. MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
  487. MODULE_LICENSE("GPL");
  488. module_init(i5k_amb_init);
  489. module_exit(i5k_amb_exit);