vid.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. #ifdef CONFIG_FSL_LSCH3
  259. int adjust_vdd(ulong vdd_override)
  260. {
  261. int re_enable = disable_interrupts();
  262. struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  263. u32 fusesr;
  264. u8 vid, buf;
  265. int vdd_target, vdd_current, vdd_last;
  266. int ret, i2caddress;
  267. unsigned long vdd_string_override;
  268. char *vdd_string;
  269. static const uint16_t vdd[32] = {
  270. 10500,
  271. 0, /* reserved */
  272. 9750,
  273. 0, /* reserved */
  274. 9500,
  275. 0, /* reserved */
  276. 0, /* reserved */
  277. 0, /* reserved */
  278. 0, /* reserved */
  279. 0, /* reserved */
  280. 0, /* reserved */
  281. 0, /* reserved */
  282. 0, /* reserved */
  283. 0, /* reserved */
  284. 0, /* reserved */
  285. 0, /* reserved */
  286. 10000, /* 1.0000V */
  287. 0, /* reserved */
  288. 10250,
  289. 0, /* reserved */
  290. 10500,
  291. 0, /* reserved */
  292. 0, /* reserved */
  293. 0, /* reserved */
  294. 0, /* reserved */
  295. 0, /* reserved */
  296. 0, /* reserved */
  297. 0, /* reserved */
  298. 0, /* reserved */
  299. 0, /* reserved */
  300. 0, /* reserved */
  301. 0, /* reserved */
  302. };
  303. struct vdd_drive {
  304. u8 vid;
  305. unsigned voltage;
  306. };
  307. ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
  308. if (ret) {
  309. debug("VID: I2C failed to switch channel\n");
  310. ret = -1;
  311. goto exit;
  312. }
  313. ret = find_ir_chip_on_i2c();
  314. if (ret < 0) {
  315. printf("VID: Could not find voltage regulator on I2C.\n");
  316. ret = -1;
  317. goto exit;
  318. } else {
  319. i2caddress = ret;
  320. debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
  321. }
  322. /* check IR chip work on Intel mode*/
  323. ret = i2c_read(i2caddress,
  324. IR36021_INTEL_MODE_OOFSET,
  325. 1, (void *)&buf, 1);
  326. if (ret) {
  327. printf("VID: failed to read IR chip mode.\n");
  328. ret = -1;
  329. goto exit;
  330. }
  331. if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
  332. printf("VID: IR Chip is not used in Intel mode.\n");
  333. ret = -1;
  334. goto exit;
  335. }
  336. /* get the voltage ID from fuse status register */
  337. fusesr = in_le32(&gur->dcfg_fusesr);
  338. vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
  339. FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
  340. if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
  341. vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
  342. FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
  343. }
  344. vdd_target = vdd[vid];
  345. /* check override variable for overriding VDD */
  346. vdd_string = env_get(CONFIG_VID_FLS_ENV);
  347. if (vdd_override == 0 && vdd_string &&
  348. !strict_strtoul(vdd_string, 10, &vdd_string_override))
  349. vdd_override = vdd_string_override;
  350. if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
  351. vdd_target = vdd_override * 10; /* convert to 1/10 mV */
  352. debug("VDD override is %lu\n", vdd_override);
  353. } else if (vdd_override != 0) {
  354. printf("Invalid value.\n");
  355. }
  356. /* divide and round up by 10 to get a value in mV */
  357. vdd_target = DIV_ROUND_UP(vdd_target, 10);
  358. if (vdd_target == 0) {
  359. debug("VID: VID not used\n");
  360. ret = 0;
  361. goto exit;
  362. } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
  363. /* Check vdd_target is in valid range */
  364. printf("VID: Target VID %d mV is not in range.\n",
  365. vdd_target);
  366. ret = -1;
  367. goto exit;
  368. } else {
  369. debug("VID: vid = %d mV\n", vdd_target);
  370. }
  371. /*
  372. * Read voltage monitor to check real voltage.
  373. */
  374. vdd_last = read_voltage(i2caddress);
  375. if (vdd_last < 0) {
  376. printf("VID: Couldn't read sensor abort VID adjustment\n");
  377. ret = -1;
  378. goto exit;
  379. }
  380. vdd_current = vdd_last;
  381. debug("VID: Core voltage is currently at %d mV\n", vdd_last);
  382. /*
  383. * Adjust voltage to at or one step above target.
  384. * As measurements are less precise than setting the values
  385. * we may run through dummy steps that cancel each other
  386. * when stepping up and then down.
  387. */
  388. while (vdd_last > 0 &&
  389. vdd_last < vdd_target) {
  390. vdd_current += IR_VDD_STEP_UP;
  391. vdd_last = set_voltage(i2caddress, vdd_current);
  392. }
  393. while (vdd_last > 0 &&
  394. vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
  395. vdd_current -= IR_VDD_STEP_DOWN;
  396. vdd_last = set_voltage(i2caddress, vdd_current);
  397. }
  398. if (vdd_last > 0)
  399. printf("VID: Core voltage after adjustment is at %d mV\n",
  400. vdd_last);
  401. else
  402. ret = -1;
  403. exit:
  404. if (re_enable)
  405. enable_interrupts();
  406. i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
  407. return ret;
  408. }
  409. #else /* !CONFIG_FSL_LSCH3 */
  410. int adjust_vdd(ulong vdd_override)
  411. {
  412. int re_enable = disable_interrupts();
  413. #if defined(CONFIG_FSL_LSCH2)
  414. struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  415. #else
  416. ccsr_gur_t __iomem *gur =
  417. (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  418. #endif
  419. u32 fusesr;
  420. u8 vid, buf;
  421. int vdd_target, vdd_current, vdd_last;
  422. int ret, i2caddress;
  423. unsigned long vdd_string_override;
  424. char *vdd_string;
  425. static const uint16_t vdd[32] = {
  426. 0, /* unused */
  427. 9875, /* 0.9875V */
  428. 9750,
  429. 9625,
  430. 9500,
  431. 9375,
  432. 9250,
  433. 9125,
  434. 9000,
  435. 8875,
  436. 8750,
  437. 8625,
  438. 8500,
  439. 8375,
  440. 8250,
  441. 8125,
  442. 10000, /* 1.0000V */
  443. 10125,
  444. 10250,
  445. 10375,
  446. 10500,
  447. 10625,
  448. 10750,
  449. 10875,
  450. 11000,
  451. 0, /* reserved */
  452. };
  453. struct vdd_drive {
  454. u8 vid;
  455. unsigned voltage;
  456. };
  457. ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
  458. if (ret) {
  459. debug("VID: I2C failed to switch channel\n");
  460. ret = -1;
  461. goto exit;
  462. }
  463. ret = find_ir_chip_on_i2c();
  464. if (ret < 0) {
  465. printf("VID: Could not find voltage regulator on I2C.\n");
  466. ret = -1;
  467. goto exit;
  468. } else {
  469. i2caddress = ret;
  470. debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
  471. }
  472. /* check IR chip work on Intel mode*/
  473. ret = i2c_read(i2caddress,
  474. IR36021_INTEL_MODE_OOFSET,
  475. 1, (void *)&buf, 1);
  476. if (ret) {
  477. printf("VID: failed to read IR chip mode.\n");
  478. ret = -1;
  479. goto exit;
  480. }
  481. if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
  482. printf("VID: IR Chip is not used in Intel mode.\n");
  483. ret = -1;
  484. goto exit;
  485. }
  486. /* get the voltage ID from fuse status register */
  487. fusesr = in_be32(&gur->dcfg_fusesr);
  488. /*
  489. * VID is used according to the table below
  490. * ---------------------------------------
  491. * | DA_V |
  492. * |-------------------------------------|
  493. * | 5b00000 | 5b00001-5b11110 | 5b11111 |
  494. * ---------------+---------+-----------------+---------|
  495. * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
  496. * | A |----------+---------+-----------------+---------|
  497. * | _ | 5b00001 |VID = | VID = |VID = |
  498. * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
  499. * | _ | 5b11110 | | | |
  500. * | A |----------+---------+-----------------+---------|
  501. * | L | 5b11111 | No VID | VID = DA_V | NO VID |
  502. * | T | | | | |
  503. * ------------------------------------------------------
  504. */
  505. #ifdef CONFIG_FSL_LSCH2
  506. vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
  507. FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
  508. if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
  509. vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
  510. FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
  511. }
  512. #else
  513. vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
  514. FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
  515. if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
  516. vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
  517. FSL_CORENET_DCFG_FUSESR_VID_MASK;
  518. }
  519. #endif
  520. vdd_target = vdd[vid];
  521. /* check override variable for overriding VDD */
  522. vdd_string = env_get(CONFIG_VID_FLS_ENV);
  523. if (vdd_override == 0 && vdd_string &&
  524. !strict_strtoul(vdd_string, 10, &vdd_string_override))
  525. vdd_override = vdd_string_override;
  526. if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
  527. vdd_target = vdd_override * 10; /* convert to 1/10 mV */
  528. debug("VDD override is %lu\n", vdd_override);
  529. } else if (vdd_override != 0) {
  530. printf("Invalid value.\n");
  531. }
  532. if (vdd_target == 0) {
  533. debug("VID: VID not used\n");
  534. ret = 0;
  535. goto exit;
  536. } else {
  537. /* divide and round up by 10 to get a value in mV */
  538. vdd_target = DIV_ROUND_UP(vdd_target, 10);
  539. debug("VID: vid = %d mV\n", vdd_target);
  540. }
  541. /*
  542. * Read voltage monitor to check real voltage.
  543. */
  544. vdd_last = read_voltage(i2caddress);
  545. if (vdd_last < 0) {
  546. printf("VID: Couldn't read sensor abort VID adjustment\n");
  547. ret = -1;
  548. goto exit;
  549. }
  550. vdd_current = vdd_last;
  551. debug("VID: Core voltage is currently at %d mV\n", vdd_last);
  552. /*
  553. * Adjust voltage to at or one step above target.
  554. * As measurements are less precise than setting the values
  555. * we may run through dummy steps that cancel each other
  556. * when stepping up and then down.
  557. */
  558. while (vdd_last > 0 &&
  559. vdd_last < vdd_target) {
  560. vdd_current += IR_VDD_STEP_UP;
  561. vdd_last = set_voltage(i2caddress, vdd_current);
  562. }
  563. while (vdd_last > 0 &&
  564. vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
  565. vdd_current -= IR_VDD_STEP_DOWN;
  566. vdd_last = set_voltage(i2caddress, vdd_current);
  567. }
  568. if (vdd_last > 0)
  569. printf("VID: Core voltage after adjustment is at %d mV\n",
  570. vdd_last);
  571. else
  572. ret = -1;
  573. exit:
  574. if (re_enable)
  575. enable_interrupts();
  576. i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
  577. return ret;
  578. }
  579. #endif
  580. static int print_vdd(void)
  581. {
  582. int vdd_last, ret, i2caddress;
  583. ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
  584. if (ret) {
  585. debug("VID : I2c failed to switch channel\n");
  586. return -1;
  587. }
  588. ret = find_ir_chip_on_i2c();
  589. if (ret < 0) {
  590. printf("VID: Could not find voltage regulator on I2C.\n");
  591. goto exit;
  592. } else {
  593. i2caddress = ret;
  594. debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
  595. }
  596. /*
  597. * Read voltage monitor to check real voltage.
  598. */
  599. vdd_last = read_voltage(i2caddress);
  600. if (vdd_last < 0) {
  601. printf("VID: Couldn't read sensor abort VID adjustment\n");
  602. goto exit;
  603. }
  604. printf("VID: Core voltage is at %d mV\n", vdd_last);
  605. exit:
  606. i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
  607. return ret < 0 ? -1 : 0;
  608. }
  609. static int do_vdd_override(cmd_tbl_t *cmdtp,
  610. int flag, int argc,
  611. char * const argv[])
  612. {
  613. ulong override;
  614. if (argc < 2)
  615. return CMD_RET_USAGE;
  616. if (!strict_strtoul(argv[1], 10, &override))
  617. adjust_vdd(override); /* the value is checked by callee */
  618. else
  619. return CMD_RET_USAGE;
  620. return 0;
  621. }
  622. static int do_vdd_read(cmd_tbl_t *cmdtp,
  623. int flag, int argc,
  624. char * const argv[])
  625. {
  626. if (argc < 1)
  627. return CMD_RET_USAGE;
  628. print_vdd();
  629. return 0;
  630. }
  631. U_BOOT_CMD(
  632. vdd_override, 2, 0, do_vdd_override,
  633. "override VDD",
  634. " - override with the voltage specified in mV, eg. 1050"
  635. );
  636. U_BOOT_CMD(
  637. vdd_read, 1, 0, do_vdd_read,
  638. "read VDD",
  639. " - Read the voltage specified in mV"
  640. )