spl_power_init.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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 "mx28_init.h"
  30. void mx28_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. void mx28_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. void mx28_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. void mx28_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. int mx28_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. int mx28_is_batt_ready(void)
  101. {
  102. return (mx28_get_batt_volt() >= 3600);
  103. }
  104. int mx28_is_batt_good(void)
  105. {
  106. struct mxs_power_regs *power_regs =
  107. (struct mxs_power_regs *)MXS_POWER_BASE;
  108. uint32_t volt = mx28_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 = mx28_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. void mx28_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. void mx28_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. void mx28_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. void mx28_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. void mx28_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. mx28_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. void mx28_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. mx28_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. void mx28_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. mx28_power_init_4p2_params();
  373. mx28_power_init_4p2_regulator();
  374. /* Shutdown battery (none present) */
  375. if (!mx28_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. mx28_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. void mx28_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. mx28_power_enable_4p2();
  421. }
  422. void mx28_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. void mx28_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. mx28_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. void mx28_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. mx28_powerdown();
  472. break;
  473. }
  474. if (tmp & POWER_STS_VDD5V_GT_VDDIO) {
  475. mx28_boot_valid_5v();
  476. break;
  477. } else {
  478. mx28_powerdown();
  479. break;
  480. }
  481. if (tmp & POWER_STS_PSWITCH_MASK) {
  482. mx28_batt_boot();
  483. break;
  484. }
  485. }
  486. }
  487. void mx28_5v_boot(void)
  488. {
  489. struct mxs_power_regs *power_regs =
  490. (struct mxs_power_regs *)MXS_POWER_BASE;
  491. /*
  492. * NOTE: In original IMX-Bootlets, this also checks for VBUSVALID,
  493. * but their implementation always returns 1 so we omit it here.
  494. */
  495. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  496. mx28_boot_valid_5v();
  497. return;
  498. }
  499. early_delay(1000);
  500. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  501. mx28_boot_valid_5v();
  502. return;
  503. }
  504. mx28_handle_5v_conflict();
  505. }
  506. void mx28_init_batt_bo(void)
  507. {
  508. struct mxs_power_regs *power_regs =
  509. (struct mxs_power_regs *)MXS_POWER_BASE;
  510. /* Brownout at 3V */
  511. clrsetbits_le32(&power_regs->hw_power_battmonitor,
  512. POWER_BATTMONITOR_BRWNOUT_LVL_MASK,
  513. 15 << POWER_BATTMONITOR_BRWNOUT_LVL_OFFSET);
  514. writel(POWER_CTRL_BATT_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  515. writel(POWER_CTRL_ENIRQ_BATT_BO, &power_regs->hw_power_ctrl_clr);
  516. }
  517. void mx28_switch_vddd_to_dcdc_source(void)
  518. {
  519. struct mxs_power_regs *power_regs =
  520. (struct mxs_power_regs *)MXS_POWER_BASE;
  521. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  522. POWER_VDDDCTRL_LINREG_OFFSET_MASK,
  523. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW);
  524. clrbits_le32(&power_regs->hw_power_vdddctrl,
  525. POWER_VDDDCTRL_DISABLE_FET | POWER_VDDDCTRL_ENABLE_LINREG |
  526. POWER_VDDDCTRL_DISABLE_STEPPING);
  527. }
  528. void mx28_power_configure_power_source(void)
  529. {
  530. int batt_ready, batt_good;
  531. struct mxs_power_regs *power_regs =
  532. (struct mxs_power_regs *)MXS_POWER_BASE;
  533. struct mxs_lradc_regs *lradc_regs =
  534. (struct mxs_lradc_regs *)MXS_LRADC_BASE;
  535. mx28_src_power_init();
  536. batt_ready = mx28_is_batt_ready();
  537. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  538. batt_good = mx28_is_batt_good();
  539. if (batt_ready) {
  540. /* 5V source detected, good battery detected. */
  541. mx28_batt_boot();
  542. } else {
  543. if (batt_good) {
  544. /* 5V source detected, low battery detceted. */
  545. } else {
  546. /* 5V source detected, bad battery detected. */
  547. writel(LRADC_CONVERSION_AUTOMATIC,
  548. &lradc_regs->hw_lradc_conversion_clr);
  549. clrbits_le32(&power_regs->hw_power_battmonitor,
  550. POWER_BATTMONITOR_BATT_VAL_MASK);
  551. }
  552. mx28_5v_boot();
  553. }
  554. } else {
  555. /* 5V not detected, booting from battery. */
  556. mx28_batt_boot();
  557. }
  558. mx28_power_clock2pll();
  559. mx28_init_batt_bo();
  560. mx28_switch_vddd_to_dcdc_source();
  561. }
  562. void mx28_enable_output_rail_protection(void)
  563. {
  564. struct mxs_power_regs *power_regs =
  565. (struct mxs_power_regs *)MXS_POWER_BASE;
  566. writel(POWER_CTRL_VDDD_BO_IRQ | POWER_CTRL_VDDA_BO_IRQ |
  567. POWER_CTRL_VDDIO_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  568. setbits_le32(&power_regs->hw_power_vdddctrl,
  569. POWER_VDDDCTRL_PWDN_BRNOUT);
  570. setbits_le32(&power_regs->hw_power_vddactrl,
  571. POWER_VDDACTRL_PWDN_BRNOUT);
  572. setbits_le32(&power_regs->hw_power_vddioctrl,
  573. POWER_VDDIOCTRL_PWDN_BRNOUT);
  574. }
  575. int mx28_get_vddio_power_source_off(void)
  576. {
  577. struct mxs_power_regs *power_regs =
  578. (struct mxs_power_regs *)MXS_POWER_BASE;
  579. uint32_t tmp;
  580. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  581. tmp = readl(&power_regs->hw_power_vddioctrl);
  582. if (tmp & POWER_VDDIOCTRL_DISABLE_FET) {
  583. if ((tmp & POWER_VDDIOCTRL_LINREG_OFFSET_MASK) ==
  584. POWER_VDDDCTRL_LINREG_OFFSET_0STEPS) {
  585. return 1;
  586. }
  587. }
  588. if (!(readl(&power_regs->hw_power_5vctrl) &
  589. POWER_5VCTRL_ENABLE_DCDC)) {
  590. if ((tmp & POWER_VDDIOCTRL_LINREG_OFFSET_MASK) ==
  591. POWER_VDDDCTRL_LINREG_OFFSET_0STEPS) {
  592. return 1;
  593. }
  594. }
  595. }
  596. return 0;
  597. }
  598. int mx28_get_vddd_power_source_off(void)
  599. {
  600. struct mxs_power_regs *power_regs =
  601. (struct mxs_power_regs *)MXS_POWER_BASE;
  602. uint32_t tmp;
  603. tmp = readl(&power_regs->hw_power_vdddctrl);
  604. if (tmp & POWER_VDDDCTRL_DISABLE_FET) {
  605. if ((tmp & POWER_VDDDCTRL_LINREG_OFFSET_MASK) ==
  606. POWER_VDDDCTRL_LINREG_OFFSET_0STEPS) {
  607. return 1;
  608. }
  609. }
  610. if (readl(&power_regs->hw_power_sts) & POWER_STS_VDD5V_GT_VDDIO) {
  611. if (!(readl(&power_regs->hw_power_5vctrl) &
  612. POWER_5VCTRL_ENABLE_DCDC)) {
  613. return 1;
  614. }
  615. }
  616. if (!(tmp & POWER_VDDDCTRL_ENABLE_LINREG)) {
  617. if ((tmp & POWER_VDDDCTRL_LINREG_OFFSET_MASK) ==
  618. POWER_VDDDCTRL_LINREG_OFFSET_1STEPS_BELOW) {
  619. return 1;
  620. }
  621. }
  622. return 0;
  623. }
  624. void mx28_power_set_vddio(uint32_t new_target, uint32_t new_brownout)
  625. {
  626. struct mxs_power_regs *power_regs =
  627. (struct mxs_power_regs *)MXS_POWER_BASE;
  628. uint32_t cur_target, diff, bo_int = 0;
  629. uint32_t powered_by_linreg = 0;
  630. new_brownout = new_target - new_brownout;
  631. cur_target = readl(&power_regs->hw_power_vddioctrl);
  632. cur_target &= POWER_VDDIOCTRL_TRG_MASK;
  633. cur_target *= 50; /* 50 mV step*/
  634. cur_target += 2800; /* 2800 mV lowest */
  635. powered_by_linreg = mx28_get_vddio_power_source_off();
  636. if (new_target > cur_target) {
  637. if (powered_by_linreg) {
  638. bo_int = readl(&power_regs->hw_power_vddioctrl);
  639. clrbits_le32(&power_regs->hw_power_vddioctrl,
  640. POWER_CTRL_ENIRQ_VDDIO_BO);
  641. }
  642. setbits_le32(&power_regs->hw_power_vddioctrl,
  643. POWER_VDDIOCTRL_BO_OFFSET_MASK);
  644. do {
  645. if (new_target - cur_target > 100)
  646. diff = cur_target + 100;
  647. else
  648. diff = new_target;
  649. diff -= 2800;
  650. diff /= 50;
  651. clrsetbits_le32(&power_regs->hw_power_vddioctrl,
  652. POWER_VDDIOCTRL_TRG_MASK, diff);
  653. if (powered_by_linreg ||
  654. (readl(&power_regs->hw_power_sts) &
  655. POWER_STS_VDD5V_GT_VDDIO))
  656. early_delay(500);
  657. else {
  658. while (!(readl(&power_regs->hw_power_sts) &
  659. POWER_STS_DC_OK))
  660. ;
  661. }
  662. cur_target = readl(&power_regs->hw_power_vddioctrl);
  663. cur_target &= POWER_VDDIOCTRL_TRG_MASK;
  664. cur_target *= 50; /* 50 mV step*/
  665. cur_target += 2800; /* 2800 mV lowest */
  666. } while (new_target > cur_target);
  667. if (powered_by_linreg) {
  668. writel(POWER_CTRL_VDDIO_BO_IRQ,
  669. &power_regs->hw_power_ctrl_clr);
  670. if (bo_int & POWER_CTRL_ENIRQ_VDDIO_BO)
  671. setbits_le32(&power_regs->hw_power_vddioctrl,
  672. POWER_CTRL_ENIRQ_VDDIO_BO);
  673. }
  674. } else {
  675. do {
  676. if (cur_target - new_target > 100)
  677. diff = cur_target - 100;
  678. else
  679. diff = new_target;
  680. diff -= 2800;
  681. diff /= 50;
  682. clrsetbits_le32(&power_regs->hw_power_vddioctrl,
  683. POWER_VDDIOCTRL_TRG_MASK, diff);
  684. if (powered_by_linreg ||
  685. (readl(&power_regs->hw_power_sts) &
  686. POWER_STS_VDD5V_GT_VDDIO))
  687. early_delay(500);
  688. else {
  689. while (!(readl(&power_regs->hw_power_sts) &
  690. POWER_STS_DC_OK))
  691. ;
  692. }
  693. cur_target = readl(&power_regs->hw_power_vddioctrl);
  694. cur_target &= POWER_VDDIOCTRL_TRG_MASK;
  695. cur_target *= 50; /* 50 mV step*/
  696. cur_target += 2800; /* 2800 mV lowest */
  697. } while (new_target < cur_target);
  698. }
  699. clrsetbits_le32(&power_regs->hw_power_vddioctrl,
  700. POWER_VDDDCTRL_BO_OFFSET_MASK,
  701. new_brownout << POWER_VDDDCTRL_BO_OFFSET_OFFSET);
  702. }
  703. void mx28_power_set_vddd(uint32_t new_target, uint32_t new_brownout)
  704. {
  705. struct mxs_power_regs *power_regs =
  706. (struct mxs_power_regs *)MXS_POWER_BASE;
  707. uint32_t cur_target, diff, bo_int = 0;
  708. uint32_t powered_by_linreg = 0;
  709. new_brownout = new_target - new_brownout;
  710. cur_target = readl(&power_regs->hw_power_vdddctrl);
  711. cur_target &= POWER_VDDDCTRL_TRG_MASK;
  712. cur_target *= 25; /* 25 mV step*/
  713. cur_target += 800; /* 800 mV lowest */
  714. powered_by_linreg = mx28_get_vddd_power_source_off();
  715. if (new_target > cur_target) {
  716. if (powered_by_linreg) {
  717. bo_int = readl(&power_regs->hw_power_vdddctrl);
  718. clrbits_le32(&power_regs->hw_power_vdddctrl,
  719. POWER_CTRL_ENIRQ_VDDD_BO);
  720. }
  721. setbits_le32(&power_regs->hw_power_vdddctrl,
  722. POWER_VDDDCTRL_BO_OFFSET_MASK);
  723. do {
  724. if (new_target - cur_target > 100)
  725. diff = cur_target + 100;
  726. else
  727. diff = new_target;
  728. diff -= 800;
  729. diff /= 25;
  730. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  731. POWER_VDDDCTRL_TRG_MASK, diff);
  732. if (powered_by_linreg ||
  733. (readl(&power_regs->hw_power_sts) &
  734. POWER_STS_VDD5V_GT_VDDIO))
  735. early_delay(500);
  736. else {
  737. while (!(readl(&power_regs->hw_power_sts) &
  738. POWER_STS_DC_OK))
  739. ;
  740. }
  741. cur_target = readl(&power_regs->hw_power_vdddctrl);
  742. cur_target &= POWER_VDDDCTRL_TRG_MASK;
  743. cur_target *= 25; /* 25 mV step*/
  744. cur_target += 800; /* 800 mV lowest */
  745. } while (new_target > cur_target);
  746. if (powered_by_linreg) {
  747. writel(POWER_CTRL_VDDD_BO_IRQ,
  748. &power_regs->hw_power_ctrl_clr);
  749. if (bo_int & POWER_CTRL_ENIRQ_VDDD_BO)
  750. setbits_le32(&power_regs->hw_power_vdddctrl,
  751. POWER_CTRL_ENIRQ_VDDD_BO);
  752. }
  753. } else {
  754. do {
  755. if (cur_target - new_target > 100)
  756. diff = cur_target - 100;
  757. else
  758. diff = new_target;
  759. diff -= 800;
  760. diff /= 25;
  761. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  762. POWER_VDDDCTRL_TRG_MASK, diff);
  763. if (powered_by_linreg ||
  764. (readl(&power_regs->hw_power_sts) &
  765. POWER_STS_VDD5V_GT_VDDIO))
  766. early_delay(500);
  767. else {
  768. while (!(readl(&power_regs->hw_power_sts) &
  769. POWER_STS_DC_OK))
  770. ;
  771. }
  772. cur_target = readl(&power_regs->hw_power_vdddctrl);
  773. cur_target &= POWER_VDDDCTRL_TRG_MASK;
  774. cur_target *= 25; /* 25 mV step*/
  775. cur_target += 800; /* 800 mV lowest */
  776. } while (new_target < cur_target);
  777. }
  778. clrsetbits_le32(&power_regs->hw_power_vdddctrl,
  779. POWER_VDDDCTRL_BO_OFFSET_MASK,
  780. new_brownout << POWER_VDDDCTRL_BO_OFFSET_OFFSET);
  781. }
  782. void mx28_setup_batt_detect(void)
  783. {
  784. mx28_lradc_init();
  785. mx28_lradc_enable_batt_measurement();
  786. early_delay(10);
  787. }
  788. void mx28_power_init(void)
  789. {
  790. struct mxs_power_regs *power_regs =
  791. (struct mxs_power_regs *)MXS_POWER_BASE;
  792. mx28_power_clock2xtal();
  793. mx28_power_clear_auto_restart();
  794. mx28_power_set_linreg();
  795. mx28_power_setup_5v_detect();
  796. mx28_setup_batt_detect();
  797. mx28_power_configure_power_source();
  798. mx28_enable_output_rail_protection();
  799. mx28_power_set_vddio(3300, 3150);
  800. mx28_power_set_vddd(1350, 1200);
  801. writel(POWER_CTRL_VDDD_BO_IRQ | POWER_CTRL_VDDA_BO_IRQ |
  802. POWER_CTRL_VDDIO_BO_IRQ | POWER_CTRL_VDD5V_DROOP_IRQ |
  803. POWER_CTRL_VBUS_VALID_IRQ | POWER_CTRL_BATT_BO_IRQ |
  804. POWER_CTRL_DCDC4P2_BO_IRQ, &power_regs->hw_power_ctrl_clr);
  805. writel(POWER_5VCTRL_PWDN_5VBRNOUT, &power_regs->hw_power_5vctrl_set);
  806. early_delay(1000);
  807. }
  808. #ifdef CONFIG_SPL_MX28_PSWITCH_WAIT
  809. void mx28_power_wait_pswitch(void)
  810. {
  811. struct mxs_power_regs *power_regs =
  812. (struct mxs_power_regs *)MXS_POWER_BASE;
  813. while (!(readl(&power_regs->hw_power_sts) & POWER_STS_PSWITCH_MASK))
  814. ;
  815. }
  816. #endif