vid.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <i2c.h>
  8. #include <asm/io.h>
  9. #ifdef CONFIG_FSL_LSCH2
  10. #include <asm/arch/immap_lsch2.h>
  11. #elif defined(CONFIG_FSL_LSCH3)
  12. #include <asm/arch/immap_lsch3.h>
  13. #else
  14. #include <asm/immap_85xx.h>
  15. #endif
  16. #include "vid.h"
  17. int __weak i2c_multiplexer_select_vid_channel(u8 channel)
  18. {
  19. return 0;
  20. }
  21. /*
  22. * Compensate for a board specific voltage drop between regulator and SoC
  23. * return a value in mV
  24. */
  25. int __weak board_vdd_drop_compensation(void)
  26. {
  27. return 0;
  28. }
  29. /*
  30. * Board specific settings for specific voltage value
  31. */
  32. int __weak board_adjust_vdd(int vdd)
  33. {
  34. return 0;
  35. }
  36. #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
  37. defined(CONFIG_VOL_MONITOR_IR36021_READ)
  38. /*
  39. * Get the i2c address configuration for the IR regulator chip
  40. *
  41. * There are some variance in the RDB HW regarding the I2C address configuration
  42. * for the IR regulator chip, which is likely a problem of external resistor
  43. * accuracy. So we just check each address in a hopefully non-intrusive mode
  44. * and use the first one that seems to work
  45. *
  46. * The IR chip can show up under the following addresses:
  47. * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
  48. * 0x09 (Verified on T1040RDB-PA)
  49. * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
  50. */
  51. static int find_ir_chip_on_i2c(void)
  52. {
  53. int i2caddress;
  54. int ret;
  55. u8 byte;
  56. int i;
  57. const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
  58. /* Check all the address */
  59. for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
  60. i2caddress = ir_i2c_addr[i];
  61. ret = i2c_read(i2caddress,
  62. IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
  63. sizeof(byte));
  64. if ((ret >= 0) && (byte == IR36021_MFR_ID))
  65. return i2caddress;
  66. }
  67. return -1;
  68. }
  69. #endif
  70. /* Maximum loop count waiting for new voltage to take effect */
  71. #define MAX_LOOP_WAIT_NEW_VOL 100
  72. /* Maximum loop count waiting for the voltage to be stable */
  73. #define MAX_LOOP_WAIT_VOL_STABLE 100
  74. /*
  75. * read_voltage from sensor on I2C bus
  76. * We use average of 4 readings, waiting for WAIT_FOR_ADC before
  77. * another reading
  78. */
  79. #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
  80. /* If an INA220 chip is available, we can use it to read back the voltage
  81. * as it may have a higher accuracy than the IR chip for the same purpose
  82. */
  83. #ifdef CONFIG_VOL_MONITOR_INA220
  84. #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
  85. #define ADC_MIN_ACCURACY 4
  86. #else
  87. #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
  88. #define ADC_MIN_ACCURACY 4
  89. #endif
  90. #ifdef CONFIG_VOL_MONITOR_INA220
  91. static int read_voltage_from_INA220(int i2caddress)
  92. {
  93. int i, ret, voltage_read = 0;
  94. u16 vol_mon;
  95. u8 buf[2];
  96. for (i = 0; i < NUM_READINGS; i++) {
  97. ret = i2c_read(I2C_VOL_MONITOR_ADDR,
  98. I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
  99. (void *)&buf, 2);
  100. if (ret) {
  101. printf("VID: failed to read core voltage\n");
  102. return ret;
  103. }
  104. vol_mon = (buf[0] << 8) | buf[1];
  105. if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
  106. printf("VID: Core voltage sensor error\n");
  107. return -1;
  108. }
  109. debug("VID: bus voltage reads 0x%04x\n", vol_mon);
  110. /* LSB = 4mv */
  111. voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
  112. udelay(WAIT_FOR_ADC);
  113. }
  114. /* calculate the average */
  115. voltage_read /= NUM_READINGS;
  116. return voltage_read;
  117. }
  118. #endif
  119. /* read voltage from IR */
  120. #ifdef CONFIG_VOL_MONITOR_IR36021_READ
  121. static int read_voltage_from_IR(int i2caddress)
  122. {
  123. int i, ret, voltage_read = 0;
  124. u16 vol_mon;
  125. u8 buf;
  126. for (i = 0; i < NUM_READINGS; i++) {
  127. ret = i2c_read(i2caddress,
  128. IR36021_LOOP1_VOUT_OFFSET,
  129. 1, (void *)&buf, 1);
  130. if (ret) {
  131. printf("VID: failed to read vcpu\n");
  132. return ret;
  133. }
  134. vol_mon = buf;
  135. if (!vol_mon) {
  136. printf("VID: Core voltage sensor error\n");
  137. return -1;
  138. }
  139. debug("VID: bus voltage reads 0x%02x\n", vol_mon);
  140. /* Resolution is 1/128V. We scale up here to get 1/128mV
  141. * and divide at the end
  142. */
  143. voltage_read += vol_mon * 1000;
  144. udelay(WAIT_FOR_ADC);
  145. }
  146. /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
  147. voltage_read = DIV_ROUND_UP(voltage_read, 128);
  148. /* calculate the average */
  149. voltage_read /= NUM_READINGS;
  150. /* Compensate for a board specific voltage drop between regulator and
  151. * SoC before converting into an IR VID value
  152. */
  153. voltage_read -= board_vdd_drop_compensation();
  154. return voltage_read;
  155. }
  156. #endif
  157. #ifdef CONFIG_VOL_MONITOR_LTC3882_READ
  158. /* read the current value of the LTC Regulator Voltage */
  159. static int read_voltage_from_LTC(int i2caddress)
  160. {
  161. int ret, vcode = 0;
  162. u8 chan = PWM_CHANNEL0;
  163. /* select the PAGE 0 using PMBus commands PAGE for VDD*/
  164. ret = i2c_write(I2C_VOL_MONITOR_ADDR,
  165. PMBUS_CMD_PAGE, 1, &chan, 1);
  166. if (ret) {
  167. printf("VID: failed to select VDD Page 0\n");
  168. return ret;
  169. }
  170. /*read the output voltage using PMBus command READ_VOUT*/
  171. ret = i2c_read(I2C_VOL_MONITOR_ADDR,
  172. PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
  173. if (ret) {
  174. printf("VID: failed to read the volatge\n");
  175. return ret;
  176. }
  177. /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
  178. vcode = DIV_ROUND_UP(vcode * 1000, 4096);
  179. return vcode;
  180. }
  181. #endif
  182. static int read_voltage(int i2caddress)
  183. {
  184. int voltage_read;
  185. #ifdef CONFIG_VOL_MONITOR_INA220
  186. voltage_read = read_voltage_from_INA220(i2caddress);
  187. #elif defined CONFIG_VOL_MONITOR_IR36021_READ
  188. voltage_read = read_voltage_from_IR(i2caddress);
  189. #elif defined CONFIG_VOL_MONITOR_LTC3882_READ
  190. voltage_read = read_voltage_from_LTC(i2caddress);
  191. #else
  192. return -1;
  193. #endif
  194. return voltage_read;
  195. }
  196. #ifdef CONFIG_VOL_MONITOR_IR36021_SET
  197. /*
  198. * We need to calculate how long before the voltage stops to drop
  199. * or increase. It returns with the loop count. Each loop takes
  200. * several readings (WAIT_FOR_ADC)
  201. */
  202. static int wait_for_new_voltage(int vdd, int i2caddress)
  203. {
  204. int timeout, vdd_current;
  205. vdd_current = read_voltage(i2caddress);
  206. /* wait until voltage starts to reach the target. Voltage slew
  207. * rates by typical regulators will always lead to stable readings
  208. * within each fairly long ADC interval in comparison to the
  209. * intended voltage delta change until the target voltage is
  210. * reached. The fairly small voltage delta change to any target
  211. * VID voltage also means that this function will always complete
  212. * within few iterations. If the timeout was ever reached, it would
  213. * point to a serious failure in the regulator system.
  214. */
  215. for (timeout = 0;
  216. abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
  217. timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
  218. vdd_current = read_voltage(i2caddress);
  219. }
  220. if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
  221. printf("VID: Voltage adjustment timeout\n");
  222. return -1;
  223. }
  224. return timeout;
  225. }
  226. /*
  227. * this function keeps reading the voltage until it is stable or until the
  228. * timeout expires
  229. */
  230. static int wait_for_voltage_stable(int i2caddress)
  231. {
  232. int timeout, vdd_current, vdd;
  233. vdd = read_voltage(i2caddress);
  234. udelay(NUM_READINGS * WAIT_FOR_ADC);
  235. /* wait until voltage is stable */
  236. vdd_current = read_voltage(i2caddress);
  237. /* The maximum timeout is
  238. * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
  239. */
  240. for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
  241. abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
  242. timeout > 0; timeout--) {
  243. vdd = vdd_current;
  244. udelay(NUM_READINGS * WAIT_FOR_ADC);
  245. vdd_current = read_voltage(i2caddress);
  246. }
  247. if (timeout == 0)
  248. return -1;
  249. return vdd_current;
  250. }
  251. /* Set the voltage to the IR chip */
  252. static int set_voltage_to_IR(int i2caddress, int vdd)
  253. {
  254. int wait, vdd_last;
  255. int ret;
  256. u8 vid;
  257. /* Compensate for a board specific voltage drop between regulator and
  258. * SoC before converting into an IR VID value
  259. */
  260. vdd += board_vdd_drop_compensation();
  261. #ifdef CONFIG_FSL_LSCH2
  262. vid = DIV_ROUND_UP(vdd - 265, 5);
  263. #else
  264. vid = DIV_ROUND_UP(vdd - 245, 5);
  265. #endif
  266. ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
  267. 1, (void *)&vid, sizeof(vid));
  268. if (ret) {
  269. printf("VID: failed to write VID\n");
  270. return -1;
  271. }
  272. wait = wait_for_new_voltage(vdd, i2caddress);
  273. if (wait < 0)
  274. return -1;
  275. debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
  276. vdd_last = wait_for_voltage_stable(i2caddress);
  277. if (vdd_last < 0)
  278. return -1;
  279. debug("VID: Current voltage is %d mV\n", vdd_last);
  280. return vdd_last;
  281. }
  282. #endif
  283. #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
  284. /* this function sets the VDD and returns the value set */
  285. static int set_voltage_to_LTC(int i2caddress, int vdd)
  286. {
  287. int ret, vdd_last, vdd_target = vdd;
  288. /* Scale up to the LTC resolution is 1/4096V */
  289. vdd = (vdd * 4096) / 1000;
  290. /* 5-byte buffer which needs to be sent following the
  291. * PMBus command PAGE_PLUS_WRITE.
  292. */
  293. u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
  294. vdd & 0xFF, (vdd & 0xFF00) >> 8};
  295. /* Write the desired voltage code to the regulator */
  296. ret = i2c_write(I2C_VOL_MONITOR_ADDR,
  297. PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
  298. if (ret) {
  299. printf("VID: I2C failed to write to the volatge regulator\n");
  300. return -1;
  301. }
  302. /* Wait for the volatge to get to the desired value */
  303. do {
  304. vdd_last = read_voltage_from_LTC(i2caddress);
  305. if (vdd_last < 0) {
  306. printf("VID: Couldn't read sensor abort VID adjust\n");
  307. return -1;
  308. }
  309. } while (vdd_last != vdd_target);
  310. return vdd_last;
  311. }
  312. #endif
  313. static int set_voltage(int i2caddress, int vdd)
  314. {
  315. int vdd_last = -1;
  316. #ifdef CONFIG_VOL_MONITOR_IR36021_SET
  317. vdd_last = set_voltage_to_IR(i2caddress, vdd);
  318. #elif defined CONFIG_VOL_MONITOR_LTC3882_SET
  319. vdd_last = set_voltage_to_LTC(i2caddress, vdd);
  320. #else
  321. #error Specific voltage monitor must be defined
  322. #endif
  323. return vdd_last;
  324. }
  325. #ifdef CONFIG_FSL_LSCH3
  326. int adjust_vdd(ulong vdd_override)
  327. {
  328. int re_enable = disable_interrupts();
  329. struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  330. u32 fusesr;
  331. #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
  332. defined(CONFIG_VOL_MONITOR_IR36021_READ)
  333. u8 vid, buf;
  334. #else
  335. u8 vid;
  336. #endif
  337. int vdd_target, vdd_current, vdd_last;
  338. int ret, i2caddress;
  339. unsigned long vdd_string_override;
  340. char *vdd_string;
  341. #ifdef CONFIG_ARCH_LS1088A
  342. static const uint16_t vdd[32] = {
  343. 10250,
  344. 9875,
  345. 9750,
  346. 0, /* reserved */
  347. 0, /* reserved */
  348. 0, /* reserved */
  349. 0, /* reserved */
  350. 0, /* reserved */
  351. 9000,
  352. 0, /* reserved */
  353. 0, /* reserved */
  354. 0, /* reserved */
  355. 0, /* reserved */
  356. 0, /* reserved */
  357. 0, /* reserved */
  358. 0, /* reserved */
  359. 10000, /* 1.0000V */
  360. 10125,
  361. 10250,
  362. 0, /* reserved */
  363. 0, /* reserved */
  364. 0, /* reserved */
  365. 0, /* reserved */
  366. 0, /* reserved */
  367. 0, /* reserved */
  368. 0, /* reserved */
  369. 0, /* reserved */
  370. 0, /* reserved */
  371. 0, /* reserved */
  372. 0, /* reserved */
  373. 0, /* reserved */
  374. 0, /* reserved */
  375. };
  376. #else
  377. static const uint16_t vdd[32] = {
  378. 10500,
  379. 0, /* reserved */
  380. 9750,
  381. 0, /* reserved */
  382. 9500,
  383. 0, /* reserved */
  384. 0, /* reserved */
  385. 0, /* reserved */
  386. 0, /* reserved */
  387. 0, /* reserved */
  388. 0, /* reserved */
  389. 9000, /* reserved */
  390. 0, /* reserved */
  391. 0, /* reserved */
  392. 0, /* reserved */
  393. 0, /* reserved */
  394. 10000, /* 1.0000V */
  395. 0, /* reserved */
  396. 10250,
  397. 0, /* reserved */
  398. 10500,
  399. 0, /* reserved */
  400. 0, /* reserved */
  401. 0, /* reserved */
  402. 0, /* reserved */
  403. 0, /* reserved */
  404. 0, /* reserved */
  405. 0, /* reserved */
  406. 0, /* reserved */
  407. 0, /* reserved */
  408. 0, /* reserved */
  409. 0, /* reserved */
  410. };
  411. #endif
  412. struct vdd_drive {
  413. u8 vid;
  414. unsigned voltage;
  415. };
  416. ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
  417. if (ret) {
  418. debug("VID: I2C failed to switch channel\n");
  419. ret = -1;
  420. goto exit;
  421. }
  422. #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
  423. defined(CONFIG_VOL_MONITOR_IR36021_READ)
  424. ret = find_ir_chip_on_i2c();
  425. if (ret < 0) {
  426. printf("VID: Could not find voltage regulator on I2C.\n");
  427. ret = -1;
  428. goto exit;
  429. } else {
  430. i2caddress = ret;
  431. debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
  432. }
  433. /* check IR chip work on Intel mode*/
  434. ret = i2c_read(i2caddress,
  435. IR36021_INTEL_MODE_OOFSET,
  436. 1, (void *)&buf, 1);
  437. if (ret) {
  438. printf("VID: failed to read IR chip mode.\n");
  439. ret = -1;
  440. goto exit;
  441. }
  442. if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
  443. printf("VID: IR Chip is not used in Intel mode.\n");
  444. ret = -1;
  445. goto exit;
  446. }
  447. #endif
  448. /* get the voltage ID from fuse status register */
  449. fusesr = in_le32(&gur->dcfg_fusesr);
  450. vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
  451. FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
  452. if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
  453. vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
  454. FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
  455. }
  456. vdd_target = vdd[vid];
  457. /* check override variable for overriding VDD */
  458. vdd_string = env_get(CONFIG_VID_FLS_ENV);
  459. if (vdd_override == 0 && vdd_string &&
  460. !strict_strtoul(vdd_string, 10, &vdd_string_override))
  461. vdd_override = vdd_string_override;
  462. if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
  463. vdd_target = vdd_override * 10; /* convert to 1/10 mV */
  464. debug("VDD override is %lu\n", vdd_override);
  465. } else if (vdd_override != 0) {
  466. printf("Invalid value.\n");
  467. }
  468. /* divide and round up by 10 to get a value in mV */
  469. vdd_target = DIV_ROUND_UP(vdd_target, 10);
  470. if (vdd_target == 0) {
  471. debug("VID: VID not used\n");
  472. ret = 0;
  473. goto exit;
  474. } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
  475. /* Check vdd_target is in valid range */
  476. printf("VID: Target VID %d mV is not in range.\n",
  477. vdd_target);
  478. ret = -1;
  479. goto exit;
  480. } else {
  481. debug("VID: vid = %d mV\n", vdd_target);
  482. }
  483. /*
  484. * Read voltage monitor to check real voltage.
  485. */
  486. vdd_last = read_voltage(i2caddress);
  487. if (vdd_last < 0) {
  488. printf("VID: Couldn't read sensor abort VID adjustment\n");
  489. ret = -1;
  490. goto exit;
  491. }
  492. vdd_current = vdd_last;
  493. debug("VID: Core voltage is currently at %d mV\n", vdd_last);
  494. #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
  495. /* Set the target voltage */
  496. vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
  497. #else
  498. /*
  499. * Adjust voltage to at or one step above target.
  500. * As measurements are less precise than setting the values
  501. * we may run through dummy steps that cancel each other
  502. * when stepping up and then down.
  503. */
  504. while (vdd_last > 0 &&
  505. vdd_last < vdd_target) {
  506. vdd_current += IR_VDD_STEP_UP;
  507. vdd_last = set_voltage(i2caddress, vdd_current);
  508. }
  509. while (vdd_last > 0 &&
  510. vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
  511. vdd_current -= IR_VDD_STEP_DOWN;
  512. vdd_last = set_voltage(i2caddress, vdd_current);
  513. }
  514. #endif
  515. if (board_adjust_vdd(vdd_target) < 0) {
  516. ret = -1;
  517. goto exit;
  518. }
  519. if (vdd_last > 0)
  520. printf("VID: Core voltage after adjustment is at %d mV\n",
  521. vdd_last);
  522. else
  523. ret = -1;
  524. exit:
  525. if (re_enable)
  526. enable_interrupts();
  527. i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
  528. return ret;
  529. }
  530. #else /* !CONFIG_FSL_LSCH3 */
  531. int adjust_vdd(ulong vdd_override)
  532. {
  533. int re_enable = disable_interrupts();
  534. #if defined(CONFIG_FSL_LSCH2)
  535. struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
  536. #else
  537. ccsr_gur_t __iomem *gur =
  538. (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
  539. #endif
  540. u32 fusesr;
  541. u8 vid, buf;
  542. int vdd_target, vdd_current, vdd_last;
  543. int ret, i2caddress;
  544. unsigned long vdd_string_override;
  545. char *vdd_string;
  546. static const uint16_t vdd[32] = {
  547. 0, /* unused */
  548. 9875, /* 0.9875V */
  549. 9750,
  550. 9625,
  551. 9500,
  552. 9375,
  553. 9250,
  554. 9125,
  555. 9000,
  556. 8875,
  557. 8750,
  558. 8625,
  559. 8500,
  560. 8375,
  561. 8250,
  562. 8125,
  563. 10000, /* 1.0000V */
  564. 10125,
  565. 10250,
  566. 10375,
  567. 10500,
  568. 10625,
  569. 10750,
  570. 10875,
  571. 11000,
  572. 0, /* reserved */
  573. };
  574. struct vdd_drive {
  575. u8 vid;
  576. unsigned voltage;
  577. };
  578. ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
  579. if (ret) {
  580. debug("VID: I2C failed to switch channel\n");
  581. ret = -1;
  582. goto exit;
  583. }
  584. #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
  585. defined(CONFIG_VOL_MONITOR_IR36021_READ)
  586. ret = find_ir_chip_on_i2c();
  587. if (ret < 0) {
  588. printf("VID: Could not find voltage regulator on I2C.\n");
  589. ret = -1;
  590. goto exit;
  591. } else {
  592. i2caddress = ret;
  593. debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
  594. }
  595. /* check IR chip work on Intel mode*/
  596. ret = i2c_read(i2caddress,
  597. IR36021_INTEL_MODE_OOFSET,
  598. 1, (void *)&buf, 1);
  599. if (ret) {
  600. printf("VID: failed to read IR chip mode.\n");
  601. ret = -1;
  602. goto exit;
  603. }
  604. if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
  605. printf("VID: IR Chip is not used in Intel mode.\n");
  606. ret = -1;
  607. goto exit;
  608. }
  609. #endif
  610. /* get the voltage ID from fuse status register */
  611. fusesr = in_be32(&gur->dcfg_fusesr);
  612. /*
  613. * VID is used according to the table below
  614. * ---------------------------------------
  615. * | DA_V |
  616. * |-------------------------------------|
  617. * | 5b00000 | 5b00001-5b11110 | 5b11111 |
  618. * ---------------+---------+-----------------+---------|
  619. * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
  620. * | A |----------+---------+-----------------+---------|
  621. * | _ | 5b00001 |VID = | VID = |VID = |
  622. * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
  623. * | _ | 5b11110 | | | |
  624. * | A |----------+---------+-----------------+---------|
  625. * | L | 5b11111 | No VID | VID = DA_V | NO VID |
  626. * | T | | | | |
  627. * ------------------------------------------------------
  628. */
  629. #ifdef CONFIG_FSL_LSCH2
  630. vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
  631. FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
  632. if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
  633. vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
  634. FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
  635. }
  636. #else
  637. vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
  638. FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
  639. if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
  640. vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
  641. FSL_CORENET_DCFG_FUSESR_VID_MASK;
  642. }
  643. #endif
  644. vdd_target = vdd[vid];
  645. /* check override variable for overriding VDD */
  646. vdd_string = env_get(CONFIG_VID_FLS_ENV);
  647. if (vdd_override == 0 && vdd_string &&
  648. !strict_strtoul(vdd_string, 10, &vdd_string_override))
  649. vdd_override = vdd_string_override;
  650. if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
  651. vdd_target = vdd_override * 10; /* convert to 1/10 mV */
  652. debug("VDD override is %lu\n", vdd_override);
  653. } else if (vdd_override != 0) {
  654. printf("Invalid value.\n");
  655. }
  656. if (vdd_target == 0) {
  657. debug("VID: VID not used\n");
  658. ret = 0;
  659. goto exit;
  660. } else {
  661. /* divide and round up by 10 to get a value in mV */
  662. vdd_target = DIV_ROUND_UP(vdd_target, 10);
  663. debug("VID: vid = %d mV\n", vdd_target);
  664. }
  665. /*
  666. * Read voltage monitor to check real voltage.
  667. */
  668. vdd_last = read_voltage(i2caddress);
  669. if (vdd_last < 0) {
  670. printf("VID: Couldn't read sensor abort VID adjustment\n");
  671. ret = -1;
  672. goto exit;
  673. }
  674. vdd_current = vdd_last;
  675. debug("VID: Core voltage is currently at %d mV\n", vdd_last);
  676. /*
  677. * Adjust voltage to at or one step above target.
  678. * As measurements are less precise than setting the values
  679. * we may run through dummy steps that cancel each other
  680. * when stepping up and then down.
  681. */
  682. while (vdd_last > 0 &&
  683. vdd_last < vdd_target) {
  684. vdd_current += IR_VDD_STEP_UP;
  685. vdd_last = set_voltage(i2caddress, vdd_current);
  686. }
  687. while (vdd_last > 0 &&
  688. vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
  689. vdd_current -= IR_VDD_STEP_DOWN;
  690. vdd_last = set_voltage(i2caddress, vdd_current);
  691. }
  692. if (vdd_last > 0)
  693. printf("VID: Core voltage after adjustment is at %d mV\n",
  694. vdd_last);
  695. else
  696. ret = -1;
  697. exit:
  698. if (re_enable)
  699. enable_interrupts();
  700. i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
  701. return ret;
  702. }
  703. #endif
  704. static int print_vdd(void)
  705. {
  706. int vdd_last, ret, i2caddress;
  707. ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
  708. if (ret) {
  709. debug("VID : I2c failed to switch channel\n");
  710. return -1;
  711. }
  712. #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
  713. defined(CONFIG_VOL_MONITOR_IR36021_READ)
  714. ret = find_ir_chip_on_i2c();
  715. if (ret < 0) {
  716. printf("VID: Could not find voltage regulator on I2C.\n");
  717. goto exit;
  718. } else {
  719. i2caddress = ret;
  720. debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
  721. }
  722. #endif
  723. /*
  724. * Read voltage monitor to check real voltage.
  725. */
  726. vdd_last = read_voltage(i2caddress);
  727. if (vdd_last < 0) {
  728. printf("VID: Couldn't read sensor abort VID adjustment\n");
  729. goto exit;
  730. }
  731. printf("VID: Core voltage is at %d mV\n", vdd_last);
  732. exit:
  733. i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
  734. return ret < 0 ? -1 : 0;
  735. }
  736. static int do_vdd_override(cmd_tbl_t *cmdtp,
  737. int flag, int argc,
  738. char * const argv[])
  739. {
  740. ulong override;
  741. if (argc < 2)
  742. return CMD_RET_USAGE;
  743. if (!strict_strtoul(argv[1], 10, &override))
  744. adjust_vdd(override); /* the value is checked by callee */
  745. else
  746. return CMD_RET_USAGE;
  747. return 0;
  748. }
  749. static int do_vdd_read(cmd_tbl_t *cmdtp,
  750. int flag, int argc,
  751. char * const argv[])
  752. {
  753. if (argc < 1)
  754. return CMD_RET_USAGE;
  755. print_vdd();
  756. return 0;
  757. }
  758. U_BOOT_CMD(
  759. vdd_override, 2, 0, do_vdd_override,
  760. "override VDD",
  761. " - override with the voltage specified in mV, eg. 1050"
  762. );
  763. U_BOOT_CMD(
  764. vdd_read, 1, 0, do_vdd_read,
  765. "read VDD",
  766. " - Read the voltage specified in mV"
  767. )