spl_power_init.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <config.h>
  27. #include <asm/io.h>
  28. #include <asm/arch/imx-regs.h>
  29. #include "mxs_init.h"
  30. static void mxs_power_clock2xtal(void)
  31. {
  32. struct mxs_clkctrl_regs *clkctrl_regs =
  33. (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
  34. /* Set XTAL as CPU reference clock */
  35. writel(CLKCTRL_CLKSEQ_BYPASS_CPU,
  36. &clkctrl_regs->hw_clkctrl_clkseq_set);
  37. }
  38. static void mxs_power_clock2pll(void)
  39. {
  40. struct mxs_clkctrl_regs *clkctrl_regs =
  41. (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
  42. setbits_le32(&clkctrl_regs->hw_clkctrl_pll0ctrl0,
  43. CLKCTRL_PLL0CTRL0_POWER);
  44. early_delay(100);
  45. setbits_le32(&clkctrl_regs->hw_clkctrl_clkseq,
  46. CLKCTRL_CLKSEQ_BYPASS_CPU);
  47. }
  48. static void mxs_power_clear_auto_restart(void)
  49. {
  50. struct mxs_rtc_regs *rtc_regs =
  51. (struct mxs_rtc_regs *)MXS_RTC_BASE;
  52. writel(RTC_CTRL_SFTRST, &rtc_regs->hw_rtc_ctrl_clr);
  53. while (readl(&rtc_regs->hw_rtc_ctrl) & RTC_CTRL_SFTRST)
  54. ;
  55. writel(RTC_CTRL_CLKGATE, &rtc_regs->hw_rtc_ctrl_clr);
  56. while (readl(&rtc_regs->hw_rtc_ctrl) & RTC_CTRL_CLKGATE)
  57. ;
  58. /*
  59. * Due to the hardware design bug of mx28 EVK-A
  60. * we need to set the AUTO_RESTART bit.
  61. */
  62. if (readl(&rtc_regs->hw_rtc_persistent0) & RTC_PERSISTENT0_AUTO_RESTART)
  63. return;
  64. while (readl(&rtc_regs->hw_rtc_stat) & RTC_STAT_NEW_REGS_MASK)
  65. ;
  66. setbits_le32(&rtc_regs->hw_rtc_persistent0,
  67. RTC_PERSISTENT0_AUTO_RESTART);
  68. writel(RTC_CTRL_FORCE_UPDATE, &rtc_regs->hw_rtc_ctrl_set);
  69. writel(RTC_CTRL_FORCE_UPDATE, &rtc_regs->hw_rtc_ctrl_clr);
  70. while (readl(&rtc_regs->hw_rtc_stat) & RTC_STAT_NEW_REGS_MASK)
  71. ;
  72. while (readl(&rtc_regs->hw_rtc_stat) & RTC_STAT_STALE_REGS_MASK)
  73. ;
  74. }
  75. static void mxs_power_set_linreg(void)
  76. {
  77. struct mxs_power_regs *power_regs =
  78. (struct mxs_power_regs *)MXS_POWER_BASE;
  79. /* Set linear regulator 25mV below switching converter */
  80. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  81. POWER_VDDDCTRL_LINREG_OFFSET_MASK,
  82. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW);
  83. clrsetbits_le32(&power_regs->hw_power_vddactrl,
  84. POWER_VDDACTRL_LINREG_OFFSET_MASK,
  85. POWER_VDDACTRL_LINREG_OFFSET_1STEPS_BELOW);
  86. clrsetbits_le32(&power_regs->hw_power_vddioctrl,
  87. POWER_VDDIOCTRL_LINREG_OFFSET_MASK,
  88. POWER_VDDIOCTRL_LINREG_OFFSET_1STEPS_BELOW);
  89. }
  90. static int mxs_get_batt_volt(void)
  91. {
  92. struct mxs_power_regs *power_regs =
  93. (struct mxs_power_regs *)MXS_POWER_BASE;
  94. uint32_t volt = readl(&power_regs->hw_power_battmonitor);
  95. volt &= POWER_BATTMONITOR_BATT_VAL_MASK;
  96. volt >>= POWER_BATTMONITOR_BATT_VAL_OFFSET;
  97. volt *= 8;
  98. return volt;
  99. }
  100. static int mxs_is_batt_ready(void)
  101. {
  102. return (mxs_get_batt_volt() >= 3600);
  103. }
  104. static int mxs_is_batt_good(void)
  105. {
  106. struct mxs_power_regs *power_regs =
  107. (struct mxs_power_regs *)MXS_POWER_BASE;
  108. uint32_t volt = mxs_get_batt_volt();
  109. if ((volt >= 2400) && (volt <= 4300))
  110. return 1;
  111. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  112. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  113. 0x3 << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  114. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  115. &power_regs->hw_power_5vctrl_clr);
  116. clrsetbits_le32(&power_regs->hw_power_charge,
  117. POWER_CHARGE_STOP_ILIMIT_MASK | POWER_CHARGE_BATTCHRG_I_MASK,
  118. POWER_CHARGE_STOP_ILIMIT_10MA | 0x3);
  119. writel(POWER_CHARGE_PWD_BATTCHRG, &power_regs->hw_power_charge_clr);
  120. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  121. &power_regs->hw_power_5vctrl_clr);
  122. early_delay(500000);
  123. volt = mxs_get_batt_volt();
  124. if (volt >= 3500)
  125. return 0;
  126. if (volt >= 2400)
  127. return 1;
  128. writel(POWER_CHARGE_STOP_ILIMIT_MASK | POWER_CHARGE_BATTCHRG_I_MASK,
  129. &power_regs->hw_power_charge_clr);
  130. writel(POWER_CHARGE_PWD_BATTCHRG, &power_regs->hw_power_charge_set);
  131. return 0;
  132. }
  133. static void mxs_power_setup_5v_detect(void)
  134. {
  135. struct mxs_power_regs *power_regs =
  136. (struct mxs_power_regs *)MXS_POWER_BASE;
  137. /* Start 5V detection */
  138. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  139. POWER_5VCTRL_VBUSVALID_TRSH_MASK,
  140. POWER_5VCTRL_VBUSVALID_TRSH_4V4 |
  141. POWER_5VCTRL_PWRUP_VBUS_CMPS);
  142. }
  143. static void mxs_src_power_init(void)
  144. {
  145. struct mxs_power_regs *power_regs =
  146. (struct mxs_power_regs *)MXS_POWER_BASE;
  147. /* Improve efficieny and reduce transient ripple */
  148. writel(POWER_LOOPCTRL_TOGGLE_DIF | POWER_LOOPCTRL_EN_CM_HYST |
  149. POWER_LOOPCTRL_EN_DF_HYST, &power_regs->hw_power_loopctrl_set);
  150. clrsetbits_le32(&power_regs->hw_power_dclimits,
  151. POWER_DCLIMITS_POSLIMIT_BUCK_MASK,
  152. 0x30 << POWER_DCLIMITS_POSLIMIT_BUCK_OFFSET);
  153. setbits_le32(&power_regs->hw_power_battmonitor,
  154. POWER_BATTMONITOR_EN_BATADJ);
  155. /* Increase the RCSCALE level for quick DCDC response to dynamic load */
  156. clrsetbits_le32(&power_regs->hw_power_loopctrl,
  157. POWER_LOOPCTRL_EN_RCSCALE_MASK,
  158. POWER_LOOPCTRL_RCSCALE_THRESH |
  159. POWER_LOOPCTRL_EN_RCSCALE_8X);
  160. clrsetbits_le32(&power_regs->hw_power_minpwr,
  161. POWER_MINPWR_HALFFETS, POWER_MINPWR_DOUBLE_FETS);
  162. /* 5V to battery handoff ... FIXME */
  163. setbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  164. early_delay(30);
  165. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  166. }
  167. static void mxs_power_init_4p2_params(void)
  168. {
  169. struct mxs_power_regs *power_regs =
  170. (struct mxs_power_regs *)MXS_POWER_BASE;
  171. /* Setup 4P2 parameters */
  172. clrsetbits_le32(&power_regs->hw_power_dcdc4p2,
  173. POWER_DCDC4P2_CMPTRIP_MASK | POWER_DCDC4P2_TRG_MASK,
  174. POWER_DCDC4P2_TRG_4V2 | (31 << POWER_DCDC4P2_CMPTRIP_OFFSET));
  175. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  176. POWER_5VCTRL_HEADROOM_ADJ_MASK,
  177. 0x4 << POWER_5VCTRL_HEADROOM_ADJ_OFFSET);
  178. clrsetbits_le32(&power_regs->hw_power_dcdc4p2,
  179. POWER_DCDC4P2_DROPOUT_CTRL_MASK,
  180. POWER_DCDC4P2_DROPOUT_CTRL_100MV |
  181. POWER_DCDC4P2_DROPOUT_CTRL_SRC_SEL);
  182. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  183. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  184. 0x3f << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  185. }
  186. static void mxs_enable_4p2_dcdc_input(int xfer)
  187. {
  188. struct mxs_power_regs *power_regs =
  189. (struct mxs_power_regs *)MXS_POWER_BASE;
  190. uint32_t tmp, vbus_thresh, vbus_5vdetect, pwd_bo;
  191. uint32_t prev_5v_brnout, prev_5v_droop;
  192. prev_5v_brnout = readl(&power_regs->hw_power_5vctrl) &
  193. POWER_5VCTRL_PWDN_5VBRNOUT;
  194. prev_5v_droop = readl(&power_regs->hw_power_ctrl) &
  195. POWER_CTRL_ENIRQ_VDD5V_DROOP;
  196. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_PWDN_5VBRNOUT);
  197. writel(POWER_RESET_UNLOCK_KEY | POWER_RESET_PWD_OFF,
  198. &power_regs->hw_power_reset);
  199. clrbits_le32(&power_regs->hw_power_ctrl, POWER_CTRL_ENIRQ_VDD5V_DROOP);
  200. if (xfer && (readl(&power_regs->hw_power_5vctrl) &
  201. POWER_5VCTRL_ENABLE_DCDC)) {
  202. return;
  203. }
  204. /*
  205. * Recording orignal values that will be modified temporarlily
  206. * to handle a chip bug. See chip errata for CQ ENGR00115837
  207. */
  208. tmp = readl(&power_regs->hw_power_5vctrl);
  209. vbus_thresh = tmp & POWER_5VCTRL_VBUSVALID_TRSH_MASK;
  210. vbus_5vdetect = tmp & POWER_5VCTRL_VBUSVALID_5VDETECT;
  211. pwd_bo = readl(&power_regs->hw_power_minpwr) & POWER_MINPWR_PWD_BO;
  212. /*
  213. * Disable mechanisms that get erroneously tripped by when setting
  214. * the DCDC4P2 EN_DCDC
  215. */
  216. clrbits_le32(&power_regs->hw_power_5vctrl,
  217. POWER_5VCTRL_VBUSVALID_5VDETECT |
  218. POWER_5VCTRL_VBUSVALID_TRSH_MASK);
  219. writel(POWER_MINPWR_PWD_BO, &power_regs->hw_power_minpwr_set);
  220. if (xfer) {
  221. setbits_le32(&power_regs->hw_power_5vctrl,
  222. POWER_5VCTRL_DCDC_XFER);
  223. early_delay(20);
  224. clrbits_le32(&power_regs->hw_power_5vctrl,
  225. POWER_5VCTRL_DCDC_XFER);
  226. setbits_le32(&power_regs->hw_power_5vctrl,
  227. POWER_5VCTRL_ENABLE_DCDC);
  228. } else {
  229. setbits_le32(&power_regs->hw_power_dcdc4p2,
  230. POWER_DCDC4P2_ENABLE_DCDC);
  231. }
  232. early_delay(25);
  233. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  234. POWER_5VCTRL_VBUSVALID_TRSH_MASK, vbus_thresh);
  235. if (vbus_5vdetect)
  236. writel(vbus_5vdetect, &power_regs->hw_power_5vctrl_set);
  237. if (!pwd_bo)
  238. clrbits_le32(&power_regs->hw_power_minpwr, POWER_MINPWR_PWD_BO);
  239. while (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VBUS_VALID_IRQ)
  240. writel(POWER_CTRL_VBUS_VALID_IRQ,
  241. &power_regs->hw_power_ctrl_clr);
  242. if (prev_5v_brnout) {
  243. writel(POWER_5VCTRL_PWDN_5VBRNOUT,
  244. &power_regs->hw_power_5vctrl_set);
  245. writel(POWER_RESET_UNLOCK_KEY,
  246. &power_regs->hw_power_reset);
  247. } else {
  248. writel(POWER_5VCTRL_PWDN_5VBRNOUT,
  249. &power_regs->hw_power_5vctrl_clr);
  250. writel(POWER_RESET_UNLOCK_KEY | POWER_RESET_PWD_OFF,
  251. &power_regs->hw_power_reset);
  252. }
  253. while (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VDD5V_DROOP_IRQ)
  254. writel(POWER_CTRL_VDD5V_DROOP_IRQ,
  255. &power_regs->hw_power_ctrl_clr);
  256. if (prev_5v_droop)
  257. clrbits_le32(&power_regs->hw_power_ctrl,
  258. POWER_CTRL_ENIRQ_VDD5V_DROOP);
  259. else
  260. setbits_le32(&power_regs->hw_power_ctrl,
  261. POWER_CTRL_ENIRQ_VDD5V_DROOP);
  262. }
  263. static void mxs_power_init_4p2_regulator(void)
  264. {
  265. struct mxs_power_regs *power_regs =
  266. (struct mxs_power_regs *)MXS_POWER_BASE;
  267. uint32_t tmp, tmp2;
  268. setbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_ENABLE_4P2);
  269. writel(POWER_CHARGE_ENABLE_LOAD, &power_regs->hw_power_charge_set);
  270. writel(POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  271. &power_regs->hw_power_5vctrl_clr);
  272. clrbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_TRG_MASK);
  273. /* Power up the 4p2 rail and logic/control */
  274. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  275. &power_regs->hw_power_5vctrl_clr);
  276. /*
  277. * Start charging up the 4p2 capacitor. We ramp of this charge
  278. * gradually to avoid large inrush current from the 5V cable which can
  279. * cause transients/problems
  280. */
  281. mxs_enable_4p2_dcdc_input(0);
  282. if (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VBUS_VALID_IRQ) {
  283. /*
  284. * If we arrived here, we were unable to recover from mx23 chip
  285. * errata 5837. 4P2 is disabled and sufficient battery power is
  286. * not present. Exiting to not enable DCDC power during 5V
  287. * connected state.
  288. */
  289. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  290. POWER_DCDC4P2_ENABLE_DCDC);
  291. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  292. &power_regs->hw_power_5vctrl_set);
  293. hang();
  294. }
  295. /*
  296. * Here we set the 4p2 brownout level to something very close to 4.2V.
  297. * We then check the brownout status. If the brownout status is false,
  298. * the voltage is already close to the target voltage of 4.2V so we
  299. * can go ahead and set the 4P2 current limit to our max target limit.
  300. * If the brownout status is true, we need to ramp us the current limit
  301. * so that we don't cause large inrush current issues. We step up the
  302. * current limit until the brownout status is false or until we've
  303. * reached our maximum defined 4p2 current limit.
  304. */
  305. clrsetbits_le32(&power_regs->hw_power_dcdc4p2,
  306. POWER_DCDC4P2_BO_MASK,
  307. 22 << POWER_DCDC4P2_BO_OFFSET); /* 4.15V */
  308. if (!(readl(&power_regs->hw_power_sts) & POWER_STS_DCDC_4P2_BO)) {
  309. setbits_le32(&power_regs->hw_power_5vctrl,
  310. 0x3f << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  311. } else {
  312. tmp = (readl(&power_regs->hw_power_5vctrl) &
  313. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK) >>
  314. POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET;
  315. while (tmp < 0x3f) {
  316. if (!(readl(&power_regs->hw_power_sts) &
  317. POWER_STS_DCDC_4P2_BO)) {
  318. tmp = readl(&power_regs->hw_power_5vctrl);
  319. tmp |= POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK;
  320. early_delay(100);
  321. writel(tmp, &power_regs->hw_power_5vctrl);
  322. break;
  323. } else {
  324. tmp++;
  325. tmp2 = readl(&power_regs->hw_power_5vctrl);
  326. tmp2 &= ~POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK;
  327. tmp2 |= tmp <<
  328. POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET;
  329. writel(tmp2, &power_regs->hw_power_5vctrl);
  330. early_delay(100);
  331. }
  332. }
  333. }
  334. clrbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_BO_MASK);
  335. writel(POWER_CTRL_DCDC4P2_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  336. }
  337. static void mxs_power_init_dcdc_4p2_source(void)
  338. {
  339. struct mxs_power_regs *power_regs =
  340. (struct mxs_power_regs *)MXS_POWER_BASE;
  341. if (!(readl(&power_regs->hw_power_dcdc4p2) &
  342. POWER_DCDC4P2_ENABLE_DCDC)) {
  343. hang();
  344. }
  345. mxs_enable_4p2_dcdc_input(1);
  346. if (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VBUS_VALID_IRQ) {
  347. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  348. POWER_DCDC4P2_ENABLE_DCDC);
  349. writel(POWER_5VCTRL_ENABLE_DCDC,
  350. &power_regs->hw_power_5vctrl_clr);
  351. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  352. &power_regs->hw_power_5vctrl_set);
  353. }
  354. }
  355. static void mxs_power_enable_4p2(void)
  356. {
  357. struct mxs_power_regs *power_regs =
  358. (struct mxs_power_regs *)MXS_POWER_BASE;
  359. uint32_t vdddctrl, vddactrl, vddioctrl;
  360. uint32_t tmp;
  361. vdddctrl = readl(&power_regs->hw_power_vdddctrl);
  362. vddactrl = readl(&power_regs->hw_power_vddactrl);
  363. vddioctrl = readl(&power_regs->hw_power_vddioctrl);
  364. setbits_le32(&power_regs->hw_power_vdddctrl,
  365. POWER_VDDDCTRL_DISABLE_FET | POWER_VDDDCTRL_ENABLE_LINREG |
  366. POWER_VDDDCTRL_PWDN_BRNOUT);
  367. setbits_le32(&power_regs->hw_power_vddactrl,
  368. POWER_VDDACTRL_DISABLE_FET | POWER_VDDACTRL_ENABLE_LINREG |
  369. POWER_VDDACTRL_PWDN_BRNOUT);
  370. setbits_le32(&power_regs->hw_power_vddioctrl,
  371. POWER_VDDIOCTRL_DISABLE_FET | POWER_VDDIOCTRL_PWDN_BRNOUT);
  372. mxs_power_init_4p2_params();
  373. mxs_power_init_4p2_regulator();
  374. /* Shutdown battery (none present) */
  375. if (!mxs_is_batt_ready()) {
  376. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  377. POWER_DCDC4P2_BO_MASK);
  378. writel(POWER_CTRL_DCDC4P2_BO_IRQ,
  379. &power_regs->hw_power_ctrl_clr);
  380. writel(POWER_CTRL_ENIRQ_DCDC4P2_BO,
  381. &power_regs->hw_power_ctrl_clr);
  382. }
  383. mxs_power_init_dcdc_4p2_source();
  384. writel(vdddctrl, &power_regs->hw_power_vdddctrl);
  385. early_delay(20);
  386. writel(vddactrl, &power_regs->hw_power_vddactrl);
  387. early_delay(20);
  388. writel(vddioctrl, &power_regs->hw_power_vddioctrl);
  389. /*
  390. * Check if FET is enabled on either powerout and if so,
  391. * disable load.
  392. */
  393. tmp = 0;
  394. tmp |= !(readl(&power_regs->hw_power_vdddctrl) &
  395. POWER_VDDDCTRL_DISABLE_FET);
  396. tmp |= !(readl(&power_regs->hw_power_vddactrl) &
  397. POWER_VDDACTRL_DISABLE_FET);
  398. tmp |= !(readl(&power_regs->hw_power_vddioctrl) &
  399. POWER_VDDIOCTRL_DISABLE_FET);
  400. if (tmp)
  401. writel(POWER_CHARGE_ENABLE_LOAD,
  402. &power_regs->hw_power_charge_clr);
  403. }
  404. static void mxs_boot_valid_5v(void)
  405. {
  406. struct mxs_power_regs *power_regs =
  407. (struct mxs_power_regs *)MXS_POWER_BASE;
  408. /*
  409. * Use VBUSVALID level instead of VDD5V_GT_VDDIO level to trigger a 5V
  410. * disconnect event. FIXME
  411. */
  412. writel(POWER_5VCTRL_VBUSVALID_5VDETECT,
  413. &power_regs->hw_power_5vctrl_set);
  414. /* Configure polarity to check for 5V disconnection. */
  415. writel(POWER_CTRL_POLARITY_VBUSVALID |
  416. POWER_CTRL_POLARITY_VDD5V_GT_VDDIO,
  417. &power_regs->hw_power_ctrl_clr);
  418. writel(POWER_CTRL_VBUS_VALID_IRQ | POWER_CTRL_VDD5V_GT_VDDIO_IRQ,
  419. &power_regs->hw_power_ctrl_clr);
  420. mxs_power_enable_4p2();
  421. }
  422. static void mxs_powerdown(void)
  423. {
  424. struct mxs_power_regs *power_regs =
  425. (struct mxs_power_regs *)MXS_POWER_BASE;
  426. writel(POWER_RESET_UNLOCK_KEY, &power_regs->hw_power_reset);
  427. writel(POWER_RESET_UNLOCK_KEY | POWER_RESET_PWD_OFF,
  428. &power_regs->hw_power_reset);
  429. }
  430. static void mxs_batt_boot(void)
  431. {
  432. struct mxs_power_regs *power_regs =
  433. (struct mxs_power_regs *)MXS_POWER_BASE;
  434. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_PWDN_5VBRNOUT);
  435. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_ENABLE_DCDC);
  436. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  437. POWER_DCDC4P2_ENABLE_DCDC | POWER_DCDC4P2_ENABLE_4P2);
  438. writel(POWER_CHARGE_ENABLE_LOAD, &power_regs->hw_power_charge_clr);
  439. /* 5V to battery handoff. */
  440. setbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  441. early_delay(30);
  442. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  443. writel(POWER_CTRL_ENIRQ_DCDC4P2_BO, &power_regs->hw_power_ctrl_clr);
  444. clrsetbits_le32(&power_regs->hw_power_minpwr,
  445. POWER_MINPWR_HALFFETS, POWER_MINPWR_DOUBLE_FETS);
  446. mxs_power_set_linreg();
  447. clrbits_le32(&power_regs->hw_power_vdddctrl,
  448. POWER_VDDDCTRL_DISABLE_FET | POWER_VDDDCTRL_ENABLE_LINREG);
  449. clrbits_le32(&power_regs->hw_power_vddactrl,
  450. POWER_VDDACTRL_DISABLE_FET | POWER_VDDACTRL_ENABLE_LINREG);
  451. clrbits_le32(&power_regs->hw_power_vddioctrl,
  452. POWER_VDDIOCTRL_DISABLE_FET);
  453. setbits_le32(&power_regs->hw_power_5vctrl,
  454. POWER_5VCTRL_PWD_CHARGE_4P2_MASK);
  455. setbits_le32(&power_regs->hw_power_5vctrl,
  456. POWER_5VCTRL_ENABLE_DCDC);
  457. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  458. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  459. 0x8 << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  460. }
  461. static void mxs_handle_5v_conflict(void)
  462. {
  463. struct mxs_power_regs *power_regs =
  464. (struct mxs_power_regs *)MXS_POWER_BASE;
  465. uint32_t tmp;
  466. setbits_le32(&power_regs->hw_power_vddioctrl,
  467. POWER_VDDIOCTRL_BO_OFFSET_MASK);
  468. for (;;) {
  469. tmp = readl(&power_regs->hw_power_sts);
  470. if (tmp & POWER_STS_VDDIO_BO) {
  471. /*
  472. * VDDIO has a brownout, then the VDD5V_GT_VDDIO becomes
  473. * unreliable
  474. */
  475. mxs_powerdown();
  476. break;
  477. }
  478. if (tmp & POWER_STS_VDD5V_GT_VDDIO) {
  479. mxs_boot_valid_5v();
  480. break;
  481. } else {
  482. mxs_powerdown();
  483. break;
  484. }
  485. if (tmp & POWER_STS_PSWITCH_MASK) {
  486. mxs_batt_boot();
  487. break;
  488. }
  489. }
  490. }
  491. static void mxs_5v_boot(void)
  492. {
  493. struct mxs_power_regs *power_regs =
  494. (struct mxs_power_regs *)MXS_POWER_BASE;
  495. /*
  496. * NOTE: In original IMX-Bootlets, this also checks for VBUSVALID,
  497. * but their implementation always returns 1 so we omit it here.
  498. */
  499. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  500. mxs_boot_valid_5v();
  501. return;
  502. }
  503. early_delay(1000);
  504. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  505. mxs_boot_valid_5v();
  506. return;
  507. }
  508. mxs_handle_5v_conflict();
  509. }
  510. static void mxs_init_batt_bo(void)
  511. {
  512. struct mxs_power_regs *power_regs =
  513. (struct mxs_power_regs *)MXS_POWER_BASE;
  514. /* Brownout at 3V */
  515. clrsetbits_le32(&power_regs->hw_power_battmonitor,
  516. POWER_BATTMONITOR_BRWNOUT_LVL_MASK,
  517. 15 << POWER_BATTMONITOR_BRWNOUT_LVL_OFFSET);
  518. writel(POWER_CTRL_BATT_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  519. writel(POWER_CTRL_ENIRQ_BATT_BO, &power_regs->hw_power_ctrl_clr);
  520. }
  521. static void mxs_switch_vddd_to_dcdc_source(void)
  522. {
  523. struct mxs_power_regs *power_regs =
  524. (struct mxs_power_regs *)MXS_POWER_BASE;
  525. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  526. POWER_VDDDCTRL_LINREG_OFFSET_MASK,
  527. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW);
  528. clrbits_le32(&power_regs->hw_power_vdddctrl,
  529. POWER_VDDDCTRL_DISABLE_FET | POWER_VDDDCTRL_ENABLE_LINREG |
  530. POWER_VDDDCTRL_DISABLE_STEPPING);
  531. }
  532. static void mxs_power_configure_power_source(void)
  533. {
  534. int batt_ready, batt_good;
  535. struct mxs_power_regs *power_regs =
  536. (struct mxs_power_regs *)MXS_POWER_BASE;
  537. struct mxs_lradc_regs *lradc_regs =
  538. (struct mxs_lradc_regs *)MXS_LRADC_BASE;
  539. mxs_src_power_init();
  540. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  541. batt_ready = mxs_is_batt_ready();
  542. if (batt_ready) {
  543. /* 5V source detected, good battery detected. */
  544. mxs_batt_boot();
  545. } else {
  546. batt_good = mxs_is_batt_good();
  547. if (!batt_good) {
  548. /* 5V source detected, bad battery detected. */
  549. writel(LRADC_CONVERSION_AUTOMATIC,
  550. &lradc_regs->hw_lradc_conversion_clr);
  551. clrbits_le32(&power_regs->hw_power_battmonitor,
  552. POWER_BATTMONITOR_BATT_VAL_MASK);
  553. }
  554. mxs_5v_boot();
  555. }
  556. } else {
  557. /* 5V not detected, booting from battery. */
  558. mxs_batt_boot();
  559. }
  560. mxs_power_clock2pll();
  561. mxs_init_batt_bo();
  562. mxs_switch_vddd_to_dcdc_source();
  563. }
  564. static void mxs_enable_output_rail_protection(void)
  565. {
  566. struct mxs_power_regs *power_regs =
  567. (struct mxs_power_regs *)MXS_POWER_BASE;
  568. writel(POWER_CTRL_VDDD_BO_IRQ | POWER_CTRL_VDDA_BO_IRQ |
  569. POWER_CTRL_VDDIO_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  570. setbits_le32(&power_regs->hw_power_vdddctrl,
  571. POWER_VDDDCTRL_PWDN_BRNOUT);
  572. setbits_le32(&power_regs->hw_power_vddactrl,
  573. POWER_VDDACTRL_PWDN_BRNOUT);
  574. setbits_le32(&power_regs->hw_power_vddioctrl,
  575. POWER_VDDIOCTRL_PWDN_BRNOUT);
  576. }
  577. static int mxs_get_vddio_power_source_off(void)
  578. {
  579. struct mxs_power_regs *power_regs =
  580. (struct mxs_power_regs *)MXS_POWER_BASE;
  581. uint32_t tmp;
  582. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  583. tmp = readl(&power_regs->hw_power_vddioctrl);
  584. if (tmp & POWER_VDDIOCTRL_DISABLE_FET) {
  585. if ((tmp & POWER_VDDIOCTRL_LINREG_OFFSET_MASK) ==
  586. POWER_VDDIOCTRL_LINREG_OFFSET_0STEPS) {
  587. return 1;
  588. }
  589. }
  590. if (!(readl(&power_regs->hw_power_5vctrl) &
  591. POWER_5VCTRL_ENABLE_DCDC)) {
  592. if ((tmp & POWER_VDDIOCTRL_LINREG_OFFSET_MASK) ==
  593. POWER_VDDIOCTRL_LINREG_OFFSET_0STEPS) {
  594. return 1;
  595. }
  596. }
  597. }
  598. return 0;
  599. }
  600. static int mxs_get_vddd_power_source_off(void)
  601. {
  602. struct mxs_power_regs *power_regs =
  603. (struct mxs_power_regs *)MXS_POWER_BASE;
  604. uint32_t tmp;
  605. tmp = readl(&power_regs->hw_power_vdddctrl);
  606. if (tmp & POWER_VDDDCTRL_DISABLE_FET) {
  607. if ((tmp & POWER_VDDDCTRL_LINREG_OFFSET_MASK) ==
  608. POWER_VDDDCTRL_LINREG_OFFSET_0STEPS) {
  609. return 1;
  610. }
  611. }
  612. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  613. if (!(readl(&power_regs->hw_power_5vctrl) &
  614. POWER_5VCTRL_ENABLE_DCDC)) {
  615. return 1;
  616. }
  617. }
  618. if (!(tmp & POWER_VDDDCTRL_ENABLE_LINREG)) {
  619. if ((tmp & POWER_VDDDCTRL_LINREG_OFFSET_MASK) ==
  620. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW) {
  621. return 1;
  622. }
  623. }
  624. return 0;
  625. }
  626. struct mxs_vddx_cfg {
  627. uint32_t *reg;
  628. uint8_t step_mV;
  629. uint16_t lowest_mV;
  630. int (*powered_by_linreg)(void);
  631. uint32_t trg_mask;
  632. uint32_t bo_irq;
  633. uint32_t bo_enirq;
  634. uint32_t bo_offset_mask;
  635. uint32_t bo_offset_offset;
  636. };
  637. static const struct mxs_vddx_cfg mxs_vddio_cfg = {
  638. .reg = &(((struct mxs_power_regs *)MXS_POWER_BASE)->
  639. hw_power_vddioctrl),
  640. .step_mV = 50,
  641. .lowest_mV = 2800,
  642. .powered_by_linreg = mxs_get_vddio_power_source_off,
  643. .trg_mask = POWER_VDDIOCTRL_TRG_MASK,
  644. .bo_irq = POWER_CTRL_VDDIO_BO_IRQ,
  645. .bo_enirq = POWER_CTRL_ENIRQ_VDDIO_BO,
  646. .bo_offset_mask = POWER_VDDIOCTRL_BO_OFFSET_MASK,
  647. .bo_offset_offset = POWER_VDDIOCTRL_BO_OFFSET_OFFSET,
  648. };
  649. static const struct mxs_vddx_cfg mxs_vddd_cfg = {
  650. .reg = &(((struct mxs_power_regs *)MXS_POWER_BASE)->
  651. hw_power_vdddctrl),
  652. .step_mV = 25,
  653. .lowest_mV = 800,
  654. .powered_by_linreg = mxs_get_vddd_power_source_off,
  655. .trg_mask = POWER_VDDDCTRL_TRG_MASK,
  656. .bo_irq = POWER_CTRL_VDDD_BO_IRQ,
  657. .bo_enirq = POWER_CTRL_ENIRQ_VDDD_BO,
  658. .bo_offset_mask = POWER_VDDDCTRL_BO_OFFSET_MASK,
  659. .bo_offset_offset = POWER_VDDDCTRL_BO_OFFSET_OFFSET,
  660. };
  661. static void mxs_power_set_vddx(const struct mxs_vddx_cfg *cfg,
  662. uint32_t new_target, uint32_t new_brownout)
  663. {
  664. struct mxs_power_regs *power_regs =
  665. (struct mxs_power_regs *)MXS_POWER_BASE;
  666. uint32_t cur_target, diff, bo_int = 0;
  667. uint32_t powered_by_linreg = 0;
  668. int adjust_up, tmp;
  669. new_brownout = DIV_ROUND(new_target - new_brownout, cfg->step_mV);
  670. cur_target = readl(cfg->reg);
  671. cur_target &= cfg->trg_mask;
  672. cur_target *= cfg->step_mV;
  673. cur_target += cfg->lowest_mV;
  674. adjust_up = new_target > cur_target;
  675. powered_by_linreg = cfg->powered_by_linreg();
  676. if (adjust_up) {
  677. if (powered_by_linreg) {
  678. bo_int = readl(cfg->reg);
  679. clrbits_le32(cfg->reg, cfg->bo_enirq);
  680. }
  681. setbits_le32(cfg->reg, cfg->bo_offset_mask);
  682. }
  683. do {
  684. if (abs(new_target - cur_target) > 100) {
  685. if (adjust_up)
  686. diff = cur_target + 100;
  687. else
  688. diff = cur_target - 100;
  689. } else {
  690. diff = new_target;
  691. }
  692. diff -= cfg->lowest_mV;
  693. diff /= cfg->step_mV;
  694. clrsetbits_le32(cfg->reg, cfg->trg_mask, diff);
  695. if (powered_by_linreg ||
  696. (readl(&power_regs->hw_power_sts) &
  697. POWER_STS_VDD5V_GT_VDDIO))
  698. early_delay(500);
  699. else {
  700. for (;;) {
  701. tmp = readl(&power_regs->hw_power_sts);
  702. if (tmp & POWER_STS_DC_OK)
  703. break;
  704. }
  705. }
  706. cur_target = readl(cfg->reg);
  707. cur_target &= cfg->trg_mask;
  708. cur_target *= cfg->step_mV;
  709. cur_target += cfg->lowest_mV;
  710. } while (new_target > cur_target);
  711. if (adjust_up && powered_by_linreg) {
  712. writel(cfg->bo_irq, &power_regs->hw_power_ctrl_clr);
  713. if (bo_int & cfg->bo_enirq)
  714. setbits_le32(cfg->reg, cfg->bo_enirq);
  715. }
  716. clrsetbits_le32(cfg->reg, cfg->bo_offset_mask,
  717. new_brownout << cfg->bo_offset_offset);
  718. }
  719. static void mxs_setup_batt_detect(void)
  720. {
  721. mxs_lradc_init();
  722. mxs_lradc_enable_batt_measurement();
  723. early_delay(10);
  724. }
  725. void mxs_power_init(void)
  726. {
  727. struct mxs_power_regs *power_regs =
  728. (struct mxs_power_regs *)MXS_POWER_BASE;
  729. mxs_power_clock2xtal();
  730. mxs_power_clear_auto_restart();
  731. mxs_power_set_linreg();
  732. mxs_power_setup_5v_detect();
  733. mxs_setup_batt_detect();
  734. mxs_power_configure_power_source();
  735. mxs_enable_output_rail_protection();
  736. mxs_power_set_vddx(&mxs_vddio_cfg, 3300, 3150);
  737. mxs_power_set_vddx(&mxs_vddd_cfg, 1500, 1000);
  738. writel(POWER_CTRL_VDDD_BO_IRQ | POWER_CTRL_VDDA_BO_IRQ |
  739. POWER_CTRL_VDDIO_BO_IRQ | POWER_CTRL_VDD5V_DROOP_IRQ |
  740. POWER_CTRL_VBUS_VALID_IRQ | POWER_CTRL_BATT_BO_IRQ |
  741. POWER_CTRL_DCDC4P2_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  742. writel(POWER_5VCTRL_PWDN_5VBRNOUT, &power_regs->hw_power_5vctrl_set);
  743. early_delay(1000);
  744. }
  745. #ifdef CONFIG_SPL_MX28_PSWITCH_WAIT
  746. void mxs_power_wait_pswitch(void)
  747. {
  748. struct mxs_power_regs *power_regs =
  749. (struct mxs_power_regs *)MXS_POWER_BASE;
  750. while (!(readl(&power_regs->hw_power_sts) & POWER_STS_PSWITCH_MASK))
  751. ;
  752. }
  753. #endif