vid.c 21 KB

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