pmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright (c) 2011 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/io.h>
  18. #include <brcm_hw_ids.h>
  19. #include <chipcommon.h>
  20. #include <brcmu_utils.h>
  21. #include "pub.h"
  22. #include "aiutils.h"
  23. #include "pmu.h"
  24. #include "soc.h"
  25. /*
  26. * external LPO crystal frequency
  27. */
  28. #define EXT_ILP_HZ 32768
  29. /*
  30. * Duration for ILP clock frequency measurment in milliseconds
  31. *
  32. * remark: 1000 must be an integer multiple of this duration
  33. */
  34. #define ILP_CALC_DUR 10
  35. /* Fields in pmucontrol */
  36. #define PCTL_ILP_DIV_MASK 0xffff0000
  37. #define PCTL_ILP_DIV_SHIFT 16
  38. #define PCTL_PLL_PLLCTL_UPD 0x00000400 /* rev 2 */
  39. #define PCTL_NOILP_ON_WAIT 0x00000200 /* rev 1 */
  40. #define PCTL_HT_REQ_EN 0x00000100
  41. #define PCTL_ALP_REQ_EN 0x00000080
  42. #define PCTL_XTALFREQ_MASK 0x0000007c
  43. #define PCTL_XTALFREQ_SHIFT 2
  44. #define PCTL_ILP_DIV_EN 0x00000002
  45. #define PCTL_LPO_SEL 0x00000001
  46. /* ILP clock */
  47. #define ILP_CLOCK 32000
  48. /* ALP clock on pre-PMU chips */
  49. #define ALP_CLOCK 20000000
  50. /* pmustatus */
  51. #define PST_EXTLPOAVAIL 0x0100
  52. #define PST_WDRESET 0x0080
  53. #define PST_INTPEND 0x0040
  54. #define PST_SBCLKST 0x0030
  55. #define PST_SBCLKST_ILP 0x0010
  56. #define PST_SBCLKST_ALP 0x0020
  57. #define PST_SBCLKST_HT 0x0030
  58. #define PST_ALPAVAIL 0x0008
  59. #define PST_HTAVAIL 0x0004
  60. #define PST_RESINIT 0x0003
  61. /* PMU resource bit position */
  62. #define PMURES_BIT(bit) (1 << (bit))
  63. /* PMU corerev and chip specific PLL controls.
  64. * PMU<rev>_PLL<num>_XX where <rev> is PMU corerev and <num> is an arbitrary
  65. * number to differentiate different PLLs controlled by the same PMU rev.
  66. */
  67. /* pllcontrol registers:
  68. * ndiv_pwrdn, pwrdn_ch<x>, refcomp_pwrdn, dly_ch<x>,
  69. * p1div, p2div, _bypass_sdmod
  70. */
  71. #define PMU1_PLL0_PLLCTL0 0
  72. #define PMU1_PLL0_PLLCTL1 1
  73. #define PMU1_PLL0_PLLCTL2 2
  74. #define PMU1_PLL0_PLLCTL3 3
  75. #define PMU1_PLL0_PLLCTL4 4
  76. #define PMU1_PLL0_PLLCTL5 5
  77. /* pmu XtalFreqRatio */
  78. #define PMU_XTALFREQ_REG_ILPCTR_MASK 0x00001FFF
  79. #define PMU_XTALFREQ_REG_MEASURE_MASK 0x80000000
  80. #define PMU_XTALFREQ_REG_MEASURE_SHIFT 31
  81. /* 4313 resources */
  82. #define RES4313_BB_PU_RSRC 0
  83. #define RES4313_ILP_REQ_RSRC 1
  84. #define RES4313_XTAL_PU_RSRC 2
  85. #define RES4313_ALP_AVAIL_RSRC 3
  86. #define RES4313_RADIO_PU_RSRC 4
  87. #define RES4313_BG_PU_RSRC 5
  88. #define RES4313_VREG1P4_PU_RSRC 6
  89. #define RES4313_AFE_PWRSW_RSRC 7
  90. #define RES4313_RX_PWRSW_RSRC 8
  91. #define RES4313_TX_PWRSW_RSRC 9
  92. #define RES4313_BB_PWRSW_RSRC 10
  93. #define RES4313_SYNTH_PWRSW_RSRC 11
  94. #define RES4313_MISC_PWRSW_RSRC 12
  95. #define RES4313_BB_PLL_PWRSW_RSRC 13
  96. #define RES4313_HT_AVAIL_RSRC 14
  97. #define RES4313_MACPHY_CLK_AVAIL_RSRC 15
  98. /* Determine min/max rsrc masks. Value 0 leaves hardware at default. */
  99. static void si_pmu_res_masks(struct si_pub *sih, u32 * pmin, u32 * pmax)
  100. {
  101. u32 min_mask = 0, max_mask = 0;
  102. uint rsrcs;
  103. /* # resources */
  104. rsrcs = (sih->pmucaps & PCAP_RC_MASK) >> PCAP_RC_SHIFT;
  105. /* determine min/max rsrc masks */
  106. switch (sih->chip) {
  107. case BCM43224_CHIP_ID:
  108. case BCM43225_CHIP_ID:
  109. /* ??? */
  110. break;
  111. case BCM4313_CHIP_ID:
  112. min_mask = PMURES_BIT(RES4313_BB_PU_RSRC) |
  113. PMURES_BIT(RES4313_XTAL_PU_RSRC) |
  114. PMURES_BIT(RES4313_ALP_AVAIL_RSRC) |
  115. PMURES_BIT(RES4313_BB_PLL_PWRSW_RSRC);
  116. max_mask = 0xffff;
  117. break;
  118. default:
  119. break;
  120. }
  121. *pmin = min_mask;
  122. *pmax = max_mask;
  123. }
  124. static void
  125. si_pmu_spuravoid_pllupdate(struct si_pub *sih, struct chipcregs __iomem *cc,
  126. u8 spuravoid)
  127. {
  128. u32 tmp = 0;
  129. switch (sih->chip) {
  130. case BCM43224_CHIP_ID:
  131. case BCM43225_CHIP_ID:
  132. if (spuravoid == 1) {
  133. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL0);
  134. W_REG(&cc->pllcontrol_data, 0x11500010);
  135. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL1);
  136. W_REG(&cc->pllcontrol_data, 0x000C0C06);
  137. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL2);
  138. W_REG(&cc->pllcontrol_data, 0x0F600a08);
  139. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL3);
  140. W_REG(&cc->pllcontrol_data, 0x00000000);
  141. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL4);
  142. W_REG(&cc->pllcontrol_data, 0x2001E920);
  143. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL5);
  144. W_REG(&cc->pllcontrol_data, 0x88888815);
  145. } else {
  146. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL0);
  147. W_REG(&cc->pllcontrol_data, 0x11100010);
  148. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL1);
  149. W_REG(&cc->pllcontrol_data, 0x000c0c06);
  150. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL2);
  151. W_REG(&cc->pllcontrol_data, 0x03000a08);
  152. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL3);
  153. W_REG(&cc->pllcontrol_data, 0x00000000);
  154. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL4);
  155. W_REG(&cc->pllcontrol_data, 0x200005c0);
  156. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL5);
  157. W_REG(&cc->pllcontrol_data, 0x88888815);
  158. }
  159. tmp = 1 << 10;
  160. break;
  161. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL0);
  162. W_REG(&cc->pllcontrol_data, 0x11100008);
  163. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL1);
  164. W_REG(&cc->pllcontrol_data, 0x0c000c06);
  165. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL2);
  166. W_REG(&cc->pllcontrol_data, 0x03000a08);
  167. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL3);
  168. W_REG(&cc->pllcontrol_data, 0x00000000);
  169. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL4);
  170. W_REG(&cc->pllcontrol_data, 0x200005c0);
  171. W_REG(&cc->pllcontrol_addr, PMU1_PLL0_PLLCTL5);
  172. W_REG(&cc->pllcontrol_data, 0x88888855);
  173. tmp = 1 << 10;
  174. break;
  175. default:
  176. /* bail out */
  177. return;
  178. }
  179. tmp |= R_REG(&cc->pmucontrol);
  180. W_REG(&cc->pmucontrol, tmp);
  181. }
  182. u16 si_pmu_fast_pwrup_delay(struct si_pub *sih)
  183. {
  184. uint delay = PMU_MAX_TRANSITION_DLY;
  185. switch (sih->chip) {
  186. case BCM43224_CHIP_ID:
  187. case BCM43225_CHIP_ID:
  188. case BCM4313_CHIP_ID:
  189. delay = 3700;
  190. break;
  191. default:
  192. break;
  193. }
  194. return (u16) delay;
  195. }
  196. void si_pmu_sprom_enable(struct si_pub *sih, bool enable)
  197. {
  198. struct chipcregs __iomem *cc;
  199. uint origidx;
  200. /* Remember original core before switch to chipc */
  201. origidx = ai_coreidx(sih);
  202. cc = ai_setcoreidx(sih, SI_CC_IDX);
  203. /* Return to original core */
  204. ai_setcoreidx(sih, origidx);
  205. }
  206. /* Read/write a chipcontrol reg */
  207. u32 si_pmu_chipcontrol(struct si_pub *sih, uint reg, u32 mask, u32 val)
  208. {
  209. ai_corereg(sih, SI_CC_IDX, offsetof(struct chipcregs, chipcontrol_addr),
  210. ~0, reg);
  211. return ai_corereg(sih, SI_CC_IDX,
  212. offsetof(struct chipcregs, chipcontrol_data), mask,
  213. val);
  214. }
  215. /* Read/write a regcontrol reg */
  216. u32 si_pmu_regcontrol(struct si_pub *sih, uint reg, u32 mask, u32 val)
  217. {
  218. ai_corereg(sih, SI_CC_IDX, offsetof(struct chipcregs, regcontrol_addr),
  219. ~0, reg);
  220. return ai_corereg(sih, SI_CC_IDX,
  221. offsetof(struct chipcregs, regcontrol_data), mask,
  222. val);
  223. }
  224. /* Read/write a pllcontrol reg */
  225. u32 si_pmu_pllcontrol(struct si_pub *sih, uint reg, u32 mask, u32 val)
  226. {
  227. ai_corereg(sih, SI_CC_IDX, offsetof(struct chipcregs, pllcontrol_addr),
  228. ~0, reg);
  229. return ai_corereg(sih, SI_CC_IDX,
  230. offsetof(struct chipcregs, pllcontrol_data), mask,
  231. val);
  232. }
  233. /* PMU PLL update */
  234. void si_pmu_pllupd(struct si_pub *sih)
  235. {
  236. ai_corereg(sih, SI_CC_IDX, offsetof(struct chipcregs, pmucontrol),
  237. PCTL_PLL_PLLCTL_UPD, PCTL_PLL_PLLCTL_UPD);
  238. }
  239. /* query alp/xtal clock frequency */
  240. u32 si_pmu_alp_clock(struct si_pub *sih)
  241. {
  242. u32 clock = ALP_CLOCK;
  243. /* bail out with default */
  244. if (!(sih->cccaps & CC_CAP_PMU))
  245. return clock;
  246. switch (sih->chip) {
  247. case BCM43224_CHIP_ID:
  248. case BCM43225_CHIP_ID:
  249. case BCM4313_CHIP_ID:
  250. /* always 20Mhz */
  251. clock = 20000 * 1000;
  252. break;
  253. default:
  254. break;
  255. }
  256. return clock;
  257. }
  258. void si_pmu_spuravoid(struct si_pub *sih, u8 spuravoid)
  259. {
  260. struct chipcregs __iomem *cc;
  261. uint origidx, intr_val;
  262. /* Remember original core before switch to chipc */
  263. cc = (struct chipcregs __iomem *)
  264. ai_switch_core(sih, CC_CORE_ID, &origidx, &intr_val);
  265. /* update the pll changes */
  266. si_pmu_spuravoid_pllupdate(sih, cc, spuravoid);
  267. /* Return to original core */
  268. ai_restore_core(sih, origidx, intr_val);
  269. }
  270. /* initialize PMU */
  271. void si_pmu_init(struct si_pub *sih)
  272. {
  273. struct chipcregs __iomem *cc;
  274. uint origidx;
  275. /* Remember original core before switch to chipc */
  276. origidx = ai_coreidx(sih);
  277. cc = ai_setcoreidx(sih, SI_CC_IDX);
  278. if (sih->pmurev == 1)
  279. AND_REG(&cc->pmucontrol, ~PCTL_NOILP_ON_WAIT);
  280. else if (sih->pmurev >= 2)
  281. OR_REG(&cc->pmucontrol, PCTL_NOILP_ON_WAIT);
  282. /* Return to original core */
  283. ai_setcoreidx(sih, origidx);
  284. }
  285. /* initialize PMU chip controls and other chip level stuff */
  286. void si_pmu_chip_init(struct si_pub *sih)
  287. {
  288. uint origidx;
  289. /* Gate off SPROM clock and chip select signals */
  290. si_pmu_sprom_enable(sih, false);
  291. /* Remember original core */
  292. origidx = ai_coreidx(sih);
  293. /* Return to original core */
  294. ai_setcoreidx(sih, origidx);
  295. }
  296. /* initialize PMU switch/regulators */
  297. void si_pmu_swreg_init(struct si_pub *sih)
  298. {
  299. }
  300. /* initialize PLL */
  301. void si_pmu_pll_init(struct si_pub *sih, uint xtalfreq)
  302. {
  303. struct chipcregs __iomem *cc;
  304. uint origidx;
  305. /* Remember original core before switch to chipc */
  306. origidx = ai_coreidx(sih);
  307. cc = ai_setcoreidx(sih, SI_CC_IDX);
  308. switch (sih->chip) {
  309. case BCM4313_CHIP_ID:
  310. case BCM43224_CHIP_ID:
  311. case BCM43225_CHIP_ID:
  312. /* ??? */
  313. break;
  314. default:
  315. break;
  316. }
  317. /* Return to original core */
  318. ai_setcoreidx(sih, origidx);
  319. }
  320. /* initialize PMU resources */
  321. void si_pmu_res_init(struct si_pub *sih)
  322. {
  323. struct chipcregs __iomem *cc;
  324. uint origidx;
  325. u32 min_mask = 0, max_mask = 0;
  326. /* Remember original core before switch to chipc */
  327. origidx = ai_coreidx(sih);
  328. cc = ai_setcoreidx(sih, SI_CC_IDX);
  329. /* Determine min/max rsrc masks */
  330. si_pmu_res_masks(sih, &min_mask, &max_mask);
  331. /* It is required to program max_mask first and then min_mask */
  332. /* Program max resource mask */
  333. if (max_mask)
  334. W_REG(&cc->max_res_mask, max_mask);
  335. /* Program min resource mask */
  336. if (min_mask)
  337. W_REG(&cc->min_res_mask, min_mask);
  338. /* Add some delay; allow resources to come up and settle. */
  339. mdelay(2);
  340. /* Return to original core */
  341. ai_setcoreidx(sih, origidx);
  342. }
  343. u32 si_pmu_measure_alpclk(struct si_pub *sih)
  344. {
  345. struct chipcregs __iomem *cc;
  346. uint origidx;
  347. u32 alp_khz;
  348. if (sih->pmurev < 10)
  349. return 0;
  350. /* Remember original core before switch to chipc */
  351. origidx = ai_coreidx(sih);
  352. cc = ai_setcoreidx(sih, SI_CC_IDX);
  353. if (R_REG(&cc->pmustatus) & PST_EXTLPOAVAIL) {
  354. u32 ilp_ctr, alp_hz;
  355. /*
  356. * Enable the reg to measure the freq,
  357. * in case it was disabled before
  358. */
  359. W_REG(&cc->pmu_xtalfreq,
  360. 1U << PMU_XTALFREQ_REG_MEASURE_SHIFT);
  361. /* Delay for well over 4 ILP clocks */
  362. udelay(1000);
  363. /* Read the latched number of ALP ticks per 4 ILP ticks */
  364. ilp_ctr =
  365. R_REG(&cc->pmu_xtalfreq) & PMU_XTALFREQ_REG_ILPCTR_MASK;
  366. /*
  367. * Turn off the PMU_XTALFREQ_REG_MEASURE_SHIFT
  368. * bit to save power
  369. */
  370. W_REG(&cc->pmu_xtalfreq, 0);
  371. /* Calculate ALP frequency */
  372. alp_hz = (ilp_ctr * EXT_ILP_HZ) / 4;
  373. /*
  374. * Round to nearest 100KHz, and at
  375. * the same time convert to KHz
  376. */
  377. alp_khz = (alp_hz + 50000) / 100000 * 100;
  378. } else
  379. alp_khz = 0;
  380. /* Return to original core */
  381. ai_setcoreidx(sih, origidx);
  382. return alp_khz;
  383. }