vid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright 2014 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <i2c.h>
  9. #include <asm/io.h>
  10. #ifdef CONFIG_FSL_LSCH2
  11. #include <asm/arch/immap_lsch2.h>
  12. #elif defined(CONFIG_FSL_LSCH3)
  13. #include <asm/arch/immap_lsch3.h>
  14. #else
  15. #include <asm/immap_85xx.h>
  16. #endif
  17. #include "vid.h"
  18. DECLARE_GLOBAL_DATA_PTR;
  19. int __weak i2c_multiplexer_select_vid_channel(u8 channel)
  20. {
  21. return 0;
  22. }
  23. /*
  24. * Compensate for a board specific voltage drop between regulator and SoC
  25. * return a value in mV
  26. */
  27. int __weak board_vdd_drop_compensation(void)
  28. {
  29. return 0;
  30. }
  31. /*
  32. * Get the i2c address configuration for the IR regulator chip
  33. *
  34. * There are some variance in the RDB HW regarding the I2C address configuration
  35. * for the IR regulator chip, which is likely a problem of external resistor
  36. * accuracy. So we just check each address in a hopefully non-intrusive mode
  37. * and use the first one that seems to work
  38. *
  39. * The IR chip can show up under the following addresses:
  40. * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
  41. * 0x09 (Verified on T1040RDB-PA)
  42. * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
  43. */
  44. static int find_ir_chip_on_i2c(void)
  45. {
  46. int i2caddress;
  47. int ret;
  48. u8 byte;
  49. int i;
  50. const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
  51. /* Check all the address */
  52. for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
  53. i2caddress = ir_i2c_addr[i];
  54. ret = i2c_read(i2caddress,
  55. IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
  56. sizeof(byte));
  57. if ((ret >= 0) && (byte == IR36021_MFR_ID))
  58. return i2caddress;
  59. }
  60. return -1;
  61. }
  62. /* Maximum loop count waiting for new voltage to take effect */
  63. #define MAX_LOOP_WAIT_NEW_VOL 100
  64. /* Maximum loop count waiting for the voltage to be stable */
  65. #define MAX_LOOP_WAIT_VOL_STABLE 100
  66. /*
  67. * read_voltage from sensor on I2C bus
  68. * We use average of 4 readings, waiting for WAIT_FOR_ADC before
  69. * another reading
  70. */
  71. #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
  72. /* If an INA220 chip is available, we can use it to read back the voltage
  73. * as it may have a higher accuracy than the IR chip for the same purpose
  74. */
  75. #ifdef CONFIG_VOL_MONITOR_INA220
  76. #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
  77. #define ADC_MIN_ACCURACY 4
  78. #else
  79. #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
  80. #define ADC_MIN_ACCURACY 4
  81. #endif
  82. #ifdef CONFIG_VOL_MONITOR_INA220
  83. static int read_voltage_from_INA220(int i2caddress)
  84. {
  85. int i, ret, voltage_read = 0;
  86. u16 vol_mon;
  87. u8 buf[2];
  88. for (i = 0; i < NUM_READINGS; i++) {
  89. ret = i2c_read(I2C_VOL_MONITOR_ADDR,
  90. I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
  91. (void *)&buf, 2);
  92. if (ret) {
  93. printf("VID: failed to read core voltage\n");
  94. return ret;
  95. }
  96. vol_mon = (buf[0] << 8) | buf[1];
  97. if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
  98. printf("VID: Core voltage sensor error\n");
  99. return -1;
  100. }
  101. debug("VID: bus voltage reads 0x%04x\n", vol_mon);
  102. /* LSB = 4mv */
  103. voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
  104. udelay(WAIT_FOR_ADC);
  105. }
  106. /* calculate the average */
  107. voltage_read /= NUM_READINGS;
  108. return voltage_read;
  109. }
  110. #endif
  111. /* read voltage from IR */
  112. #ifdef CONFIG_VOL_MONITOR_IR36021_READ
  113. static int read_voltage_from_IR(int i2caddress)
  114. {
  115. int i, ret, voltage_read = 0;
  116. u16 vol_mon;
  117. u8 buf;
  118. for (i = 0; i < NUM_READINGS; i++) {
  119. ret = i2c_read(i2caddress,
  120. IR36021_LOOP1_VOUT_OFFSET,
  121. 1, (void *)&buf, 1);
  122. if (ret) {
  123. printf("VID: failed to read vcpu\n");
  124. return ret;
  125. }
  126. vol_mon = buf;
  127. if (!vol_mon) {
  128. printf("VID: Core voltage sensor error\n");
  129. return -1;
  130. }
  131. debug("VID: bus voltage reads 0x%02x\n", vol_mon);
  132. /* Resolution is 1/128V. We scale up here to get 1/128mV
  133. * and divide at the end
  134. */
  135. voltage_read += vol_mon * 1000;
  136. udelay(WAIT_FOR_ADC);
  137. }
  138. /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
  139. voltage_read = DIV_ROUND_UP(voltage_read, 128);
  140. /* calculate the average */
  141. voltage_read /= NUM_READINGS;
  142. /* Compensate for a board specific voltage drop between regulator and
  143. * SoC before converting into an IR VID value
  144. */
  145. voltage_read -= board_vdd_drop_compensation();
  146. return voltage_read;
  147. }
  148. #endif
  149. static int read_voltage(int i2caddress)
  150. {
  151. int voltage_read;
  152. #ifdef CONFIG_VOL_MONITOR_INA220
  153. voltage_read = read_voltage_from_INA220(i2caddress);
  154. #elif defined CONFIG_VOL_MONITOR_IR36021_READ
  155. voltage_read = read_voltage_from_IR(i2caddress);
  156. #else
  157. return -1;
  158. #endif
  159. return voltage_read;
  160. }
  161. /*
  162. * We need to calculate how long before the voltage stops to drop
  163. * or increase. It returns with the loop count. Each loop takes
  164. * several readings (WAIT_FOR_ADC)
  165. */
  166. static int wait_for_new_voltage(int vdd, int i2caddress)
  167. {
  168. int timeout, vdd_current;
  169. vdd_current = read_voltage(i2caddress);
  170. /* wait until voltage starts to reach the target. Voltage slew
  171. * rates by typical regulators will always lead to stable readings
  172. * within each fairly long ADC interval in comparison to the
  173. * intended voltage delta change until the target voltage is
  174. * reached. The fairly small voltage delta change to any target
  175. * VID voltage also means that this function will always complete
  176. * within few iterations. If the timeout was ever reached, it would
  177. * point to a serious failure in the regulator system.
  178. */
  179. for (timeout = 0;
  180. abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
  181. timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
  182. vdd_current = read_voltage(i2caddress);
  183. }
  184. if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
  185. printf("VID: Voltage adjustment timeout\n");
  186. return -1;
  187. }
  188. return timeout;
  189. }
  190. /*
  191. * this function keeps reading the voltage until it is stable or until the
  192. * timeout expires
  193. */
  194. static int wait_for_voltage_stable(int i2caddress)
  195. {
  196. int timeout, vdd_current, vdd;
  197. vdd = read_voltage(i2caddress);
  198. udelay(NUM_READINGS * WAIT_FOR_ADC);
  199. /* wait until voltage is stable */
  200. vdd_current = read_voltage(i2caddress);
  201. /* The maximum timeout is
  202. * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
  203. */
  204. for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
  205. abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
  206. timeout > 0; timeout--) {
  207. vdd = vdd_current;
  208. udelay(NUM_READINGS * WAIT_FOR_ADC);
  209. vdd_current = read_voltage(i2caddress);
  210. }
  211. if (timeout == 0)
  212. return -1;
  213. return vdd_current;
  214. }
  215. #ifdef CONFIG_VOL_MONITOR_IR36021_SET
  216. /* Set the voltage to the IR chip */
  217. static int set_voltage_to_IR(int i2caddress, int vdd)
  218. {
  219. int wait, vdd_last;
  220. int ret;
  221. u8 vid;
  222. /* Compensate for a board specific voltage drop between regulator and
  223. * SoC before converting into an IR VID value
  224. */
  225. vdd += board_vdd_drop_compensation();
  226. #ifdef CONFIG_FSL_LSCH2
  227. vid = DIV_ROUND_UP(vdd - 265, 5);
  228. #else
  229. vid = DIV_ROUND_UP(vdd - 245, 5);
  230. #endif
  231. ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
  232. 1, (void *)&vid, sizeof(vid));
  233. if (ret) {
  234. printf("VID: failed to write VID\n");
  235. return -1;
  236. }
  237. wait = wait_for_new_voltage(vdd, i2caddress);
  238. if (wait < 0)
  239. return -1;
  240. debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
  241. vdd_last = wait_for_voltage_stable(i2caddress);
  242. if (vdd_last < 0)
  243. return -1;
  244. debug("VID: Current voltage is %d mV\n", vdd_last);
  245. return vdd_last;
  246. }
  247. #endif
  248. static int set_voltage(int i2caddress, int vdd)
  249. {
  250. int vdd_last = -1;
  251. #ifdef CONFIG_VOL_MONITOR_IR36021_SET
  252. vdd_last = set_voltage_to_IR(i2caddress, vdd);
  253. #else
  254. #error Specific voltage monitor must be defined
  255. #endif
  256. return vdd_last;
  257. }
  258. int adjust_vdd(ulong vdd_override)
  259. {
  260. int re_enable = disable_interrupts();
  261. #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3)
  262. struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  263. #else
  264. ccsr_gur_t __iomem *gur =
  265. (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  266. #endif
  267. u32 fusesr;
  268. u8 vid, buf;
  269. int vdd_target, vdd_current, vdd_last;
  270. int ret, i2caddress;
  271. unsigned long vdd_string_override;
  272. char *vdd_string;
  273. static const uint16_t vdd[32] = {
  274. 0, /* unused */
  275. 9875, /* 0.9875V */
  276. 9750,
  277. 9625,
  278. 9500,
  279. 9375,
  280. 9250,
  281. 9125,
  282. 9000,
  283. 8875,
  284. 8750,
  285. 8625,
  286. 8500,
  287. 8375,
  288. 8250,
  289. 8125,
  290. 10000, /* 1.0000V */
  291. 10125,
  292. 10250,
  293. 10375,
  294. 10500,
  295. 10625,
  296. 10750,
  297. 10875,
  298. 11000,
  299. 0, /* reserved */
  300. };
  301. struct vdd_drive {
  302. u8 vid;
  303. unsigned voltage;
  304. };
  305. ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
  306. if (ret) {
  307. debug("VID: I2C failed to switch channel\n");
  308. ret = -1;
  309. goto exit;
  310. }
  311. ret = find_ir_chip_on_i2c();
  312. if (ret < 0) {
  313. printf("VID: Could not find voltage regulator on I2C.\n");
  314. ret = -1;
  315. goto exit;
  316. } else {
  317. i2caddress = ret;
  318. debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
  319. }
  320. /* check IR chip work on Intel mode*/
  321. ret = i2c_read(i2caddress,
  322. IR36021_INTEL_MODE_OOFSET,
  323. 1, (void *)&buf, 1);
  324. if (ret) {
  325. printf("VID: failed to read IR chip mode.\n");
  326. ret = -1;
  327. goto exit;
  328. }
  329. if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
  330. printf("VID: IR Chip is not used in Intel mode.\n");
  331. ret = -1;
  332. goto exit;
  333. }
  334. /* get the voltage ID from fuse status register */
  335. #ifdef CONFIG_FSL_LSCH3
  336. fusesr = in_le32(&gur->dcfg_fusesr);
  337. #else
  338. fusesr = in_be32(&gur->dcfg_fusesr);
  339. #endif
  340. /*
  341. * VID is used according to the table below
  342. * ---------------------------------------
  343. * | DA_V |
  344. * |-------------------------------------|
  345. * | 5b00000 | 5b00001-5b11110 | 5b11111 |
  346. * ---------------+---------+-----------------+---------|
  347. * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
  348. * | A |----------+---------+-----------------+---------|
  349. * | _ | 5b00001 |VID = | VID = |VID = |
  350. * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
  351. * | _ | 5b11110 | | | |
  352. * | A |----------+---------+-----------------+---------|
  353. * | L | 5b11111 | No VID | VID = DA_V | NO VID |
  354. * | T | | | | |
  355. * ------------------------------------------------------
  356. */
  357. #ifdef CONFIG_FSL_LSCH2
  358. vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
  359. FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
  360. if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
  361. vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
  362. FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
  363. }
  364. #elif defined(CONFIG_FSL_LSCH3)
  365. vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
  366. FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
  367. if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
  368. vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
  369. FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
  370. }
  371. #else
  372. vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
  373. FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
  374. if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
  375. vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
  376. FSL_CORENET_DCFG_FUSESR_VID_MASK;
  377. }
  378. #endif
  379. vdd_target = vdd[vid];
  380. /* check override variable for overriding VDD */
  381. vdd_string = getenv(CONFIG_VID_FLS_ENV);
  382. if (vdd_override == 0 && vdd_string &&
  383. !strict_strtoul(vdd_string, 10, &vdd_string_override))
  384. vdd_override = vdd_string_override;
  385. if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
  386. vdd_target = vdd_override * 10; /* convert to 1/10 mV */
  387. debug("VDD override is %lu\n", vdd_override);
  388. } else if (vdd_override != 0) {
  389. printf("Invalid value.\n");
  390. }
  391. if (vdd_target == 0) {
  392. debug("VID: VID not used\n");
  393. ret = 0;
  394. goto exit;
  395. } else {
  396. /* divide and round up by 10 to get a value in mV */
  397. vdd_target = DIV_ROUND_UP(vdd_target, 10);
  398. debug("VID: vid = %d mV\n", vdd_target);
  399. }
  400. /*
  401. * Read voltage monitor to check real voltage.
  402. */
  403. vdd_last = read_voltage(i2caddress);
  404. if (vdd_last < 0) {
  405. printf("VID: Couldn't read sensor abort VID adjustment\n");
  406. ret = -1;
  407. goto exit;
  408. }
  409. vdd_current = vdd_last;
  410. debug("VID: Core voltage is currently at %d mV\n", vdd_last);
  411. /*
  412. * Adjust voltage to at or one step above target.
  413. * As measurements are less precise than setting the values
  414. * we may run through dummy steps that cancel each other
  415. * when stepping up and then down.
  416. */
  417. while (vdd_last > 0 &&
  418. vdd_last < vdd_target) {
  419. vdd_current += IR_VDD_STEP_UP;
  420. vdd_last = set_voltage(i2caddress, vdd_current);
  421. }
  422. while (vdd_last > 0 &&
  423. vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
  424. vdd_current -= IR_VDD_STEP_DOWN;
  425. vdd_last = set_voltage(i2caddress, vdd_current);
  426. }
  427. if (vdd_last > 0)
  428. printf("VID: Core voltage after adjustment is at %d mV\n",
  429. vdd_last);
  430. else
  431. ret = -1;
  432. exit:
  433. if (re_enable)
  434. enable_interrupts();
  435. i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
  436. return ret;
  437. }
  438. static int print_vdd(void)
  439. {
  440. int vdd_last, ret, i2caddress;
  441. ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
  442. if (ret) {
  443. debug("VID : I2c failed to switch channel\n");
  444. return -1;
  445. }
  446. ret = find_ir_chip_on_i2c();
  447. if (ret < 0) {
  448. printf("VID: Could not find voltage regulator on I2C.\n");
  449. goto exit;
  450. } else {
  451. i2caddress = ret;
  452. debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
  453. }
  454. /*
  455. * Read voltage monitor to check real voltage.
  456. */
  457. vdd_last = read_voltage(i2caddress);
  458. if (vdd_last < 0) {
  459. printf("VID: Couldn't read sensor abort VID adjustment\n");
  460. goto exit;
  461. }
  462. printf("VID: Core voltage is at %d mV\n", vdd_last);
  463. exit:
  464. i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
  465. return ret < 0 ? -1 : 0;
  466. }
  467. static int do_vdd_override(cmd_tbl_t *cmdtp,
  468. int flag, int argc,
  469. char * const argv[])
  470. {
  471. ulong override;
  472. if (argc < 2)
  473. return CMD_RET_USAGE;
  474. if (!strict_strtoul(argv[1], 10, &override))
  475. adjust_vdd(override); /* the value is checked by callee */
  476. else
  477. return CMD_RET_USAGE;
  478. return 0;
  479. }
  480. static int do_vdd_read(cmd_tbl_t *cmdtp,
  481. int flag, int argc,
  482. char * const argv[])
  483. {
  484. if (argc < 1)
  485. return CMD_RET_USAGE;
  486. print_vdd();
  487. return 0;
  488. }
  489. U_BOOT_CMD(
  490. vdd_override, 2, 0, do_vdd_override,
  491. "override VDD",
  492. " - override with the voltage specified in mV, eg. 1050"
  493. );
  494. U_BOOT_CMD(
  495. vdd_read, 1, 0, do_vdd_read,
  496. "read VDD",
  497. " - Read the voltage specified in mV"
  498. )