pt5161l.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/debugfs.h>
  3. #include <linux/delay.h>
  4. #include <linux/err.h>
  5. #include <linux/i2c.h>
  6. #include <linux/init.h>
  7. #include <linux/hwmon.h>
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. /* Aries current average temp ADC code CSR */
  11. #define ARIES_CURRENT_AVG_TEMP_ADC_CSR 0x42c
  12. /* Device Load check register */
  13. #define ARIES_CODE_LOAD_REG 0x605
  14. /* Value indicating FW was loaded properly, [3:1] = 3'b111 */
  15. #define ARIES_LOAD_CODE 0xe
  16. /* Main Micro Heartbeat register */
  17. #define ARIES_MM_HEARTBEAT_ADDR 0x923
  18. /* Reg offset to specify Address for MM assisted accesses */
  19. #define ARIES_MM_ASSIST_REG_ADDR_OFFSET 0xd99
  20. /* Reg offset to specify Command for MM assisted accesses */
  21. #define ARIES_MM_ASSIST_CMD_OFFSET 0xd9d
  22. /* Reg offset to MM SPARE 0 used specify Address[7:0] */
  23. #define ARIES_MM_ASSIST_SPARE_0_OFFSET 0xd9f
  24. /* Reg offset to MM SPARE 3 used specify Data Byte 0 */
  25. #define ARIES_MM_ASSIST_SPARE_3_OFFSET 0xda2
  26. /* Wide register reads */
  27. #define ARIES_MM_RD_WIDE_REG_2B 0x1d
  28. #define ARIES_MM_RD_WIDE_REG_3B 0x1e
  29. #define ARIES_MM_RD_WIDE_REG_4B 0x1f
  30. #define ARIES_MM_RD_WIDE_REG_5B 0x20
  31. /* Time delay between checking MM status of EEPROM write (microseconds) */
  32. #define ARIES_MM_STATUS_TIME 5000
  33. /* AL Main SRAM DMEM offset (A0) */
  34. #define AL_MAIN_SRAM_DMEM_OFFSET (64 * 1024)
  35. /* SRAM read command */
  36. #define AL_TG_RD_LOC_IND_SRAM 0x16
  37. /* Offset for main micro FW info */
  38. #define ARIES_MAIN_MICRO_FW_INFO (96 * 1024 - 128)
  39. /* FW Info (Major) offset location in struct */
  40. #define ARIES_MM_FW_VERSION_MAJOR 0
  41. /* FW Info (Minor) offset location in struct */
  42. #define ARIES_MM_FW_VERSION_MINOR 1
  43. /* FW Info (Build no.) offset location in struct */
  44. #define ARIES_MM_FW_VERSION_BUILD 2
  45. #define ARIES_TEMP_CAL_CODE_DEFAULT 84
  46. /* Struct defining FW version loaded on an Aries device */
  47. struct pt5161l_fw_ver {
  48. u8 major;
  49. u8 minor;
  50. u16 build;
  51. };
  52. /* Each client has this additional data */
  53. struct pt5161l_data {
  54. struct i2c_client *client;
  55. struct pt5161l_fw_ver fw_ver;
  56. struct mutex lock; /* for atomic I2C transactions */
  57. bool init_done;
  58. bool code_load_okay; /* indicate if code load reg value is expected */
  59. bool mm_heartbeat_okay; /* indicate if Main Micro heartbeat is good */
  60. bool mm_wide_reg_access; /* MM assisted wide register access */
  61. };
  62. /*
  63. * Write multiple data bytes to Aries over I2C
  64. */
  65. static int pt5161l_write_block_data(struct pt5161l_data *data, u32 address,
  66. u8 len, u8 *val)
  67. {
  68. struct i2c_client *client = data->client;
  69. int ret;
  70. u8 remain_len = len;
  71. u8 xfer_len, curr_len;
  72. u8 buf[16];
  73. u8 cmd = 0x0F; /* [7]:pec_en, [4:2]:func, [1]:start, [0]:end */
  74. u8 config = 0x40; /* [6]:cfg_type, [4:1]:burst_len, [0]:address bit16 */
  75. while (remain_len > 0) {
  76. if (remain_len > 4) {
  77. curr_len = 4;
  78. remain_len -= 4;
  79. } else {
  80. curr_len = remain_len;
  81. remain_len = 0;
  82. }
  83. buf[0] = config | (curr_len - 1) << 1 | ((address >> 16) & 0x1);
  84. buf[1] = (address >> 8) & 0xff;
  85. buf[2] = address & 0xff;
  86. memcpy(&buf[3], val, curr_len);
  87. xfer_len = 3 + curr_len;
  88. ret = i2c_smbus_write_block_data(client, cmd, xfer_len, buf);
  89. if (ret)
  90. return ret;
  91. val += curr_len;
  92. address += curr_len;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * Read multiple data bytes from Aries over I2C
  98. */
  99. static int pt5161l_read_block_data(struct pt5161l_data *data, u32 address,
  100. u8 len, u8 *val)
  101. {
  102. struct i2c_client *client = data->client;
  103. int ret, tries;
  104. u8 remain_len = len;
  105. u8 curr_len;
  106. u8 wbuf[16], rbuf[24];
  107. u8 cmd = 0x08; /* [7]:pec_en, [4:2]:func, [1]:start, [0]:end */
  108. u8 config = 0x00; /* [6]:cfg_type, [4:1]:burst_len, [0]:address bit16 */
  109. while (remain_len > 0) {
  110. if (remain_len > 16) {
  111. curr_len = 16;
  112. remain_len -= 16;
  113. } else {
  114. curr_len = remain_len;
  115. remain_len = 0;
  116. }
  117. wbuf[0] = config | (curr_len - 1) << 1 |
  118. ((address >> 16) & 0x1);
  119. wbuf[1] = (address >> 8) & 0xff;
  120. wbuf[2] = address & 0xff;
  121. for (tries = 0; tries < 3; tries++) {
  122. ret = i2c_smbus_write_block_data(client, (cmd | 0x2), 3,
  123. wbuf);
  124. if (ret)
  125. return ret;
  126. ret = i2c_smbus_read_block_data(client, (cmd | 0x1),
  127. rbuf);
  128. if (ret == curr_len)
  129. break;
  130. }
  131. if (tries >= 3)
  132. return ret;
  133. memcpy(val, rbuf, curr_len);
  134. val += curr_len;
  135. address += curr_len;
  136. }
  137. return 0;
  138. }
  139. static int pt5161l_read_wide_reg(struct pt5161l_data *data, u32 address,
  140. u8 width, u8 *val)
  141. {
  142. int ret, tries;
  143. u8 buf[8];
  144. u8 status;
  145. /*
  146. * Safely access wide registers using mailbox method to prevent
  147. * risking conflict with Aries firmware; otherwise fallback to
  148. * legacy, less secure method.
  149. */
  150. if (data->mm_wide_reg_access) {
  151. buf[0] = address & 0xff;
  152. buf[1] = (address >> 8) & 0xff;
  153. buf[2] = (address >> 16) & 0x1;
  154. ret = pt5161l_write_block_data(data,
  155. ARIES_MM_ASSIST_SPARE_0_OFFSET,
  156. 3, buf);
  157. if (ret)
  158. return ret;
  159. /* Set command based on width */
  160. switch (width) {
  161. case 2:
  162. buf[0] = ARIES_MM_RD_WIDE_REG_2B;
  163. break;
  164. case 3:
  165. buf[0] = ARIES_MM_RD_WIDE_REG_3B;
  166. break;
  167. case 4:
  168. buf[0] = ARIES_MM_RD_WIDE_REG_4B;
  169. break;
  170. case 5:
  171. buf[0] = ARIES_MM_RD_WIDE_REG_5B;
  172. break;
  173. default:
  174. return -EINVAL;
  175. }
  176. ret = pt5161l_write_block_data(data, ARIES_MM_ASSIST_CMD_OFFSET,
  177. 1, buf);
  178. if (ret)
  179. return ret;
  180. status = 0xff;
  181. for (tries = 0; tries < 100; tries++) {
  182. ret = pt5161l_read_block_data(data,
  183. ARIES_MM_ASSIST_CMD_OFFSET,
  184. 1, &status);
  185. if (ret)
  186. return ret;
  187. if (status == 0)
  188. break;
  189. usleep_range(ARIES_MM_STATUS_TIME,
  190. ARIES_MM_STATUS_TIME + 1000);
  191. }
  192. if (status != 0)
  193. return -ETIMEDOUT;
  194. ret = pt5161l_read_block_data(data,
  195. ARIES_MM_ASSIST_SPARE_3_OFFSET,
  196. width, val);
  197. if (ret)
  198. return ret;
  199. } else {
  200. return pt5161l_read_block_data(data, address, width, val);
  201. }
  202. return 0;
  203. }
  204. /*
  205. * Read multiple (up to eight) data bytes from micro SRAM over I2C
  206. */
  207. static int
  208. pt5161l_read_block_data_main_micro_indirect(struct pt5161l_data *data,
  209. u32 address, u8 len, u8 *val)
  210. {
  211. int ret, tries;
  212. u8 buf[8];
  213. u8 i, status;
  214. u32 uind_offs = ARIES_MM_ASSIST_REG_ADDR_OFFSET;
  215. u32 eeprom_base, eeprom_addr;
  216. /* No multi-byte indirect support here. Hence read a byte at a time */
  217. eeprom_base = address - AL_MAIN_SRAM_DMEM_OFFSET;
  218. for (i = 0; i < len; i++) {
  219. eeprom_addr = eeprom_base + i;
  220. buf[0] = eeprom_addr & 0xff;
  221. buf[1] = (eeprom_addr >> 8) & 0xff;
  222. buf[2] = (eeprom_addr >> 16) & 0xff;
  223. ret = pt5161l_write_block_data(data, uind_offs, 3, buf);
  224. if (ret)
  225. return ret;
  226. buf[0] = AL_TG_RD_LOC_IND_SRAM;
  227. ret = pt5161l_write_block_data(data, uind_offs + 4, 1, buf);
  228. if (ret)
  229. return ret;
  230. status = 0xff;
  231. for (tries = 0; tries < 255; tries++) {
  232. ret = pt5161l_read_block_data(data, uind_offs + 4, 1,
  233. &status);
  234. if (ret)
  235. return ret;
  236. if (status == 0)
  237. break;
  238. }
  239. if (status != 0)
  240. return -ETIMEDOUT;
  241. ret = pt5161l_read_block_data(data, uind_offs + 3, 1, buf);
  242. if (ret)
  243. return ret;
  244. val[i] = buf[0];
  245. }
  246. return 0;
  247. }
  248. /*
  249. * Check firmware load status
  250. */
  251. static int pt5161l_fw_load_check(struct pt5161l_data *data)
  252. {
  253. int ret;
  254. u8 buf[8];
  255. ret = pt5161l_read_block_data(data, ARIES_CODE_LOAD_REG, 1, buf);
  256. if (ret)
  257. return ret;
  258. if (buf[0] < ARIES_LOAD_CODE) {
  259. dev_dbg(&data->client->dev,
  260. "Code Load reg unexpected. Not all modules are loaded %x\n",
  261. buf[0]);
  262. data->code_load_okay = false;
  263. } else {
  264. data->code_load_okay = true;
  265. }
  266. return 0;
  267. }
  268. /*
  269. * Check main micro heartbeat
  270. */
  271. static int pt5161l_heartbeat_check(struct pt5161l_data *data)
  272. {
  273. int ret, tries;
  274. u8 buf[8];
  275. u8 heartbeat;
  276. bool hb_changed = false;
  277. ret = pt5161l_read_block_data(data, ARIES_MM_HEARTBEAT_ADDR, 1, buf);
  278. if (ret)
  279. return ret;
  280. heartbeat = buf[0];
  281. for (tries = 0; tries < 100; tries++) {
  282. ret = pt5161l_read_block_data(data, ARIES_MM_HEARTBEAT_ADDR, 1,
  283. buf);
  284. if (ret)
  285. return ret;
  286. if (buf[0] != heartbeat) {
  287. hb_changed = true;
  288. break;
  289. }
  290. }
  291. data->mm_heartbeat_okay = hb_changed;
  292. return 0;
  293. }
  294. /*
  295. * Check the status of firmware
  296. */
  297. static int pt5161l_fwsts_check(struct pt5161l_data *data)
  298. {
  299. int ret;
  300. u8 buf[8];
  301. u8 major = 0, minor = 0;
  302. u16 build = 0;
  303. ret = pt5161l_fw_load_check(data);
  304. if (ret)
  305. return ret;
  306. ret = pt5161l_heartbeat_check(data);
  307. if (ret)
  308. return ret;
  309. if (data->code_load_okay && data->mm_heartbeat_okay) {
  310. ret = pt5161l_read_block_data_main_micro_indirect(data, ARIES_MAIN_MICRO_FW_INFO +
  311. ARIES_MM_FW_VERSION_MAJOR,
  312. 1, &major);
  313. if (ret)
  314. return ret;
  315. ret = pt5161l_read_block_data_main_micro_indirect(data, ARIES_MAIN_MICRO_FW_INFO +
  316. ARIES_MM_FW_VERSION_MINOR,
  317. 1, &minor);
  318. if (ret)
  319. return ret;
  320. ret = pt5161l_read_block_data_main_micro_indirect(data, ARIES_MAIN_MICRO_FW_INFO +
  321. ARIES_MM_FW_VERSION_BUILD,
  322. 2, buf);
  323. if (ret)
  324. return ret;
  325. build = buf[1] << 8 | buf[0];
  326. }
  327. data->fw_ver.major = major;
  328. data->fw_ver.minor = minor;
  329. data->fw_ver.build = build;
  330. return 0;
  331. }
  332. static int pt5161l_fw_is_at_least(struct pt5161l_data *data, u8 major, u8 minor,
  333. u16 build)
  334. {
  335. u32 ver = major << 24 | minor << 16 | build;
  336. u32 curr_ver = data->fw_ver.major << 24 | data->fw_ver.minor << 16 |
  337. data->fw_ver.build;
  338. if (curr_ver >= ver)
  339. return true;
  340. return false;
  341. }
  342. static int pt5161l_init_dev(struct pt5161l_data *data)
  343. {
  344. int ret;
  345. mutex_lock(&data->lock);
  346. ret = pt5161l_fwsts_check(data);
  347. mutex_unlock(&data->lock);
  348. if (ret)
  349. return ret;
  350. /* Firmware 2.2.0 enables safe access to wide registers */
  351. if (pt5161l_fw_is_at_least(data, 2, 2, 0))
  352. data->mm_wide_reg_access = true;
  353. data->init_done = true;
  354. return 0;
  355. }
  356. static int pt5161l_read(struct device *dev, enum hwmon_sensor_types type,
  357. u32 attr, int channel, long *val)
  358. {
  359. struct pt5161l_data *data = dev_get_drvdata(dev);
  360. int ret;
  361. u8 buf[8];
  362. u32 adc_code;
  363. switch (attr) {
  364. case hwmon_temp_input:
  365. if (!data->init_done) {
  366. ret = pt5161l_init_dev(data);
  367. if (ret)
  368. return ret;
  369. }
  370. mutex_lock(&data->lock);
  371. ret = pt5161l_read_wide_reg(data,
  372. ARIES_CURRENT_AVG_TEMP_ADC_CSR, 4,
  373. buf);
  374. mutex_unlock(&data->lock);
  375. if (ret) {
  376. dev_dbg(dev, "Read adc_code failed %d\n", ret);
  377. return ret;
  378. }
  379. adc_code = buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
  380. if (adc_code == 0 || adc_code >= 0x3ff) {
  381. dev_dbg(dev, "Invalid adc_code %x\n", adc_code);
  382. return -EIO;
  383. }
  384. *val = 110000 +
  385. ((adc_code - (ARIES_TEMP_CAL_CODE_DEFAULT + 250)) *
  386. -320);
  387. break;
  388. default:
  389. return -EOPNOTSUPP;
  390. }
  391. return 0;
  392. }
  393. static umode_t pt5161l_is_visible(const void *data,
  394. enum hwmon_sensor_types type, u32 attr,
  395. int channel)
  396. {
  397. switch (attr) {
  398. case hwmon_temp_input:
  399. return 0444;
  400. default:
  401. break;
  402. }
  403. return 0;
  404. }
  405. static const struct hwmon_channel_info *pt5161l_info[] = {
  406. HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
  407. NULL
  408. };
  409. static const struct hwmon_ops pt5161l_hwmon_ops = {
  410. .is_visible = pt5161l_is_visible,
  411. .read = pt5161l_read,
  412. };
  413. static const struct hwmon_chip_info pt5161l_chip_info = {
  414. .ops = &pt5161l_hwmon_ops,
  415. .info = pt5161l_info,
  416. };
  417. static ssize_t pt5161l_debugfs_read_fw_ver(struct file *file, char __user *buf,
  418. size_t count, loff_t *ppos)
  419. {
  420. struct pt5161l_data *data = file->private_data;
  421. int ret;
  422. char ver[32];
  423. mutex_lock(&data->lock);
  424. ret = pt5161l_fwsts_check(data);
  425. mutex_unlock(&data->lock);
  426. if (ret)
  427. return ret;
  428. ret = snprintf(ver, sizeof(ver), "%u.%u.%u\n", data->fw_ver.major,
  429. data->fw_ver.minor, data->fw_ver.build);
  430. return simple_read_from_buffer(buf, count, ppos, ver, ret);
  431. }
  432. static const struct file_operations pt5161l_debugfs_ops_fw_ver = {
  433. .read = pt5161l_debugfs_read_fw_ver,
  434. .open = simple_open,
  435. };
  436. static ssize_t pt5161l_debugfs_read_fw_load_sts(struct file *file,
  437. char __user *buf, size_t count,
  438. loff_t *ppos)
  439. {
  440. struct pt5161l_data *data = file->private_data;
  441. int ret;
  442. bool status = false;
  443. char health[16];
  444. mutex_lock(&data->lock);
  445. ret = pt5161l_fw_load_check(data);
  446. mutex_unlock(&data->lock);
  447. if (ret == 0)
  448. status = data->code_load_okay;
  449. ret = snprintf(health, sizeof(health), "%s\n",
  450. status ? "normal" : "abnormal");
  451. return simple_read_from_buffer(buf, count, ppos, health, ret);
  452. }
  453. static const struct file_operations pt5161l_debugfs_ops_fw_load_sts = {
  454. .read = pt5161l_debugfs_read_fw_load_sts,
  455. .open = simple_open,
  456. };
  457. static ssize_t pt5161l_debugfs_read_hb_sts(struct file *file, char __user *buf,
  458. size_t count, loff_t *ppos)
  459. {
  460. struct pt5161l_data *data = file->private_data;
  461. int ret;
  462. bool status = false;
  463. char health[16];
  464. mutex_lock(&data->lock);
  465. ret = pt5161l_heartbeat_check(data);
  466. mutex_unlock(&data->lock);
  467. if (ret == 0)
  468. status = data->mm_heartbeat_okay;
  469. ret = snprintf(health, sizeof(health), "%s\n",
  470. status ? "normal" : "abnormal");
  471. return simple_read_from_buffer(buf, count, ppos, health, ret);
  472. }
  473. static const struct file_operations pt5161l_debugfs_ops_hb_sts = {
  474. .read = pt5161l_debugfs_read_hb_sts,
  475. .open = simple_open,
  476. };
  477. static void pt5161l_init_debugfs(struct i2c_client *client, struct pt5161l_data *data)
  478. {
  479. debugfs_create_file("fw_ver", 0444, client->debugfs, data,
  480. &pt5161l_debugfs_ops_fw_ver);
  481. debugfs_create_file("fw_load_status", 0444, client->debugfs, data,
  482. &pt5161l_debugfs_ops_fw_load_sts);
  483. debugfs_create_file("heartbeat_status", 0444, client->debugfs, data,
  484. &pt5161l_debugfs_ops_hb_sts);
  485. }
  486. static int pt5161l_probe(struct i2c_client *client)
  487. {
  488. struct device *dev = &client->dev;
  489. struct device *hwmon_dev;
  490. struct pt5161l_data *data;
  491. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  492. if (!data)
  493. return -ENOMEM;
  494. data->client = client;
  495. mutex_init(&data->lock);
  496. pt5161l_init_dev(data);
  497. dev_set_drvdata(dev, data);
  498. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  499. data,
  500. &pt5161l_chip_info,
  501. NULL);
  502. if (IS_ERR(hwmon_dev))
  503. return PTR_ERR(hwmon_dev);
  504. pt5161l_init_debugfs(client, data);
  505. return 0;
  506. }
  507. static const struct of_device_id __maybe_unused pt5161l_of_match[] = {
  508. { .compatible = "asteralabs,pt5161l" },
  509. {},
  510. };
  511. MODULE_DEVICE_TABLE(of, pt5161l_of_match);
  512. static const struct acpi_device_id __maybe_unused pt5161l_acpi_match[] = {
  513. { "PT5161L", 0 },
  514. {},
  515. };
  516. MODULE_DEVICE_TABLE(acpi, pt5161l_acpi_match);
  517. static const struct i2c_device_id pt5161l_id[] = {
  518. { "pt5161l" },
  519. {}
  520. };
  521. MODULE_DEVICE_TABLE(i2c, pt5161l_id);
  522. static struct i2c_driver pt5161l_driver = {
  523. .class = I2C_CLASS_HWMON,
  524. .driver = {
  525. .name = "pt5161l",
  526. .of_match_table = of_match_ptr(pt5161l_of_match),
  527. .acpi_match_table = ACPI_PTR(pt5161l_acpi_match),
  528. },
  529. .probe = pt5161l_probe,
  530. .id_table = pt5161l_id,
  531. };
  532. module_i2c_driver(pt5161l_driver);
  533. MODULE_AUTHOR("Cosmo Chou <cosmo.chou@quantatw.com>");
  534. MODULE_DESCRIPTION("Hwmon driver for Astera Labs Aries PCIe retimer");
  535. MODULE_LICENSE("GPL");