pmu.c 11 KB

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