vid.c 24 KB

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