spl_power_init.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * Freescale i.MX28 Boot PMIC init
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. * on behalf of DENX Software Engineering GmbH
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <config.h>
  11. #include <asm/io.h>
  12. #include <asm/arch/imx-regs.h>
  13. #include "mxs_init.h"
  14. /**
  15. * mxs_power_clock2xtal() - Switch CPU core clock source to 24MHz XTAL
  16. *
  17. * This function switches the CPU core clock from PLL to 24MHz XTAL
  18. * oscilator. This is necessary if the PLL is being reconfigured to
  19. * prevent crash of the CPU core.
  20. */
  21. static void mxs_power_clock2xtal(void)
  22. {
  23. struct mxs_clkctrl_regs *clkctrl_regs =
  24. (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
  25. /* Set XTAL as CPU reference clock */
  26. writel(CLKCTRL_CLKSEQ_BYPASS_CPU,
  27. &clkctrl_regs->hw_clkctrl_clkseq_set);
  28. }
  29. /**
  30. * mxs_power_clock2pll() - Switch CPU core clock source to PLL
  31. *
  32. * This function switches the CPU core clock from 24MHz XTAL oscilator
  33. * to PLL. This can only be called once the PLL has re-locked and once
  34. * the PLL is stable after reconfiguration.
  35. */
  36. static void mxs_power_clock2pll(void)
  37. {
  38. struct mxs_clkctrl_regs *clkctrl_regs =
  39. (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
  40. setbits_le32(&clkctrl_regs->hw_clkctrl_pll0ctrl0,
  41. CLKCTRL_PLL0CTRL0_POWER);
  42. early_delay(100);
  43. setbits_le32(&clkctrl_regs->hw_clkctrl_clkseq,
  44. CLKCTRL_CLKSEQ_BYPASS_CPU);
  45. }
  46. /**
  47. * mxs_power_set_auto_restart() - Set the auto-restart bit
  48. *
  49. * This function ungates the RTC block and sets the AUTO_RESTART
  50. * bit to work around a design bug on MX28EVK Rev. A .
  51. */
  52. static void mxs_power_set_auto_restart(void)
  53. {
  54. struct mxs_rtc_regs *rtc_regs =
  55. (struct mxs_rtc_regs *)MXS_RTC_BASE;
  56. writel(RTC_CTRL_SFTRST, &rtc_regs->hw_rtc_ctrl_clr);
  57. while (readl(&rtc_regs->hw_rtc_ctrl) & RTC_CTRL_SFTRST)
  58. ;
  59. writel(RTC_CTRL_CLKGATE, &rtc_regs->hw_rtc_ctrl_clr);
  60. while (readl(&rtc_regs->hw_rtc_ctrl) & RTC_CTRL_CLKGATE)
  61. ;
  62. /* Do nothing if flag already set */
  63. if (readl(&rtc_regs->hw_rtc_persistent0) & RTC_PERSISTENT0_AUTO_RESTART)
  64. return;
  65. while (readl(&rtc_regs->hw_rtc_stat) & RTC_STAT_NEW_REGS_MASK)
  66. ;
  67. setbits_le32(&rtc_regs->hw_rtc_persistent0,
  68. RTC_PERSISTENT0_AUTO_RESTART);
  69. writel(RTC_CTRL_FORCE_UPDATE, &rtc_regs->hw_rtc_ctrl_set);
  70. writel(RTC_CTRL_FORCE_UPDATE, &rtc_regs->hw_rtc_ctrl_clr);
  71. while (readl(&rtc_regs->hw_rtc_stat) & RTC_STAT_NEW_REGS_MASK)
  72. ;
  73. while (readl(&rtc_regs->hw_rtc_stat) & RTC_STAT_STALE_REGS_MASK)
  74. ;
  75. }
  76. /**
  77. * mxs_power_set_linreg() - Set linear regulators 25mV below DC-DC converter
  78. *
  79. * This function configures the VDDIO, VDDA and VDDD linear regulators output
  80. * to be 25mV below the VDDIO, VDDA and VDDD output from the DC-DC switching
  81. * converter. This is the recommended setting for the case where we use both
  82. * linear regulators and DC-DC converter to power the VDDIO rail.
  83. */
  84. static void mxs_power_set_linreg(void)
  85. {
  86. struct mxs_power_regs *power_regs =
  87. (struct mxs_power_regs *)MXS_POWER_BASE;
  88. /* Set linear regulator 25mV below switching converter */
  89. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  90. POWER_VDDDCTRL_LINREG_OFFSET_MASK,
  91. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW);
  92. clrsetbits_le32(&power_regs->hw_power_vddactrl,
  93. POWER_VDDACTRL_LINREG_OFFSET_MASK,
  94. POWER_VDDACTRL_LINREG_OFFSET_1STEPS_BELOW);
  95. clrsetbits_le32(&power_regs->hw_power_vddioctrl,
  96. POWER_VDDIOCTRL_LINREG_OFFSET_MASK,
  97. POWER_VDDIOCTRL_LINREG_OFFSET_1STEPS_BELOW);
  98. }
  99. /**
  100. * mxs_get_batt_volt() - Measure battery input voltage
  101. *
  102. * This function retrieves the battery input voltage and returns it.
  103. */
  104. static int mxs_get_batt_volt(void)
  105. {
  106. struct mxs_power_regs *power_regs =
  107. (struct mxs_power_regs *)MXS_POWER_BASE;
  108. uint32_t volt = readl(&power_regs->hw_power_battmonitor);
  109. volt &= POWER_BATTMONITOR_BATT_VAL_MASK;
  110. volt >>= POWER_BATTMONITOR_BATT_VAL_OFFSET;
  111. volt *= 8;
  112. return volt;
  113. }
  114. /**
  115. * mxs_is_batt_ready() - Test if the battery provides enough voltage to boot
  116. *
  117. * This function checks if the battery input voltage is higher than 3.6V and
  118. * therefore allows the system to successfully boot using this power source.
  119. */
  120. static int mxs_is_batt_ready(void)
  121. {
  122. return (mxs_get_batt_volt() >= 3600);
  123. }
  124. /**
  125. * mxs_is_batt_good() - Test if battery is operational at all
  126. *
  127. * This function starts recharging the battery and tests if the input current
  128. * provided by the 5V input recharging the battery is also sufficient to power
  129. * the DC-DC converter.
  130. */
  131. static int mxs_is_batt_good(void)
  132. {
  133. struct mxs_power_regs *power_regs =
  134. (struct mxs_power_regs *)MXS_POWER_BASE;
  135. uint32_t volt = mxs_get_batt_volt();
  136. if ((volt >= 2400) && (volt <= 4300))
  137. return 1;
  138. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  139. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  140. 0x3 << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  141. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  142. &power_regs->hw_power_5vctrl_clr);
  143. clrsetbits_le32(&power_regs->hw_power_charge,
  144. POWER_CHARGE_STOP_ILIMIT_MASK | POWER_CHARGE_BATTCHRG_I_MASK,
  145. POWER_CHARGE_STOP_ILIMIT_10MA | 0x3);
  146. writel(POWER_CHARGE_PWD_BATTCHRG, &power_regs->hw_power_charge_clr);
  147. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  148. &power_regs->hw_power_5vctrl_clr);
  149. early_delay(500000);
  150. volt = mxs_get_batt_volt();
  151. if (volt >= 3500)
  152. return 0;
  153. if (volt >= 2400)
  154. return 1;
  155. writel(POWER_CHARGE_STOP_ILIMIT_MASK | POWER_CHARGE_BATTCHRG_I_MASK,
  156. &power_regs->hw_power_charge_clr);
  157. writel(POWER_CHARGE_PWD_BATTCHRG, &power_regs->hw_power_charge_set);
  158. return 0;
  159. }
  160. /**
  161. * mxs_power_setup_5v_detect() - Start the 5V input detection comparator
  162. *
  163. * This function enables the 5V detection comparator and sets the 5V valid
  164. * threshold to 4.4V . We use 4.4V threshold here to make sure that even
  165. * under high load, the voltage drop on the 5V input won't be so critical
  166. * to cause undervolt on the 4P2 linear regulator supplying the DC-DC
  167. * converter and thus making the system crash.
  168. */
  169. static void mxs_power_setup_5v_detect(void)
  170. {
  171. struct mxs_power_regs *power_regs =
  172. (struct mxs_power_regs *)MXS_POWER_BASE;
  173. /* Start 5V detection */
  174. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  175. POWER_5VCTRL_VBUSVALID_TRSH_MASK,
  176. POWER_5VCTRL_VBUSVALID_TRSH_4V4 |
  177. POWER_5VCTRL_PWRUP_VBUS_CMPS);
  178. }
  179. /**
  180. * mxs_src_power_init() - Preconfigure the power block
  181. *
  182. * This function configures reasonable values for the DC-DC control loop
  183. * and battery monitor.
  184. */
  185. static void mxs_src_power_init(void)
  186. {
  187. struct mxs_power_regs *power_regs =
  188. (struct mxs_power_regs *)MXS_POWER_BASE;
  189. /* Improve efficieny and reduce transient ripple */
  190. writel(POWER_LOOPCTRL_TOGGLE_DIF | POWER_LOOPCTRL_EN_CM_HYST |
  191. POWER_LOOPCTRL_EN_DF_HYST, &power_regs->hw_power_loopctrl_set);
  192. clrsetbits_le32(&power_regs->hw_power_dclimits,
  193. POWER_DCLIMITS_POSLIMIT_BUCK_MASK,
  194. 0x30 << POWER_DCLIMITS_POSLIMIT_BUCK_OFFSET);
  195. setbits_le32(&power_regs->hw_power_battmonitor,
  196. POWER_BATTMONITOR_EN_BATADJ);
  197. /* Increase the RCSCALE level for quick DCDC response to dynamic load */
  198. clrsetbits_le32(&power_regs->hw_power_loopctrl,
  199. POWER_LOOPCTRL_EN_RCSCALE_MASK,
  200. POWER_LOOPCTRL_RCSCALE_THRESH |
  201. POWER_LOOPCTRL_EN_RCSCALE_8X);
  202. clrsetbits_le32(&power_regs->hw_power_minpwr,
  203. POWER_MINPWR_HALFFETS, POWER_MINPWR_DOUBLE_FETS);
  204. /* 5V to battery handoff ... FIXME */
  205. setbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  206. early_delay(30);
  207. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  208. }
  209. /**
  210. * mxs_power_init_4p2_params() - Configure the parameters of the 4P2 regulator
  211. *
  212. * This function configures the necessary parameters for the 4P2 linear
  213. * regulator to supply the DC-DC converter from 5V input.
  214. */
  215. static void mxs_power_init_4p2_params(void)
  216. {
  217. struct mxs_power_regs *power_regs =
  218. (struct mxs_power_regs *)MXS_POWER_BASE;
  219. /* Setup 4P2 parameters */
  220. clrsetbits_le32(&power_regs->hw_power_dcdc4p2,
  221. POWER_DCDC4P2_CMPTRIP_MASK | POWER_DCDC4P2_TRG_MASK,
  222. POWER_DCDC4P2_TRG_4V2 | (31 << POWER_DCDC4P2_CMPTRIP_OFFSET));
  223. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  224. POWER_5VCTRL_HEADROOM_ADJ_MASK,
  225. 0x4 << POWER_5VCTRL_HEADROOM_ADJ_OFFSET);
  226. clrsetbits_le32(&power_regs->hw_power_dcdc4p2,
  227. POWER_DCDC4P2_DROPOUT_CTRL_MASK,
  228. POWER_DCDC4P2_DROPOUT_CTRL_100MV |
  229. POWER_DCDC4P2_DROPOUT_CTRL_SRC_SEL);
  230. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  231. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  232. 0x3f << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  233. }
  234. /**
  235. * mxs_enable_4p2_dcdc_input() - Enable or disable the DCDC input from 4P2
  236. * @xfer: Select if the input shall be enabled or disabled
  237. *
  238. * This function enables or disables the 4P2 input into the DC-DC converter.
  239. */
  240. static void mxs_enable_4p2_dcdc_input(int xfer)
  241. {
  242. struct mxs_power_regs *power_regs =
  243. (struct mxs_power_regs *)MXS_POWER_BASE;
  244. uint32_t tmp, vbus_thresh, vbus_5vdetect, pwd_bo;
  245. uint32_t prev_5v_brnout, prev_5v_droop;
  246. prev_5v_brnout = readl(&power_regs->hw_power_5vctrl) &
  247. POWER_5VCTRL_PWDN_5VBRNOUT;
  248. prev_5v_droop = readl(&power_regs->hw_power_ctrl) &
  249. POWER_CTRL_ENIRQ_VDD5V_DROOP;
  250. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_PWDN_5VBRNOUT);
  251. writel(POWER_RESET_UNLOCK_KEY | POWER_RESET_PWD_OFF,
  252. &power_regs->hw_power_reset);
  253. clrbits_le32(&power_regs->hw_power_ctrl, POWER_CTRL_ENIRQ_VDD5V_DROOP);
  254. if (xfer && (readl(&power_regs->hw_power_5vctrl) &
  255. POWER_5VCTRL_ENABLE_DCDC)) {
  256. return;
  257. }
  258. /*
  259. * Recording orignal values that will be modified temporarlily
  260. * to handle a chip bug. See chip errata for CQ ENGR00115837
  261. */
  262. tmp = readl(&power_regs->hw_power_5vctrl);
  263. vbus_thresh = tmp & POWER_5VCTRL_VBUSVALID_TRSH_MASK;
  264. vbus_5vdetect = tmp & POWER_5VCTRL_VBUSVALID_5VDETECT;
  265. pwd_bo = readl(&power_regs->hw_power_minpwr) & POWER_MINPWR_PWD_BO;
  266. /*
  267. * Disable mechanisms that get erroneously tripped by when setting
  268. * the DCDC4P2 EN_DCDC
  269. */
  270. clrbits_le32(&power_regs->hw_power_5vctrl,
  271. POWER_5VCTRL_VBUSVALID_5VDETECT |
  272. POWER_5VCTRL_VBUSVALID_TRSH_MASK);
  273. writel(POWER_MINPWR_PWD_BO, &power_regs->hw_power_minpwr_set);
  274. if (xfer) {
  275. setbits_le32(&power_regs->hw_power_5vctrl,
  276. POWER_5VCTRL_DCDC_XFER);
  277. early_delay(20);
  278. clrbits_le32(&power_regs->hw_power_5vctrl,
  279. POWER_5VCTRL_DCDC_XFER);
  280. setbits_le32(&power_regs->hw_power_5vctrl,
  281. POWER_5VCTRL_ENABLE_DCDC);
  282. } else {
  283. setbits_le32(&power_regs->hw_power_dcdc4p2,
  284. POWER_DCDC4P2_ENABLE_DCDC);
  285. }
  286. early_delay(25);
  287. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  288. POWER_5VCTRL_VBUSVALID_TRSH_MASK, vbus_thresh);
  289. if (vbus_5vdetect)
  290. writel(vbus_5vdetect, &power_regs->hw_power_5vctrl_set);
  291. if (!pwd_bo)
  292. clrbits_le32(&power_regs->hw_power_minpwr, POWER_MINPWR_PWD_BO);
  293. while (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VBUS_VALID_IRQ)
  294. writel(POWER_CTRL_VBUS_VALID_IRQ,
  295. &power_regs->hw_power_ctrl_clr);
  296. if (prev_5v_brnout) {
  297. writel(POWER_5VCTRL_PWDN_5VBRNOUT,
  298. &power_regs->hw_power_5vctrl_set);
  299. writel(POWER_RESET_UNLOCK_KEY,
  300. &power_regs->hw_power_reset);
  301. } else {
  302. writel(POWER_5VCTRL_PWDN_5VBRNOUT,
  303. &power_regs->hw_power_5vctrl_clr);
  304. writel(POWER_RESET_UNLOCK_KEY | POWER_RESET_PWD_OFF,
  305. &power_regs->hw_power_reset);
  306. }
  307. while (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VDD5V_DROOP_IRQ)
  308. writel(POWER_CTRL_VDD5V_DROOP_IRQ,
  309. &power_regs->hw_power_ctrl_clr);
  310. if (prev_5v_droop)
  311. clrbits_le32(&power_regs->hw_power_ctrl,
  312. POWER_CTRL_ENIRQ_VDD5V_DROOP);
  313. else
  314. setbits_le32(&power_regs->hw_power_ctrl,
  315. POWER_CTRL_ENIRQ_VDD5V_DROOP);
  316. }
  317. /**
  318. * mxs_power_init_4p2_regulator() - Start the 4P2 regulator
  319. *
  320. * This function enables the 4P2 regulator and switches the DC-DC converter
  321. * to use the 4P2 input.
  322. */
  323. static void mxs_power_init_4p2_regulator(void)
  324. {
  325. struct mxs_power_regs *power_regs =
  326. (struct mxs_power_regs *)MXS_POWER_BASE;
  327. uint32_t tmp, tmp2;
  328. setbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_ENABLE_4P2);
  329. writel(POWER_CHARGE_ENABLE_LOAD, &power_regs->hw_power_charge_set);
  330. writel(POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  331. &power_regs->hw_power_5vctrl_clr);
  332. clrbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_TRG_MASK);
  333. /* Power up the 4p2 rail and logic/control */
  334. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  335. &power_regs->hw_power_5vctrl_clr);
  336. /*
  337. * Start charging up the 4p2 capacitor. We ramp of this charge
  338. * gradually to avoid large inrush current from the 5V cable which can
  339. * cause transients/problems
  340. */
  341. mxs_enable_4p2_dcdc_input(0);
  342. if (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VBUS_VALID_IRQ) {
  343. /*
  344. * If we arrived here, we were unable to recover from mx23 chip
  345. * errata 5837. 4P2 is disabled and sufficient battery power is
  346. * not present. Exiting to not enable DCDC power during 5V
  347. * connected state.
  348. */
  349. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  350. POWER_DCDC4P2_ENABLE_DCDC);
  351. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  352. &power_regs->hw_power_5vctrl_set);
  353. hang();
  354. }
  355. /*
  356. * Here we set the 4p2 brownout level to something very close to 4.2V.
  357. * We then check the brownout status. If the brownout status is false,
  358. * the voltage is already close to the target voltage of 4.2V so we
  359. * can go ahead and set the 4P2 current limit to our max target limit.
  360. * If the brownout status is true, we need to ramp us the current limit
  361. * so that we don't cause large inrush current issues. We step up the
  362. * current limit until the brownout status is false or until we've
  363. * reached our maximum defined 4p2 current limit.
  364. */
  365. clrsetbits_le32(&power_regs->hw_power_dcdc4p2,
  366. POWER_DCDC4P2_BO_MASK,
  367. 22 << POWER_DCDC4P2_BO_OFFSET); /* 4.15V */
  368. if (!(readl(&power_regs->hw_power_sts) & POWER_STS_DCDC_4P2_BO)) {
  369. setbits_le32(&power_regs->hw_power_5vctrl,
  370. 0x3f << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  371. } else {
  372. tmp = (readl(&power_regs->hw_power_5vctrl) &
  373. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK) >>
  374. POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET;
  375. while (tmp < 0x3f) {
  376. if (!(readl(&power_regs->hw_power_sts) &
  377. POWER_STS_DCDC_4P2_BO)) {
  378. tmp = readl(&power_regs->hw_power_5vctrl);
  379. tmp |= POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK;
  380. early_delay(100);
  381. writel(tmp, &power_regs->hw_power_5vctrl);
  382. break;
  383. } else {
  384. tmp++;
  385. tmp2 = readl(&power_regs->hw_power_5vctrl);
  386. tmp2 &= ~POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK;
  387. tmp2 |= tmp <<
  388. POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET;
  389. writel(tmp2, &power_regs->hw_power_5vctrl);
  390. early_delay(100);
  391. }
  392. }
  393. }
  394. clrbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_BO_MASK);
  395. writel(POWER_CTRL_DCDC4P2_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  396. }
  397. /**
  398. * mxs_power_init_dcdc_4p2_source() - Switch DC-DC converter to 4P2 source
  399. *
  400. * This function configures the DC-DC converter to be supplied from the 4P2
  401. * linear regulator.
  402. */
  403. static void mxs_power_init_dcdc_4p2_source(void)
  404. {
  405. struct mxs_power_regs *power_regs =
  406. (struct mxs_power_regs *)MXS_POWER_BASE;
  407. if (!(readl(&power_regs->hw_power_dcdc4p2) &
  408. POWER_DCDC4P2_ENABLE_DCDC)) {
  409. hang();
  410. }
  411. mxs_enable_4p2_dcdc_input(1);
  412. if (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VBUS_VALID_IRQ) {
  413. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  414. POWER_DCDC4P2_ENABLE_DCDC);
  415. writel(POWER_5VCTRL_ENABLE_DCDC,
  416. &power_regs->hw_power_5vctrl_clr);
  417. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  418. &power_regs->hw_power_5vctrl_set);
  419. }
  420. }
  421. /**
  422. * mxs_power_enable_4p2() - Power up the 4P2 regulator
  423. *
  424. * This function drives the process of powering up the 4P2 linear regulator
  425. * and switching the DC-DC converter input over to the 4P2 linear regulator.
  426. */
  427. static void mxs_power_enable_4p2(void)
  428. {
  429. struct mxs_power_regs *power_regs =
  430. (struct mxs_power_regs *)MXS_POWER_BASE;
  431. uint32_t vdddctrl, vddactrl, vddioctrl;
  432. uint32_t tmp;
  433. vdddctrl = readl(&power_regs->hw_power_vdddctrl);
  434. vddactrl = readl(&power_regs->hw_power_vddactrl);
  435. vddioctrl = readl(&power_regs->hw_power_vddioctrl);
  436. setbits_le32(&power_regs->hw_power_vdddctrl,
  437. POWER_VDDDCTRL_DISABLE_FET | POWER_VDDDCTRL_ENABLE_LINREG |
  438. POWER_VDDDCTRL_PWDN_BRNOUT);
  439. setbits_le32(&power_regs->hw_power_vddactrl,
  440. POWER_VDDACTRL_DISABLE_FET | POWER_VDDACTRL_ENABLE_LINREG |
  441. POWER_VDDACTRL_PWDN_BRNOUT);
  442. setbits_le32(&power_regs->hw_power_vddioctrl,
  443. POWER_VDDIOCTRL_DISABLE_FET | POWER_VDDIOCTRL_PWDN_BRNOUT);
  444. mxs_power_init_4p2_params();
  445. mxs_power_init_4p2_regulator();
  446. /* Shutdown battery (none present) */
  447. if (!mxs_is_batt_ready()) {
  448. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  449. POWER_DCDC4P2_BO_MASK);
  450. writel(POWER_CTRL_DCDC4P2_BO_IRQ,
  451. &power_regs->hw_power_ctrl_clr);
  452. writel(POWER_CTRL_ENIRQ_DCDC4P2_BO,
  453. &power_regs->hw_power_ctrl_clr);
  454. }
  455. mxs_power_init_dcdc_4p2_source();
  456. writel(vdddctrl, &power_regs->hw_power_vdddctrl);
  457. early_delay(20);
  458. writel(vddactrl, &power_regs->hw_power_vddactrl);
  459. early_delay(20);
  460. writel(vddioctrl, &power_regs->hw_power_vddioctrl);
  461. /*
  462. * Check if FET is enabled on either powerout and if so,
  463. * disable load.
  464. */
  465. tmp = 0;
  466. tmp |= !(readl(&power_regs->hw_power_vdddctrl) &
  467. POWER_VDDDCTRL_DISABLE_FET);
  468. tmp |= !(readl(&power_regs->hw_power_vddactrl) &
  469. POWER_VDDACTRL_DISABLE_FET);
  470. tmp |= !(readl(&power_regs->hw_power_vddioctrl) &
  471. POWER_VDDIOCTRL_DISABLE_FET);
  472. if (tmp)
  473. writel(POWER_CHARGE_ENABLE_LOAD,
  474. &power_regs->hw_power_charge_clr);
  475. }
  476. /**
  477. * mxs_boot_valid_5v() - Boot from 5V supply
  478. *
  479. * This function configures the power block to boot from valid 5V input.
  480. * This is called only if the 5V is reliable and can properly supply the
  481. * CPU. This function proceeds to configure the 4P2 converter to be supplied
  482. * from the 5V input.
  483. */
  484. static void mxs_boot_valid_5v(void)
  485. {
  486. struct mxs_power_regs *power_regs =
  487. (struct mxs_power_regs *)MXS_POWER_BASE;
  488. /*
  489. * Use VBUSVALID level instead of VDD5V_GT_VDDIO level to trigger a 5V
  490. * disconnect event. FIXME
  491. */
  492. writel(POWER_5VCTRL_VBUSVALID_5VDETECT,
  493. &power_regs->hw_power_5vctrl_set);
  494. /* Configure polarity to check for 5V disconnection. */
  495. writel(POWER_CTRL_POLARITY_VBUSVALID |
  496. POWER_CTRL_POLARITY_VDD5V_GT_VDDIO,
  497. &power_regs->hw_power_ctrl_clr);
  498. writel(POWER_CTRL_VBUS_VALID_IRQ | POWER_CTRL_VDD5V_GT_VDDIO_IRQ,
  499. &power_regs->hw_power_ctrl_clr);
  500. mxs_power_enable_4p2();
  501. }
  502. /**
  503. * mxs_powerdown() - Shut down the system
  504. *
  505. * This function powers down the CPU completely.
  506. */
  507. static void mxs_powerdown(void)
  508. {
  509. struct mxs_power_regs *power_regs =
  510. (struct mxs_power_regs *)MXS_POWER_BASE;
  511. writel(POWER_RESET_UNLOCK_KEY, &power_regs->hw_power_reset);
  512. writel(POWER_RESET_UNLOCK_KEY | POWER_RESET_PWD_OFF,
  513. &power_regs->hw_power_reset);
  514. }
  515. /**
  516. * mxs_batt_boot() - Configure the power block to boot from battery input
  517. *
  518. * This function configures the power block to boot from the battery voltage
  519. * supply.
  520. */
  521. static void mxs_batt_boot(void)
  522. {
  523. struct mxs_power_regs *power_regs =
  524. (struct mxs_power_regs *)MXS_POWER_BASE;
  525. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_PWDN_5VBRNOUT);
  526. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_ENABLE_DCDC);
  527. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  528. POWER_DCDC4P2_ENABLE_DCDC | POWER_DCDC4P2_ENABLE_4P2);
  529. writel(POWER_CHARGE_ENABLE_LOAD, &power_regs->hw_power_charge_clr);
  530. /* 5V to battery handoff. */
  531. setbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  532. early_delay(30);
  533. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  534. writel(POWER_CTRL_ENIRQ_DCDC4P2_BO, &power_regs->hw_power_ctrl_clr);
  535. clrsetbits_le32(&power_regs->hw_power_minpwr,
  536. POWER_MINPWR_HALFFETS, POWER_MINPWR_DOUBLE_FETS);
  537. mxs_power_set_linreg();
  538. clrbits_le32(&power_regs->hw_power_vdddctrl,
  539. POWER_VDDDCTRL_DISABLE_FET | POWER_VDDDCTRL_ENABLE_LINREG);
  540. clrbits_le32(&power_regs->hw_power_vddactrl,
  541. POWER_VDDACTRL_DISABLE_FET | POWER_VDDACTRL_ENABLE_LINREG);
  542. clrbits_le32(&power_regs->hw_power_vddioctrl,
  543. POWER_VDDIOCTRL_DISABLE_FET);
  544. setbits_le32(&power_regs->hw_power_5vctrl,
  545. POWER_5VCTRL_PWD_CHARGE_4P2_MASK);
  546. setbits_le32(&power_regs->hw_power_5vctrl,
  547. POWER_5VCTRL_ENABLE_DCDC);
  548. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  549. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  550. 0x8 << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  551. mxs_power_enable_4p2();
  552. }
  553. /**
  554. * mxs_handle_5v_conflict() - Test if the 5V input is reliable
  555. *
  556. * This function tests if the 5V input can reliably supply the system. If it
  557. * can, then proceed to configuring the system to boot from 5V source, otherwise
  558. * try booting from battery supply. If we can not boot from battery supply
  559. * either, shut down the system.
  560. */
  561. static void mxs_handle_5v_conflict(void)
  562. {
  563. struct mxs_power_regs *power_regs =
  564. (struct mxs_power_regs *)MXS_POWER_BASE;
  565. uint32_t tmp;
  566. setbits_le32(&power_regs->hw_power_vddioctrl,
  567. POWER_VDDIOCTRL_BO_OFFSET_MASK);
  568. for (;;) {
  569. tmp = readl(&power_regs->hw_power_sts);
  570. if (tmp & POWER_STS_VDDIO_BO) {
  571. /*
  572. * VDDIO has a brownout, then the VDD5V_GT_VDDIO becomes
  573. * unreliable
  574. */
  575. mxs_powerdown();
  576. break;
  577. }
  578. if (tmp & POWER_STS_VDD5V_GT_VDDIO) {
  579. mxs_boot_valid_5v();
  580. break;
  581. } else {
  582. mxs_powerdown();
  583. break;
  584. }
  585. if (tmp & POWER_STS_PSWITCH_MASK) {
  586. mxs_batt_boot();
  587. break;
  588. }
  589. }
  590. }
  591. /**
  592. * mxs_5v_boot() - Configure the power block to boot from 5V input
  593. *
  594. * This function handles configuration of the power block when supplied by
  595. * a 5V input.
  596. */
  597. static void mxs_5v_boot(void)
  598. {
  599. struct mxs_power_regs *power_regs =
  600. (struct mxs_power_regs *)MXS_POWER_BASE;
  601. /*
  602. * NOTE: In original IMX-Bootlets, this also checks for VBUSVALID,
  603. * but their implementation always returns 1 so we omit it here.
  604. */
  605. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  606. mxs_boot_valid_5v();
  607. return;
  608. }
  609. early_delay(1000);
  610. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  611. mxs_boot_valid_5v();
  612. return;
  613. }
  614. mxs_handle_5v_conflict();
  615. }
  616. /**
  617. * mxs_init_batt_bo() - Configure battery brownout threshold
  618. *
  619. * This function configures the battery input brownout threshold. The value
  620. * at which the battery brownout happens is configured to 3.0V in the code.
  621. */
  622. static void mxs_init_batt_bo(void)
  623. {
  624. struct mxs_power_regs *power_regs =
  625. (struct mxs_power_regs *)MXS_POWER_BASE;
  626. /* Brownout at 3V */
  627. clrsetbits_le32(&power_regs->hw_power_battmonitor,
  628. POWER_BATTMONITOR_BRWNOUT_LVL_MASK,
  629. 15 << POWER_BATTMONITOR_BRWNOUT_LVL_OFFSET);
  630. writel(POWER_CTRL_BATT_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  631. writel(POWER_CTRL_ENIRQ_BATT_BO, &power_regs->hw_power_ctrl_clr);
  632. }
  633. /**
  634. * mxs_switch_vddd_to_dcdc_source() - Switch VDDD rail to DC-DC converter
  635. *
  636. * This function turns off the VDDD linear regulator and therefore makes
  637. * the VDDD rail be supplied only by the DC-DC converter.
  638. */
  639. static void mxs_switch_vddd_to_dcdc_source(void)
  640. {
  641. struct mxs_power_regs *power_regs =
  642. (struct mxs_power_regs *)MXS_POWER_BASE;
  643. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  644. POWER_VDDDCTRL_LINREG_OFFSET_MASK,
  645. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW);
  646. clrbits_le32(&power_regs->hw_power_vdddctrl,
  647. POWER_VDDDCTRL_DISABLE_FET | POWER_VDDDCTRL_ENABLE_LINREG |
  648. POWER_VDDDCTRL_DISABLE_STEPPING);
  649. }
  650. /**
  651. * mxs_power_configure_power_source() - Configure power block source
  652. *
  653. * This function is the core of the power configuration logic. The function
  654. * selects the power block input source and configures the whole power block
  655. * accordingly. After the configuration is complete and the system is stable
  656. * again, the function switches the CPU clock source back to PLL. Finally,
  657. * the function switches the voltage rails to DC-DC converter.
  658. */
  659. static void mxs_power_configure_power_source(void)
  660. {
  661. int batt_ready, batt_good;
  662. struct mxs_power_regs *power_regs =
  663. (struct mxs_power_regs *)MXS_POWER_BASE;
  664. struct mxs_lradc_regs *lradc_regs =
  665. (struct mxs_lradc_regs *)MXS_LRADC_BASE;
  666. mxs_src_power_init();
  667. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  668. batt_ready = mxs_is_batt_ready();
  669. if (batt_ready) {
  670. /* 5V source detected, good battery detected. */
  671. mxs_batt_boot();
  672. } else {
  673. batt_good = mxs_is_batt_good();
  674. if (!batt_good) {
  675. /* 5V source detected, bad battery detected. */
  676. writel(LRADC_CONVERSION_AUTOMATIC,
  677. &lradc_regs->hw_lradc_conversion_clr);
  678. clrbits_le32(&power_regs->hw_power_battmonitor,
  679. POWER_BATTMONITOR_BATT_VAL_MASK);
  680. }
  681. mxs_5v_boot();
  682. }
  683. } else {
  684. /* 5V not detected, booting from battery. */
  685. mxs_batt_boot();
  686. }
  687. mxs_power_clock2pll();
  688. mxs_init_batt_bo();
  689. mxs_switch_vddd_to_dcdc_source();
  690. #ifdef CONFIG_MX23
  691. /* Fire up the VDDMEM LinReg now that we're all set. */
  692. writel(POWER_VDDMEMCTRL_ENABLE_LINREG | POWER_VDDMEMCTRL_ENABLE_ILIMIT,
  693. &power_regs->hw_power_vddmemctrl);
  694. #endif
  695. }
  696. /**
  697. * mxs_enable_output_rail_protection() - Enable power rail protection
  698. *
  699. * This function enables overload protection on the power rails. This is
  700. * triggered if the power rails' voltage drops rapidly due to overload and
  701. * in such case, the supply to the powerrail is cut-off, protecting the
  702. * CPU from damage. Note that under such condition, the system will likely
  703. * crash or misbehave.
  704. */
  705. static void mxs_enable_output_rail_protection(void)
  706. {
  707. struct mxs_power_regs *power_regs =
  708. (struct mxs_power_regs *)MXS_POWER_BASE;
  709. writel(POWER_CTRL_VDDD_BO_IRQ | POWER_CTRL_VDDA_BO_IRQ |
  710. POWER_CTRL_VDDIO_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  711. setbits_le32(&power_regs->hw_power_vdddctrl,
  712. POWER_VDDDCTRL_PWDN_BRNOUT);
  713. setbits_le32(&power_regs->hw_power_vddactrl,
  714. POWER_VDDACTRL_PWDN_BRNOUT);
  715. setbits_le32(&power_regs->hw_power_vddioctrl,
  716. POWER_VDDIOCTRL_PWDN_BRNOUT);
  717. }
  718. /**
  719. * mxs_get_vddio_power_source_off() - Get VDDIO rail power source
  720. *
  721. * This function tests if the VDDIO rail is supplied by linear regulator
  722. * or by the DC-DC converter. Returns 1 if powered by linear regulator,
  723. * returns 0 if powered by the DC-DC converter.
  724. */
  725. static int mxs_get_vddio_power_source_off(void)
  726. {
  727. struct mxs_power_regs *power_regs =
  728. (struct mxs_power_regs *)MXS_POWER_BASE;
  729. uint32_t tmp;
  730. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  731. tmp = readl(&power_regs->hw_power_vddioctrl);
  732. if (tmp & POWER_VDDIOCTRL_DISABLE_FET) {
  733. if ((tmp & POWER_VDDIOCTRL_LINREG_OFFSET_MASK) ==
  734. POWER_VDDIOCTRL_LINREG_OFFSET_0STEPS) {
  735. return 1;
  736. }
  737. }
  738. if (!(readl(&power_regs->hw_power_5vctrl) &
  739. POWER_5VCTRL_ENABLE_DCDC)) {
  740. if ((tmp & POWER_VDDIOCTRL_LINREG_OFFSET_MASK) ==
  741. POWER_VDDIOCTRL_LINREG_OFFSET_0STEPS) {
  742. return 1;
  743. }
  744. }
  745. }
  746. return 0;
  747. }
  748. /**
  749. * mxs_get_vddd_power_source_off() - Get VDDD rail power source
  750. *
  751. * This function tests if the VDDD rail is supplied by linear regulator
  752. * or by the DC-DC converter. Returns 1 if powered by linear regulator,
  753. * returns 0 if powered by the DC-DC converter.
  754. */
  755. static int mxs_get_vddd_power_source_off(void)
  756. {
  757. struct mxs_power_regs *power_regs =
  758. (struct mxs_power_regs *)MXS_POWER_BASE;
  759. uint32_t tmp;
  760. tmp = readl(&power_regs->hw_power_vdddctrl);
  761. if (tmp & POWER_VDDDCTRL_DISABLE_FET) {
  762. if ((tmp & POWER_VDDDCTRL_LINREG_OFFSET_MASK) ==
  763. POWER_VDDDCTRL_LINREG_OFFSET_0STEPS) {
  764. return 1;
  765. }
  766. }
  767. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  768. if (!(readl(&power_regs->hw_power_5vctrl) &
  769. POWER_5VCTRL_ENABLE_DCDC)) {
  770. return 1;
  771. }
  772. }
  773. if (!(tmp & POWER_VDDDCTRL_ENABLE_LINREG)) {
  774. if ((tmp & POWER_VDDDCTRL_LINREG_OFFSET_MASK) ==
  775. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW) {
  776. return 1;
  777. }
  778. }
  779. return 0;
  780. }
  781. struct mxs_vddx_cfg {
  782. uint32_t *reg;
  783. uint8_t step_mV;
  784. uint16_t lowest_mV;
  785. int (*powered_by_linreg)(void);
  786. uint32_t trg_mask;
  787. uint32_t bo_irq;
  788. uint32_t bo_enirq;
  789. uint32_t bo_offset_mask;
  790. uint32_t bo_offset_offset;
  791. };
  792. static const struct mxs_vddx_cfg mxs_vddio_cfg = {
  793. .reg = &(((struct mxs_power_regs *)MXS_POWER_BASE)->
  794. hw_power_vddioctrl),
  795. #if defined(CONFIG_MX23)
  796. .step_mV = 25,
  797. #else
  798. .step_mV = 50,
  799. #endif
  800. .lowest_mV = 2800,
  801. .powered_by_linreg = mxs_get_vddio_power_source_off,
  802. .trg_mask = POWER_VDDIOCTRL_TRG_MASK,
  803. .bo_irq = POWER_CTRL_VDDIO_BO_IRQ,
  804. .bo_enirq = POWER_CTRL_ENIRQ_VDDIO_BO,
  805. .bo_offset_mask = POWER_VDDIOCTRL_BO_OFFSET_MASK,
  806. .bo_offset_offset = POWER_VDDIOCTRL_BO_OFFSET_OFFSET,
  807. };
  808. static const struct mxs_vddx_cfg mxs_vddd_cfg = {
  809. .reg = &(((struct mxs_power_regs *)MXS_POWER_BASE)->
  810. hw_power_vdddctrl),
  811. .step_mV = 25,
  812. .lowest_mV = 800,
  813. .powered_by_linreg = mxs_get_vddd_power_source_off,
  814. .trg_mask = POWER_VDDDCTRL_TRG_MASK,
  815. .bo_irq = POWER_CTRL_VDDD_BO_IRQ,
  816. .bo_enirq = POWER_CTRL_ENIRQ_VDDD_BO,
  817. .bo_offset_mask = POWER_VDDDCTRL_BO_OFFSET_MASK,
  818. .bo_offset_offset = POWER_VDDDCTRL_BO_OFFSET_OFFSET,
  819. };
  820. #ifdef CONFIG_MX23
  821. static const struct mxs_vddx_cfg mxs_vddmem_cfg = {
  822. .reg = &(((struct mxs_power_regs *)MXS_POWER_BASE)->
  823. hw_power_vddmemctrl),
  824. .step_mV = 50,
  825. .lowest_mV = 1700,
  826. .powered_by_linreg = NULL,
  827. .trg_mask = POWER_VDDMEMCTRL_TRG_MASK,
  828. .bo_irq = 0,
  829. .bo_enirq = 0,
  830. .bo_offset_mask = 0,
  831. .bo_offset_offset = 0,
  832. };
  833. #endif
  834. /**
  835. * mxs_power_set_vddx() - Configure voltage on DC-DC converter rail
  836. * @cfg: Configuration data of the DC-DC converter rail
  837. * @new_target: New target voltage of the DC-DC converter rail
  838. * @new_brownout: New brownout trigger voltage
  839. *
  840. * This function configures the output voltage on the DC-DC converter rail.
  841. * The rail is selected by the @cfg argument. The new voltage target is
  842. * selected by the @new_target and the voltage is specified in mV. The
  843. * new brownout value is selected by the @new_brownout argument and the
  844. * value is also in mV.
  845. */
  846. static void mxs_power_set_vddx(const struct mxs_vddx_cfg *cfg,
  847. uint32_t new_target, uint32_t new_brownout)
  848. {
  849. struct mxs_power_regs *power_regs =
  850. (struct mxs_power_regs *)MXS_POWER_BASE;
  851. uint32_t cur_target, diff, bo_int = 0;
  852. uint32_t powered_by_linreg = 0;
  853. int adjust_up, tmp;
  854. new_brownout = DIV_ROUND(new_target - new_brownout, cfg->step_mV);
  855. cur_target = readl(cfg->reg);
  856. cur_target &= cfg->trg_mask;
  857. cur_target *= cfg->step_mV;
  858. cur_target += cfg->lowest_mV;
  859. adjust_up = new_target > cur_target;
  860. if (cfg->powered_by_linreg)
  861. powered_by_linreg = cfg->powered_by_linreg();
  862. if (adjust_up && cfg->bo_irq) {
  863. if (powered_by_linreg) {
  864. bo_int = readl(cfg->reg);
  865. clrbits_le32(cfg->reg, cfg->bo_enirq);
  866. }
  867. setbits_le32(cfg->reg, cfg->bo_offset_mask);
  868. }
  869. do {
  870. if (abs(new_target - cur_target) > 100) {
  871. if (adjust_up)
  872. diff = cur_target + 100;
  873. else
  874. diff = cur_target - 100;
  875. } else {
  876. diff = new_target;
  877. }
  878. diff -= cfg->lowest_mV;
  879. diff /= cfg->step_mV;
  880. clrsetbits_le32(cfg->reg, cfg->trg_mask, diff);
  881. if (powered_by_linreg ||
  882. (readl(&power_regs->hw_power_sts) &
  883. POWER_STS_VDD5V_GT_VDDIO))
  884. early_delay(500);
  885. else {
  886. for (;;) {
  887. tmp = readl(&power_regs->hw_power_sts);
  888. if (tmp & POWER_STS_DC_OK)
  889. break;
  890. }
  891. }
  892. cur_target = readl(cfg->reg);
  893. cur_target &= cfg->trg_mask;
  894. cur_target *= cfg->step_mV;
  895. cur_target += cfg->lowest_mV;
  896. } while (new_target > cur_target);
  897. if (cfg->bo_irq) {
  898. if (adjust_up && powered_by_linreg) {
  899. writel(cfg->bo_irq, &power_regs->hw_power_ctrl_clr);
  900. if (bo_int & cfg->bo_enirq)
  901. setbits_le32(cfg->reg, cfg->bo_enirq);
  902. }
  903. clrsetbits_le32(cfg->reg, cfg->bo_offset_mask,
  904. new_brownout << cfg->bo_offset_offset);
  905. }
  906. }
  907. /**
  908. * mxs_setup_batt_detect() - Start the battery voltage measurement logic
  909. *
  910. * This function starts and configures the LRADC block. This allows the
  911. * power initialization code to measure battery voltage and based on this
  912. * knowledge, decide whether to boot at all, boot from battery or boot
  913. * from 5V input.
  914. */
  915. static void mxs_setup_batt_detect(void)
  916. {
  917. mxs_lradc_init();
  918. mxs_lradc_enable_batt_measurement();
  919. early_delay(10);
  920. }
  921. /**
  922. * mxs_ungate_power() - Ungate the POWER block
  923. *
  924. * This function ungates clock to the power block. In case the power block
  925. * was still gated at this point, it will not be possible to configure the
  926. * block and therefore the power initialization would fail. This function
  927. * is only needed on i.MX233, on i.MX28 the power block is always ungated.
  928. */
  929. static void mxs_ungate_power(void)
  930. {
  931. #ifdef CONFIG_MX23
  932. struct mxs_power_regs *power_regs =
  933. (struct mxs_power_regs *)MXS_POWER_BASE;
  934. writel(POWER_CTRL_CLKGATE, &power_regs->hw_power_ctrl_clr);
  935. #endif
  936. }
  937. /**
  938. * mxs_power_init() - The power block init main function
  939. *
  940. * This function calls all the power block initialization functions in
  941. * proper sequence to start the power block.
  942. */
  943. void mxs_power_init(void)
  944. {
  945. struct mxs_power_regs *power_regs =
  946. (struct mxs_power_regs *)MXS_POWER_BASE;
  947. mxs_ungate_power();
  948. mxs_power_clock2xtal();
  949. mxs_power_set_auto_restart();
  950. mxs_power_set_linreg();
  951. mxs_power_setup_5v_detect();
  952. mxs_setup_batt_detect();
  953. mxs_power_configure_power_source();
  954. mxs_enable_output_rail_protection();
  955. mxs_power_set_vddx(&mxs_vddio_cfg, 3300, 3150);
  956. mxs_power_set_vddx(&mxs_vddd_cfg, 1500, 1000);
  957. #ifdef CONFIG_MX23
  958. mxs_power_set_vddx(&mxs_vddmem_cfg, 2500, 1700);
  959. #endif
  960. writel(POWER_CTRL_VDDD_BO_IRQ | POWER_CTRL_VDDA_BO_IRQ |
  961. POWER_CTRL_VDDIO_BO_IRQ | POWER_CTRL_VDD5V_DROOP_IRQ |
  962. POWER_CTRL_VBUS_VALID_IRQ | POWER_CTRL_BATT_BO_IRQ |
  963. POWER_CTRL_DCDC4P2_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  964. writel(POWER_5VCTRL_PWDN_5VBRNOUT, &power_regs->hw_power_5vctrl_set);
  965. early_delay(1000);
  966. }
  967. #ifdef CONFIG_SPL_MXS_PSWITCH_WAIT
  968. /**
  969. * mxs_power_wait_pswitch() - Wait for power switch to be pressed
  970. *
  971. * This function waits until the power-switch was pressed to start booting
  972. * the board.
  973. */
  974. void mxs_power_wait_pswitch(void)
  975. {
  976. struct mxs_power_regs *power_regs =
  977. (struct mxs_power_regs *)MXS_POWER_BASE;
  978. while (!(readl(&power_regs->hw_power_sts) & POWER_STS_PSWITCH_MASK))
  979. ;
  980. }
  981. #endif