spl_power_init.c 37 KB

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