power_init.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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 "m28_init.h"
  30. void mx28_power_clock2xtal(void)
  31. {
  32. struct mx28_clkctrl_regs *clkctrl_regs =
  33. (struct mx28_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. void mx28_power_clock2pll(void)
  39. {
  40. struct mx28_clkctrl_regs *clkctrl_regs =
  41. (struct mx28_clkctrl_regs *)MXS_CLKCTRL_BASE;
  42. writel(CLKCTRL_PLL0CTRL0_POWER,
  43. &clkctrl_regs->hw_clkctrl_pll0ctrl0_set);
  44. early_delay(100);
  45. writel(CLKCTRL_CLKSEQ_BYPASS_CPU,
  46. &clkctrl_regs->hw_clkctrl_clkseq_clr);
  47. }
  48. void mx28_power_clear_auto_restart(void)
  49. {
  50. struct mx28_rtc_regs *rtc_regs =
  51. (struct mx28_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. void mx28_power_set_linreg(void)
  76. {
  77. struct mx28_power_regs *power_regs =
  78. (struct mx28_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. void mx28_power_setup_5v_detect(void)
  91. {
  92. struct mx28_power_regs *power_regs =
  93. (struct mx28_power_regs *)MXS_POWER_BASE;
  94. /* Start 5V detection */
  95. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  96. POWER_5VCTRL_VBUSVALID_TRSH_MASK,
  97. POWER_5VCTRL_VBUSVALID_TRSH_4V4 |
  98. POWER_5VCTRL_PWRUP_VBUS_CMPS);
  99. }
  100. void mx28_src_power_init(void)
  101. {
  102. struct mx28_power_regs *power_regs =
  103. (struct mx28_power_regs *)MXS_POWER_BASE;
  104. /* Improve efficieny and reduce transient ripple */
  105. writel(POWER_LOOPCTRL_TOGGLE_DIF | POWER_LOOPCTRL_EN_CM_HYST |
  106. POWER_LOOPCTRL_EN_DF_HYST, &power_regs->hw_power_loopctrl_set);
  107. clrsetbits_le32(&power_regs->hw_power_dclimits,
  108. POWER_DCLIMITS_POSLIMIT_BUCK_MASK,
  109. 0x30 << POWER_DCLIMITS_POSLIMIT_BUCK_OFFSET);
  110. setbits_le32(&power_regs->hw_power_battmonitor,
  111. POWER_BATTMONITOR_EN_BATADJ);
  112. /* Increase the RCSCALE level for quick DCDC response to dynamic load */
  113. clrsetbits_le32(&power_regs->hw_power_loopctrl,
  114. POWER_LOOPCTRL_EN_RCSCALE_MASK,
  115. POWER_LOOPCTRL_RCSCALE_THRESH |
  116. POWER_LOOPCTRL_EN_RCSCALE_8X);
  117. clrsetbits_le32(&power_regs->hw_power_minpwr,
  118. POWER_MINPWR_HALFFETS, POWER_MINPWR_DOUBLE_FETS);
  119. /* 5V to battery handoff ... FIXME */
  120. setbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  121. early_delay(30);
  122. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_DCDC_XFER);
  123. }
  124. void mx28_power_init_4p2_params(void)
  125. {
  126. struct mx28_power_regs *power_regs =
  127. (struct mx28_power_regs *)MXS_POWER_BASE;
  128. /* Setup 4P2 parameters */
  129. clrsetbits_le32(&power_regs->hw_power_dcdc4p2,
  130. POWER_DCDC4P2_CMPTRIP_MASK | POWER_DCDC4P2_TRG_MASK,
  131. POWER_DCDC4P2_TRG_4V2 | (31 << POWER_DCDC4P2_CMPTRIP_OFFSET));
  132. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  133. POWER_5VCTRL_HEADROOM_ADJ_MASK,
  134. 0x4 << POWER_5VCTRL_HEADROOM_ADJ_OFFSET);
  135. clrsetbits_le32(&power_regs->hw_power_dcdc4p2,
  136. POWER_DCDC4P2_DROPOUT_CTRL_MASK,
  137. POWER_DCDC4P2_DROPOUT_CTRL_100MV |
  138. POWER_DCDC4P2_DROPOUT_CTRL_SRC_SEL);
  139. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  140. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  141. 0x3f << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  142. }
  143. void mx28_enable_4p2_dcdc_input(int xfer)
  144. {
  145. struct mx28_power_regs *power_regs =
  146. (struct mx28_power_regs *)MXS_POWER_BASE;
  147. uint32_t tmp, vbus_thresh, vbus_5vdetect, pwd_bo;
  148. uint32_t prev_5v_brnout, prev_5v_droop;
  149. prev_5v_brnout = readl(&power_regs->hw_power_5vctrl) &
  150. POWER_5VCTRL_PWDN_5VBRNOUT;
  151. prev_5v_droop = readl(&power_regs->hw_power_ctrl) &
  152. POWER_CTRL_ENIRQ_VDD5V_DROOP;
  153. clrbits_le32(&power_regs->hw_power_5vctrl, POWER_5VCTRL_PWDN_5VBRNOUT);
  154. writel(POWER_RESET_UNLOCK_KEY | POWER_RESET_PWD_OFF,
  155. &power_regs->hw_power_reset);
  156. clrbits_le32(&power_regs->hw_power_ctrl, POWER_CTRL_ENIRQ_VDD5V_DROOP);
  157. if (xfer && (readl(&power_regs->hw_power_5vctrl) &
  158. POWER_5VCTRL_ENABLE_DCDC)) {
  159. return;
  160. }
  161. /*
  162. * Recording orignal values that will be modified temporarlily
  163. * to handle a chip bug. See chip errata for CQ ENGR00115837
  164. */
  165. tmp = readl(&power_regs->hw_power_5vctrl);
  166. vbus_thresh = tmp & POWER_5VCTRL_VBUSVALID_TRSH_MASK;
  167. vbus_5vdetect = tmp & POWER_5VCTRL_VBUSVALID_5VDETECT;
  168. pwd_bo = readl(&power_regs->hw_power_minpwr) & POWER_MINPWR_PWD_BO;
  169. /*
  170. * Disable mechanisms that get erroneously tripped by when setting
  171. * the DCDC4P2 EN_DCDC
  172. */
  173. clrbits_le32(&power_regs->hw_power_5vctrl,
  174. POWER_5VCTRL_VBUSVALID_5VDETECT |
  175. POWER_5VCTRL_VBUSVALID_TRSH_MASK);
  176. writel(POWER_MINPWR_PWD_BO, &power_regs->hw_power_minpwr_set);
  177. if (xfer) {
  178. setbits_le32(&power_regs->hw_power_5vctrl,
  179. POWER_5VCTRL_DCDC_XFER);
  180. early_delay(20);
  181. clrbits_le32(&power_regs->hw_power_5vctrl,
  182. POWER_5VCTRL_DCDC_XFER);
  183. setbits_le32(&power_regs->hw_power_5vctrl,
  184. POWER_5VCTRL_ENABLE_DCDC);
  185. } else {
  186. setbits_le32(&power_regs->hw_power_dcdc4p2,
  187. POWER_DCDC4P2_ENABLE_DCDC);
  188. }
  189. early_delay(25);
  190. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  191. POWER_5VCTRL_VBUSVALID_TRSH_MASK, vbus_thresh);
  192. if (vbus_5vdetect)
  193. writel(vbus_5vdetect, &power_regs->hw_power_5vctrl_set);
  194. if (!pwd_bo)
  195. clrbits_le32(&power_regs->hw_power_minpwr, POWER_MINPWR_PWD_BO);
  196. while (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VBUS_VALID_IRQ)
  197. clrbits_le32(&power_regs->hw_power_ctrl,
  198. POWER_CTRL_VBUS_VALID_IRQ);
  199. if (prev_5v_brnout) {
  200. writel(POWER_5VCTRL_PWDN_5VBRNOUT,
  201. &power_regs->hw_power_5vctrl_set);
  202. writel(POWER_RESET_UNLOCK_KEY,
  203. &power_regs->hw_power_reset);
  204. } else {
  205. writel(POWER_5VCTRL_PWDN_5VBRNOUT,
  206. &power_regs->hw_power_5vctrl_clr);
  207. writel(POWER_RESET_UNLOCK_KEY | POWER_RESET_PWD_OFF,
  208. &power_regs->hw_power_reset);
  209. }
  210. while (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VDD5V_DROOP_IRQ)
  211. clrbits_le32(&power_regs->hw_power_ctrl,
  212. POWER_CTRL_VDD5V_DROOP_IRQ);
  213. if (prev_5v_droop)
  214. clrbits_le32(&power_regs->hw_power_ctrl,
  215. POWER_CTRL_ENIRQ_VDD5V_DROOP);
  216. else
  217. setbits_le32(&power_regs->hw_power_ctrl,
  218. POWER_CTRL_ENIRQ_VDD5V_DROOP);
  219. }
  220. void mx28_power_init_4p2_regulator(void)
  221. {
  222. struct mx28_power_regs *power_regs =
  223. (struct mx28_power_regs *)MXS_POWER_BASE;
  224. uint32_t tmp, tmp2;
  225. setbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_ENABLE_4P2);
  226. writel(POWER_CHARGE_ENABLE_LOAD, &power_regs->hw_power_charge_set);
  227. writel(POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  228. &power_regs->hw_power_5vctrl_clr);
  229. clrbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_TRG_MASK);
  230. /* Power up the 4p2 rail and logic/control */
  231. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  232. &power_regs->hw_power_5vctrl_clr);
  233. /*
  234. * Start charging up the 4p2 capacitor. We ramp of this charge
  235. * gradually to avoid large inrush current from the 5V cable which can
  236. * cause transients/problems
  237. */
  238. mx28_enable_4p2_dcdc_input(0);
  239. if (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VBUS_VALID_IRQ) {
  240. /*
  241. * If we arrived here, we were unable to recover from mx23 chip
  242. * errata 5837. 4P2 is disabled and sufficient battery power is
  243. * not present. Exiting to not enable DCDC power during 5V
  244. * connected state.
  245. */
  246. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  247. POWER_DCDC4P2_ENABLE_DCDC);
  248. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  249. &power_regs->hw_power_5vctrl_set);
  250. hang();
  251. }
  252. /*
  253. * Here we set the 4p2 brownout level to something very close to 4.2V.
  254. * We then check the brownout status. If the brownout status is false,
  255. * the voltage is already close to the target voltage of 4.2V so we
  256. * can go ahead and set the 4P2 current limit to our max target limit.
  257. * If the brownout status is true, we need to ramp us the current limit
  258. * so that we don't cause large inrush current issues. We step up the
  259. * current limit until the brownout status is false or until we've
  260. * reached our maximum defined 4p2 current limit.
  261. */
  262. clrsetbits_le32(&power_regs->hw_power_dcdc4p2,
  263. POWER_DCDC4P2_BO_MASK,
  264. 22 << POWER_DCDC4P2_BO_OFFSET); /* 4.15V */
  265. if (!(readl(&power_regs->hw_power_sts) & POWER_STS_DCDC_4P2_BO)) {
  266. setbits_le32(&power_regs->hw_power_5vctrl,
  267. 0x3f << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  268. } else {
  269. tmp = (readl(&power_regs->hw_power_5vctrl) &
  270. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK) >>
  271. POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET;
  272. while (tmp < 0x3f) {
  273. if (!(readl(&power_regs->hw_power_sts) &
  274. POWER_STS_DCDC_4P2_BO)) {
  275. tmp = readl(&power_regs->hw_power_5vctrl);
  276. tmp |= POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK;
  277. early_delay(100);
  278. writel(tmp, &power_regs->hw_power_5vctrl);
  279. break;
  280. } else {
  281. tmp++;
  282. tmp2 = readl(&power_regs->hw_power_5vctrl);
  283. tmp2 &= ~POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK;
  284. tmp2 |= tmp <<
  285. POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET;
  286. writel(tmp2, &power_regs->hw_power_5vctrl);
  287. early_delay(100);
  288. }
  289. }
  290. }
  291. clrbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_BO_MASK);
  292. writel(POWER_CTRL_DCDC4P2_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  293. }
  294. void mx28_power_init_dcdc_4p2_source(void)
  295. {
  296. struct mx28_power_regs *power_regs =
  297. (struct mx28_power_regs *)MXS_POWER_BASE;
  298. if (!(readl(&power_regs->hw_power_dcdc4p2) &
  299. POWER_DCDC4P2_ENABLE_DCDC)) {
  300. hang();
  301. }
  302. mx28_enable_4p2_dcdc_input(1);
  303. if (readl(&power_regs->hw_power_ctrl) & POWER_CTRL_VBUS_VALID_IRQ) {
  304. clrbits_le32(&power_regs->hw_power_dcdc4p2,
  305. POWER_DCDC4P2_ENABLE_DCDC);
  306. writel(POWER_5VCTRL_ENABLE_DCDC,
  307. &power_regs->hw_power_5vctrl_clr);
  308. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  309. &power_regs->hw_power_5vctrl_set);
  310. }
  311. }
  312. void mx28_power_enable_4p2(void)
  313. {
  314. struct mx28_power_regs *power_regs =
  315. (struct mx28_power_regs *)MXS_POWER_BASE;
  316. uint32_t vdddctrl, vddactrl, vddioctrl;
  317. uint32_t tmp;
  318. vdddctrl = readl(&power_regs->hw_power_vdddctrl);
  319. vddactrl = readl(&power_regs->hw_power_vddactrl);
  320. vddioctrl = readl(&power_regs->hw_power_vddioctrl);
  321. setbits_le32(&power_regs->hw_power_vdddctrl,
  322. POWER_VDDDCTRL_DISABLE_FET | POWER_VDDDCTRL_ENABLE_LINREG |
  323. POWER_VDDDCTRL_PWDN_BRNOUT);
  324. setbits_le32(&power_regs->hw_power_vddactrl,
  325. POWER_VDDACTRL_DISABLE_FET | POWER_VDDACTRL_ENABLE_LINREG |
  326. POWER_VDDACTRL_PWDN_BRNOUT);
  327. setbits_le32(&power_regs->hw_power_vddioctrl,
  328. POWER_VDDIOCTRL_DISABLE_FET | POWER_VDDIOCTRL_PWDN_BRNOUT);
  329. mx28_power_init_4p2_params();
  330. mx28_power_init_4p2_regulator();
  331. /* Shutdown battery (none present) */
  332. clrbits_le32(&power_regs->hw_power_dcdc4p2, POWER_DCDC4P2_BO_MASK);
  333. writel(POWER_CTRL_DCDC4P2_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  334. writel(POWER_CTRL_ENIRQ_DCDC4P2_BO, &power_regs->hw_power_ctrl_clr);
  335. mx28_power_init_dcdc_4p2_source();
  336. writel(vdddctrl, &power_regs->hw_power_vdddctrl);
  337. early_delay(20);
  338. writel(vddactrl, &power_regs->hw_power_vddactrl);
  339. early_delay(20);
  340. writel(vddioctrl, &power_regs->hw_power_vddioctrl);
  341. /*
  342. * Check if FET is enabled on either powerout and if so,
  343. * disable load.
  344. */
  345. tmp = 0;
  346. tmp |= !(readl(&power_regs->hw_power_vdddctrl) &
  347. POWER_VDDDCTRL_DISABLE_FET);
  348. tmp |= !(readl(&power_regs->hw_power_vddactrl) &
  349. POWER_VDDACTRL_DISABLE_FET);
  350. tmp |= !(readl(&power_regs->hw_power_vddioctrl) &
  351. POWER_VDDIOCTRL_DISABLE_FET);
  352. if (tmp)
  353. writel(POWER_CHARGE_ENABLE_LOAD,
  354. &power_regs->hw_power_charge_clr);
  355. }
  356. void mx28_boot_valid_5v(void)
  357. {
  358. struct mx28_power_regs *power_regs =
  359. (struct mx28_power_regs *)MXS_POWER_BASE;
  360. /*
  361. * Use VBUSVALID level instead of VDD5V_GT_VDDIO level to trigger a 5V
  362. * disconnect event. FIXME
  363. */
  364. writel(POWER_5VCTRL_VBUSVALID_5VDETECT,
  365. &power_regs->hw_power_5vctrl_set);
  366. /* Configure polarity to check for 5V disconnection. */
  367. writel(POWER_CTRL_POLARITY_VBUSVALID |
  368. POWER_CTRL_POLARITY_VDD5V_GT_VDDIO,
  369. &power_regs->hw_power_ctrl_clr);
  370. writel(POWER_CTRL_VBUS_VALID_IRQ | POWER_CTRL_VDD5V_GT_VDDIO_IRQ,
  371. &power_regs->hw_power_ctrl_clr);
  372. mx28_power_enable_4p2();
  373. }
  374. void mx28_powerdown(void)
  375. {
  376. struct mx28_power_regs *power_regs =
  377. (struct mx28_power_regs *)MXS_POWER_BASE;
  378. writel(POWER_RESET_UNLOCK_KEY, &power_regs->hw_power_reset);
  379. writel(POWER_RESET_UNLOCK_KEY | POWER_RESET_PWD_OFF,
  380. &power_regs->hw_power_reset);
  381. }
  382. void mx28_handle_5v_conflict(void)
  383. {
  384. struct mx28_power_regs *power_regs =
  385. (struct mx28_power_regs *)MXS_POWER_BASE;
  386. uint32_t tmp;
  387. setbits_le32(&power_regs->hw_power_vddioctrl,
  388. POWER_VDDIOCTRL_BO_OFFSET_MASK);
  389. for (;;) {
  390. tmp = readl(&power_regs->hw_power_sts);
  391. if (tmp & POWER_STS_VDDIO_BO) {
  392. mx28_powerdown();
  393. break;
  394. }
  395. if (tmp & POWER_STS_VDD5V_GT_VDDIO) {
  396. mx28_boot_valid_5v();
  397. break;
  398. } else {
  399. mx28_powerdown();
  400. break;
  401. }
  402. }
  403. }
  404. int mx28_get_batt_volt(void)
  405. {
  406. struct mx28_power_regs *power_regs =
  407. (struct mx28_power_regs *)MXS_POWER_BASE;
  408. uint32_t volt = readl(&power_regs->hw_power_battmonitor);
  409. volt &= POWER_BATTMONITOR_BATT_VAL_MASK;
  410. volt >>= POWER_BATTMONITOR_BATT_VAL_OFFSET;
  411. volt *= 8;
  412. return volt;
  413. }
  414. int mx28_is_batt_ready(void)
  415. {
  416. return (mx28_get_batt_volt() >= 3600);
  417. }
  418. void mx28_5v_boot(void)
  419. {
  420. struct mx28_power_regs *power_regs =
  421. (struct mx28_power_regs *)MXS_POWER_BASE;
  422. /*
  423. * NOTE: In original IMX-Bootlets, this also checks for VBUSVALID,
  424. * but their implementation always returns 1 so we omit it here.
  425. */
  426. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  427. mx28_boot_valid_5v();
  428. return;
  429. }
  430. early_delay(1000);
  431. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  432. mx28_boot_valid_5v();
  433. return;
  434. }
  435. mx28_handle_5v_conflict();
  436. }
  437. void mx28_init_batt_bo(void)
  438. {
  439. struct mx28_power_regs *power_regs =
  440. (struct mx28_power_regs *)MXS_POWER_BASE;
  441. /* Brownout at 3V */
  442. clrsetbits_le32(&power_regs->hw_power_battmonitor,
  443. POWER_BATTMONITOR_BRWNOUT_LVL_MASK,
  444. 15 << POWER_BATTMONITOR_BRWNOUT_LVL_OFFSET);
  445. writel(POWER_CTRL_BATT_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  446. writel(POWER_CTRL_ENIRQ_BATT_BO, &power_regs->hw_power_ctrl_clr);
  447. }
  448. void mx28_switch_vddd_to_dcdc_source(void)
  449. {
  450. struct mx28_power_regs *power_regs =
  451. (struct mx28_power_regs *)MXS_POWER_BASE;
  452. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  453. POWER_VDDDCTRL_LINREG_OFFSET_MASK,
  454. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW);
  455. clrbits_le32(&power_regs->hw_power_vdddctrl,
  456. POWER_VDDDCTRL_DISABLE_FET | POWER_VDDDCTRL_ENABLE_LINREG |
  457. POWER_VDDDCTRL_DISABLE_STEPPING);
  458. }
  459. int mx28_is_batt_good(void)
  460. {
  461. struct mx28_power_regs *power_regs =
  462. (struct mx28_power_regs *)MXS_POWER_BASE;
  463. uint32_t volt;
  464. volt = readl(&power_regs->hw_power_battmonitor);
  465. volt &= POWER_BATTMONITOR_BATT_VAL_MASK;
  466. volt >>= POWER_BATTMONITOR_BATT_VAL_OFFSET;
  467. volt *= 8;
  468. if ((volt >= 2400) && (volt <= 4300))
  469. return 1;
  470. clrsetbits_le32(&power_regs->hw_power_5vctrl,
  471. POWER_5VCTRL_CHARGE_4P2_ILIMIT_MASK,
  472. 0x3 << POWER_5VCTRL_CHARGE_4P2_ILIMIT_OFFSET);
  473. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  474. &power_regs->hw_power_5vctrl_clr);
  475. clrsetbits_le32(&power_regs->hw_power_charge,
  476. POWER_CHARGE_STOP_ILIMIT_MASK | POWER_CHARGE_BATTCHRG_I_MASK,
  477. POWER_CHARGE_STOP_ILIMIT_10MA | 0x3);
  478. writel(POWER_CHARGE_PWD_BATTCHRG, &power_regs->hw_power_charge_clr);
  479. writel(POWER_5VCTRL_PWD_CHARGE_4P2_MASK,
  480. &power_regs->hw_power_5vctrl_clr);
  481. early_delay(500000);
  482. volt = readl(&power_regs->hw_power_battmonitor);
  483. volt &= POWER_BATTMONITOR_BATT_VAL_MASK;
  484. volt >>= POWER_BATTMONITOR_BATT_VAL_OFFSET;
  485. volt *= 8;
  486. if (volt >= 3500)
  487. return 0;
  488. if (volt >= 2400)
  489. return 1;
  490. writel(POWER_CHARGE_STOP_ILIMIT_MASK | POWER_CHARGE_BATTCHRG_I_MASK,
  491. &power_regs->hw_power_charge_clr);
  492. writel(POWER_CHARGE_PWD_BATTCHRG, &power_regs->hw_power_charge_set);
  493. return 0;
  494. }
  495. void mx28_power_configure_power_source(void)
  496. {
  497. mx28_src_power_init();
  498. mx28_5v_boot();
  499. mx28_power_clock2pll();
  500. mx28_init_batt_bo();
  501. mx28_switch_vddd_to_dcdc_source();
  502. }
  503. void mx28_enable_output_rail_protection(void)
  504. {
  505. struct mx28_power_regs *power_regs =
  506. (struct mx28_power_regs *)MXS_POWER_BASE;
  507. writel(POWER_CTRL_VDDD_BO_IRQ | POWER_CTRL_VDDA_BO_IRQ |
  508. POWER_CTRL_VDDIO_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  509. setbits_le32(&power_regs->hw_power_vdddctrl,
  510. POWER_VDDDCTRL_PWDN_BRNOUT);
  511. setbits_le32(&power_regs->hw_power_vddactrl,
  512. POWER_VDDACTRL_PWDN_BRNOUT);
  513. setbits_le32(&power_regs->hw_power_vddioctrl,
  514. POWER_VDDIOCTRL_PWDN_BRNOUT);
  515. }
  516. int mx28_get_vddio_power_source_off(void)
  517. {
  518. struct mx28_power_regs *power_regs =
  519. (struct mx28_power_regs *)MXS_POWER_BASE;
  520. uint32_t tmp;
  521. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  522. tmp = readl(&power_regs->hw_power_vddioctrl);
  523. if (tmp & POWER_VDDIOCTRL_DISABLE_FET) {
  524. if ((tmp & POWER_VDDIOCTRL_LINREG_OFFSET_MASK) ==
  525. POWER_VDDDCTRL_LINREG_OFFSET_0STEPS) {
  526. return 1;
  527. }
  528. }
  529. if (!(readl(&power_regs->hw_power_5vctrl) &
  530. POWER_5VCTRL_ENABLE_DCDC)) {
  531. if ((tmp & POWER_VDDIOCTRL_LINREG_OFFSET_MASK) ==
  532. POWER_VDDDCTRL_LINREG_OFFSET_0STEPS) {
  533. return 1;
  534. }
  535. }
  536. }
  537. return 0;
  538. }
  539. int mx28_get_vddd_power_source_off(void)
  540. {
  541. struct mx28_power_regs *power_regs =
  542. (struct mx28_power_regs *)MXS_POWER_BASE;
  543. uint32_t tmp;
  544. tmp = readl(&power_regs->hw_power_vdddctrl);
  545. if (tmp & POWER_VDDDCTRL_DISABLE_FET) {
  546. if ((tmp & POWER_VDDDCTRL_LINREG_OFFSET_MASK) ==
  547. POWER_VDDDCTRL_LINREG_OFFSET_0STEPS) {
  548. return 1;
  549. }
  550. }
  551. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  552. if (!(readl(&power_regs->hw_power_5vctrl) &
  553. POWER_5VCTRL_ENABLE_DCDC)) {
  554. return 1;
  555. }
  556. }
  557. if (!(tmp & POWER_VDDDCTRL_ENABLE_LINREG)) {
  558. if ((tmp & POWER_VDDDCTRL_LINREG_OFFSET_MASK) ==
  559. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW) {
  560. return 1;
  561. }
  562. }
  563. return 0;
  564. }
  565. void mx28_power_set_vddio(uint32_t new_target, uint32_t new_brownout)
  566. {
  567. struct mx28_power_regs *power_regs =
  568. (struct mx28_power_regs *)MXS_POWER_BASE;
  569. uint32_t cur_target, diff, bo_int = 0;
  570. uint32_t powered_by_linreg = 0;
  571. new_brownout = new_target - new_brownout;
  572. cur_target = readl(&power_regs->hw_power_vddioctrl);
  573. cur_target &= POWER_VDDIOCTRL_TRG_MASK;
  574. cur_target *= 50; /* 50 mV step*/
  575. cur_target += 2800; /* 2800 mV lowest */
  576. powered_by_linreg = mx28_get_vddio_power_source_off();
  577. if (new_target > cur_target) {
  578. if (powered_by_linreg) {
  579. bo_int = readl(&power_regs->hw_power_vddioctrl);
  580. clrbits_le32(&power_regs->hw_power_vddioctrl,
  581. POWER_CTRL_ENIRQ_VDDIO_BO);
  582. }
  583. setbits_le32(&power_regs->hw_power_vddioctrl,
  584. POWER_VDDIOCTRL_BO_OFFSET_MASK);
  585. do {
  586. if (new_target - cur_target > 100)
  587. diff = cur_target + 100;
  588. else
  589. diff = new_target;
  590. diff -= 2800;
  591. diff /= 50;
  592. clrsetbits_le32(&power_regs->hw_power_vddioctrl,
  593. POWER_VDDIOCTRL_TRG_MASK, diff);
  594. if (powered_by_linreg)
  595. early_delay(1500);
  596. else {
  597. while (!(readl(&power_regs->hw_power_sts) &
  598. POWER_STS_DC_OK))
  599. ;
  600. }
  601. cur_target = readl(&power_regs->hw_power_vddioctrl);
  602. cur_target &= POWER_VDDIOCTRL_TRG_MASK;
  603. cur_target *= 50; /* 50 mV step*/
  604. cur_target += 2800; /* 2800 mV lowest */
  605. } while (new_target > cur_target);
  606. if (powered_by_linreg) {
  607. writel(POWER_CTRL_VDDIO_BO_IRQ,
  608. &power_regs->hw_power_ctrl_clr);
  609. if (bo_int & POWER_CTRL_ENIRQ_VDDIO_BO)
  610. setbits_le32(&power_regs->hw_power_vddioctrl,
  611. POWER_CTRL_ENIRQ_VDDIO_BO);
  612. }
  613. } else {
  614. do {
  615. if (cur_target - new_target > 100)
  616. diff = cur_target - 100;
  617. else
  618. diff = new_target;
  619. diff -= 2800;
  620. diff /= 50;
  621. clrsetbits_le32(&power_regs->hw_power_vddioctrl,
  622. POWER_VDDIOCTRL_TRG_MASK, diff);
  623. if (powered_by_linreg)
  624. early_delay(1500);
  625. else {
  626. while (!(readl(&power_regs->hw_power_sts) &
  627. POWER_STS_DC_OK))
  628. ;
  629. }
  630. cur_target = readl(&power_regs->hw_power_vddioctrl);
  631. cur_target &= POWER_VDDIOCTRL_TRG_MASK;
  632. cur_target *= 50; /* 50 mV step*/
  633. cur_target += 2800; /* 2800 mV lowest */
  634. } while (new_target < cur_target);
  635. }
  636. clrsetbits_le32(&power_regs->hw_power_vddioctrl,
  637. POWER_VDDDCTRL_BO_OFFSET_MASK,
  638. new_brownout << POWER_VDDDCTRL_BO_OFFSET_OFFSET);
  639. }
  640. void mx28_power_set_vddd(uint32_t new_target, uint32_t new_brownout)
  641. {
  642. struct mx28_power_regs *power_regs =
  643. (struct mx28_power_regs *)MXS_POWER_BASE;
  644. uint32_t cur_target, diff, bo_int = 0;
  645. uint32_t powered_by_linreg = 0;
  646. new_brownout = new_target - new_brownout;
  647. cur_target = readl(&power_regs->hw_power_vdddctrl);
  648. cur_target &= POWER_VDDDCTRL_TRG_MASK;
  649. cur_target *= 25; /* 25 mV step*/
  650. cur_target += 800; /* 800 mV lowest */
  651. powered_by_linreg = mx28_get_vddd_power_source_off();
  652. if (new_target > cur_target) {
  653. if (powered_by_linreg) {
  654. bo_int = readl(&power_regs->hw_power_vdddctrl);
  655. clrbits_le32(&power_regs->hw_power_vdddctrl,
  656. POWER_CTRL_ENIRQ_VDDD_BO);
  657. }
  658. setbits_le32(&power_regs->hw_power_vdddctrl,
  659. POWER_VDDDCTRL_BO_OFFSET_MASK);
  660. do {
  661. if (new_target - cur_target > 100)
  662. diff = cur_target + 100;
  663. else
  664. diff = new_target;
  665. diff -= 800;
  666. diff /= 25;
  667. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  668. POWER_VDDDCTRL_TRG_MASK, diff);
  669. if (powered_by_linreg)
  670. early_delay(1500);
  671. else {
  672. while (!(readl(&power_regs->hw_power_sts) &
  673. POWER_STS_DC_OK))
  674. ;
  675. }
  676. cur_target = readl(&power_regs->hw_power_vdddctrl);
  677. cur_target &= POWER_VDDDCTRL_TRG_MASK;
  678. cur_target *= 25; /* 25 mV step*/
  679. cur_target += 800; /* 800 mV lowest */
  680. } while (new_target > cur_target);
  681. if (powered_by_linreg) {
  682. writel(POWER_CTRL_VDDD_BO_IRQ,
  683. &power_regs->hw_power_ctrl_clr);
  684. if (bo_int & POWER_CTRL_ENIRQ_VDDD_BO)
  685. setbits_le32(&power_regs->hw_power_vdddctrl,
  686. POWER_CTRL_ENIRQ_VDDD_BO);
  687. }
  688. } else {
  689. do {
  690. if (cur_target - new_target > 100)
  691. diff = cur_target - 100;
  692. else
  693. diff = new_target;
  694. diff -= 800;
  695. diff /= 25;
  696. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  697. POWER_VDDDCTRL_TRG_MASK, diff);
  698. if (powered_by_linreg)
  699. early_delay(1500);
  700. else {
  701. while (!(readl(&power_regs->hw_power_sts) &
  702. POWER_STS_DC_OK))
  703. ;
  704. }
  705. cur_target = readl(&power_regs->hw_power_vdddctrl);
  706. cur_target &= POWER_VDDDCTRL_TRG_MASK;
  707. cur_target *= 25; /* 25 mV step*/
  708. cur_target += 800; /* 800 mV lowest */
  709. } while (new_target < cur_target);
  710. }
  711. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  712. POWER_VDDDCTRL_BO_OFFSET_MASK,
  713. new_brownout << POWER_VDDDCTRL_BO_OFFSET_OFFSET);
  714. }
  715. void mx28_power_init(void)
  716. {
  717. struct mx28_power_regs *power_regs =
  718. (struct mx28_power_regs *)MXS_POWER_BASE;
  719. mx28_power_clock2xtal();
  720. mx28_power_clear_auto_restart();
  721. mx28_power_set_linreg();
  722. mx28_power_setup_5v_detect();
  723. mx28_power_configure_power_source();
  724. mx28_enable_output_rail_protection();
  725. mx28_power_set_vddio(3300, 3150);
  726. mx28_power_set_vddd(1350, 1200);
  727. writel(POWER_CTRL_VDDD_BO_IRQ | POWER_CTRL_VDDA_BO_IRQ |
  728. POWER_CTRL_VDDIO_BO_IRQ | POWER_CTRL_VDD5V_DROOP_IRQ |
  729. POWER_CTRL_VBUS_VALID_IRQ | POWER_CTRL_BATT_BO_IRQ |
  730. POWER_CTRL_DCDC4P2_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  731. writel(POWER_5VCTRL_PWDN_5VBRNOUT, &power_regs->hw_power_5vctrl_set);
  732. early_delay(1000);
  733. }
  734. #ifdef CONFIG_SPL_MX28_PSWITCH_WAIT
  735. void mx28_power_wait_pswitch(void)
  736. {
  737. struct mx28_power_regs *power_regs =
  738. (struct mx28_power_regs *)MXS_POWER_BASE;
  739. while (!(readl(&power_regs->hw_power_sts) & POWER_STS_PSWITCH_MASK))
  740. ;
  741. }
  742. #endif