i5k_amb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. };
  100. static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
  101. char *buf)
  102. {
  103. return sprintf(buf, "%s\n", DRVNAME);
  104. }
  105. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  106. static struct platform_device *amb_pdev;
  107. static u8 amb_read_byte(struct i5k_amb_data *data, unsigned long offset)
  108. {
  109. return ioread8(data->amb_mmio + offset);
  110. }
  111. static void amb_write_byte(struct i5k_amb_data *data, unsigned long offset,
  112. u8 val)
  113. {
  114. iowrite8(val, data->amb_mmio + offset);
  115. }
  116. static ssize_t show_amb_alarm(struct device *dev,
  117. struct device_attribute *devattr,
  118. char *buf)
  119. {
  120. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  121. struct i5k_amb_data *data = dev_get_drvdata(dev);
  122. if (!(amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x20) &&
  123. (amb_read_byte(data, amb_reg_temp_status(attr->index)) & 0x8))
  124. return sprintf(buf, "1\n");
  125. else
  126. return sprintf(buf, "0\n");
  127. }
  128. static ssize_t store_amb_min(struct device *dev,
  129. struct device_attribute *devattr,
  130. const char *buf,
  131. size_t count)
  132. {
  133. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  134. struct i5k_amb_data *data = dev_get_drvdata(dev);
  135. unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
  136. if (temp > 255)
  137. temp = 255;
  138. amb_write_byte(data, amb_reg_temp_min(attr->index), temp);
  139. return count;
  140. }
  141. static ssize_t store_amb_mid(struct device *dev,
  142. struct device_attribute *devattr,
  143. const char *buf,
  144. size_t count)
  145. {
  146. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  147. struct i5k_amb_data *data = dev_get_drvdata(dev);
  148. unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
  149. if (temp > 255)
  150. temp = 255;
  151. amb_write_byte(data, amb_reg_temp_mid(attr->index), temp);
  152. return count;
  153. }
  154. static ssize_t store_amb_max(struct device *dev,
  155. struct device_attribute *devattr,
  156. const char *buf,
  157. size_t count)
  158. {
  159. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  160. struct i5k_amb_data *data = dev_get_drvdata(dev);
  161. unsigned long temp = simple_strtoul(buf, NULL, 10) / 500;
  162. if (temp > 255)
  163. temp = 255;
  164. amb_write_byte(data, amb_reg_temp_max(attr->index), temp);
  165. return count;
  166. }
  167. static ssize_t show_amb_min(struct device *dev,
  168. struct device_attribute *devattr,
  169. char *buf)
  170. {
  171. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  172. struct i5k_amb_data *data = dev_get_drvdata(dev);
  173. return sprintf(buf, "%d\n",
  174. 500 * amb_read_byte(data, amb_reg_temp_min(attr->index)));
  175. }
  176. static ssize_t show_amb_mid(struct device *dev,
  177. struct device_attribute *devattr,
  178. char *buf)
  179. {
  180. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  181. struct i5k_amb_data *data = dev_get_drvdata(dev);
  182. return sprintf(buf, "%d\n",
  183. 500 * amb_read_byte(data, amb_reg_temp_mid(attr->index)));
  184. }
  185. static ssize_t show_amb_max(struct device *dev,
  186. struct device_attribute *devattr,
  187. char *buf)
  188. {
  189. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  190. struct i5k_amb_data *data = dev_get_drvdata(dev);
  191. return sprintf(buf, "%d\n",
  192. 500 * amb_read_byte(data, amb_reg_temp_max(attr->index)));
  193. }
  194. static ssize_t show_amb_temp(struct device *dev,
  195. struct device_attribute *devattr,
  196. char *buf)
  197. {
  198. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  199. struct i5k_amb_data *data = dev_get_drvdata(dev);
  200. return sprintf(buf, "%d\n",
  201. 500 * amb_read_byte(data, amb_reg_temp(attr->index)));
  202. }
  203. static int __devinit i5k_amb_hwmon_init(struct platform_device *pdev)
  204. {
  205. int i, j, k, d = 0;
  206. u16 c;
  207. int res = 0;
  208. int num_ambs = 0;
  209. struct i5k_amb_data *data = platform_get_drvdata(pdev);
  210. /* Count the number of AMBs found */
  211. /* ignore the high-order bit, see "Ugly hack" comment above */
  212. for (i = 0; i < MAX_MEM_CHANNELS; i++)
  213. num_ambs += hweight16(data->amb_present[i] & 0x7fff);
  214. /* Set up sysfs stuff */
  215. data->attrs = kzalloc(sizeof(*data->attrs) * num_ambs * KNOBS_PER_AMB,
  216. GFP_KERNEL);
  217. if (!data->attrs)
  218. return -ENOMEM;
  219. data->num_attrs = 0;
  220. for (i = 0; i < MAX_MEM_CHANNELS; i++) {
  221. c = data->amb_present[i];
  222. for (j = 0; j < REAL_MAX_AMBS_PER_CHANNEL; j++, c >>= 1) {
  223. struct i5k_device_attribute *iattr;
  224. k = amb_num_from_reg(i, j);
  225. if (!(c & 0x1))
  226. continue;
  227. d++;
  228. /* Temperature sysfs knob */
  229. iattr = data->attrs + data->num_attrs;
  230. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  231. "temp%d_input", d);
  232. iattr->s_attr.dev_attr.attr.name = iattr->name;
  233. iattr->s_attr.dev_attr.attr.mode = S_IRUGO;
  234. iattr->s_attr.dev_attr.show = show_amb_temp;
  235. iattr->s_attr.index = k;
  236. res = device_create_file(&pdev->dev,
  237. &iattr->s_attr.dev_attr);
  238. if (res)
  239. goto exit_remove;
  240. data->num_attrs++;
  241. /* Temperature min sysfs knob */
  242. iattr = data->attrs + data->num_attrs;
  243. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  244. "temp%d_min", d);
  245. iattr->s_attr.dev_attr.attr.name = iattr->name;
  246. iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  247. iattr->s_attr.dev_attr.show = show_amb_min;
  248. iattr->s_attr.dev_attr.store = store_amb_min;
  249. iattr->s_attr.index = k;
  250. res = device_create_file(&pdev->dev,
  251. &iattr->s_attr.dev_attr);
  252. if (res)
  253. goto exit_remove;
  254. data->num_attrs++;
  255. /* Temperature mid sysfs knob */
  256. iattr = data->attrs + data->num_attrs;
  257. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  258. "temp%d_mid", d);
  259. iattr->s_attr.dev_attr.attr.name = iattr->name;
  260. iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  261. iattr->s_attr.dev_attr.show = show_amb_mid;
  262. iattr->s_attr.dev_attr.store = store_amb_mid;
  263. iattr->s_attr.index = k;
  264. res = device_create_file(&pdev->dev,
  265. &iattr->s_attr.dev_attr);
  266. if (res)
  267. goto exit_remove;
  268. data->num_attrs++;
  269. /* Temperature max sysfs knob */
  270. iattr = data->attrs + data->num_attrs;
  271. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  272. "temp%d_max", d);
  273. iattr->s_attr.dev_attr.attr.name = iattr->name;
  274. iattr->s_attr.dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  275. iattr->s_attr.dev_attr.show = show_amb_max;
  276. iattr->s_attr.dev_attr.store = store_amb_max;
  277. iattr->s_attr.index = k;
  278. res = device_create_file(&pdev->dev,
  279. &iattr->s_attr.dev_attr);
  280. if (res)
  281. goto exit_remove;
  282. data->num_attrs++;
  283. /* Temperature alarm sysfs knob */
  284. iattr = data->attrs + data->num_attrs;
  285. snprintf(iattr->name, AMB_SYSFS_NAME_LEN,
  286. "temp%d_alarm", d);
  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_amb_alarm;
  290. iattr->s_attr.index = k;
  291. res = device_create_file(&pdev->dev,
  292. &iattr->s_attr.dev_attr);
  293. if (res)
  294. goto exit_remove;
  295. data->num_attrs++;
  296. }
  297. }
  298. res = device_create_file(&pdev->dev, &dev_attr_name);
  299. if (res)
  300. goto exit_remove;
  301. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  302. if (IS_ERR(data->hwmon_dev)) {
  303. res = PTR_ERR(data->hwmon_dev);
  304. goto exit_remove;
  305. }
  306. return res;
  307. exit_remove:
  308. device_remove_file(&pdev->dev, &dev_attr_name);
  309. for (i = 0; i < data->num_attrs; i++)
  310. device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
  311. kfree(data->attrs);
  312. return res;
  313. }
  314. static int __devinit i5k_amb_add(void)
  315. {
  316. int res = -ENODEV;
  317. /* only ever going to be one of these */
  318. amb_pdev = platform_device_alloc(DRVNAME, 0);
  319. if (!amb_pdev)
  320. return -ENOMEM;
  321. res = platform_device_add(amb_pdev);
  322. if (res)
  323. goto err;
  324. return 0;
  325. err:
  326. platform_device_put(amb_pdev);
  327. return res;
  328. }
  329. static int __devinit i5k_find_amb_registers(struct i5k_amb_data *data)
  330. {
  331. struct pci_dev *pcidev;
  332. u32 val32;
  333. int res = -ENODEV;
  334. /* Find AMB register memory space */
  335. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  336. PCI_DEVICE_ID_INTEL_5000_ERR,
  337. NULL);
  338. if (!pcidev)
  339. return -ENODEV;
  340. if (pci_read_config_dword(pcidev, I5K_REG_AMB_BASE_ADDR, &val32))
  341. goto out;
  342. data->amb_base = val32;
  343. if (pci_read_config_dword(pcidev, I5K_REG_AMB_LEN_ADDR, &val32))
  344. goto out;
  345. data->amb_len = val32;
  346. /* Is it big enough? */
  347. if (data->amb_len < AMB_CONFIG_SIZE * MAX_AMBS) {
  348. dev_err(&pcidev->dev, "AMB region too small!\n");
  349. goto out;
  350. }
  351. res = 0;
  352. out:
  353. pci_dev_put(pcidev);
  354. return res;
  355. }
  356. static int __devinit i5k_channel_probe(u16 *amb_present, unsigned long dev_id)
  357. {
  358. struct pci_dev *pcidev;
  359. u16 val16;
  360. int res = -ENODEV;
  361. /* Copy the DIMM presence map for these two channels */
  362. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, dev_id, NULL);
  363. if (!pcidev)
  364. return -ENODEV;
  365. if (pci_read_config_word(pcidev, I5K_REG_CHAN0_PRESENCE_ADDR, &val16))
  366. goto out;
  367. amb_present[0] = val16;
  368. if (pci_read_config_word(pcidev, I5K_REG_CHAN1_PRESENCE_ADDR, &val16))
  369. goto out;
  370. amb_present[1] = val16;
  371. res = 0;
  372. out:
  373. pci_dev_put(pcidev);
  374. return res;
  375. }
  376. static int __devinit i5k_amb_probe(struct platform_device *pdev)
  377. {
  378. struct i5k_amb_data *data;
  379. struct resource *reso;
  380. int res = -ENODEV;
  381. data = kzalloc(sizeof(*data), GFP_KERNEL);
  382. if (!data)
  383. return -ENOMEM;
  384. /* Figure out where the AMB registers live */
  385. res = i5k_find_amb_registers(data);
  386. if (res)
  387. goto err;
  388. /* Copy the DIMM presence map for the first two channels */
  389. res = i5k_channel_probe(&data->amb_present[0],
  390. PCI_DEVICE_ID_INTEL_5000_FBD0);
  391. if (res)
  392. goto err;
  393. /* Copy the DIMM presence map for the optional second two channels */
  394. i5k_channel_probe(&data->amb_present[2],
  395. PCI_DEVICE_ID_INTEL_5000_FBD1);
  396. /* Set up resource regions */
  397. reso = request_mem_region(data->amb_base, data->amb_len, DRVNAME);
  398. if (!reso) {
  399. res = -EBUSY;
  400. goto err;
  401. }
  402. data->amb_mmio = ioremap_nocache(data->amb_base, data->amb_len);
  403. if (!data->amb_mmio) {
  404. res = -EBUSY;
  405. goto err_map_failed;
  406. }
  407. platform_set_drvdata(pdev, data);
  408. res = i5k_amb_hwmon_init(pdev);
  409. if (res)
  410. goto err_init_failed;
  411. return res;
  412. err_init_failed:
  413. iounmap(data->amb_mmio);
  414. platform_set_drvdata(pdev, NULL);
  415. err_map_failed:
  416. release_mem_region(data->amb_base, data->amb_len);
  417. err:
  418. kfree(data);
  419. return res;
  420. }
  421. static int __devexit i5k_amb_remove(struct platform_device *pdev)
  422. {
  423. int i;
  424. struct i5k_amb_data *data = platform_get_drvdata(pdev);
  425. hwmon_device_unregister(data->hwmon_dev);
  426. device_remove_file(&pdev->dev, &dev_attr_name);
  427. for (i = 0; i < data->num_attrs; i++)
  428. device_remove_file(&pdev->dev, &data->attrs[i].s_attr.dev_attr);
  429. kfree(data->attrs);
  430. iounmap(data->amb_mmio);
  431. release_mem_region(data->amb_base, data->amb_len);
  432. platform_set_drvdata(pdev, NULL);
  433. kfree(data);
  434. return 0;
  435. }
  436. static struct platform_driver i5k_amb_driver = {
  437. .driver = {
  438. .owner = THIS_MODULE,
  439. .name = DRVNAME,
  440. },
  441. .probe = i5k_amb_probe,
  442. .remove = __devexit_p(i5k_amb_remove),
  443. };
  444. static int __init i5k_amb_init(void)
  445. {
  446. int res;
  447. res = platform_driver_register(&i5k_amb_driver);
  448. if (res)
  449. return res;
  450. res = i5k_amb_add();
  451. if (res)
  452. platform_driver_unregister(&i5k_amb_driver);
  453. return res;
  454. }
  455. static void __exit i5k_amb_exit(void)
  456. {
  457. platform_device_unregister(amb_pdev);
  458. platform_driver_unregister(&i5k_amb_driver);
  459. }
  460. MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
  461. MODULE_DESCRIPTION("Intel 5000 chipset FB-DIMM AMB temperature sensor");
  462. MODULE_LICENSE("GPL");
  463. module_init(i5k_amb_init);
  464. module_exit(i5k_amb_exit);