vid.c 24 KB

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