vid.c 24 KB

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