pmu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 = (ai_get_pmucaps(sih) & PCAP_RC_MASK) >> PCAP_RC_SHIFT;
  105. /* determine min/max rsrc masks */
  106. switch (ai_get_chip_id(sih)) {
  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 bcma_device *core,
  126. u8 spuravoid)
  127. {
  128. u32 tmp = 0;
  129. switch (ai_get_chip_id(sih)) {
  130. case BCM43224_CHIP_ID:
  131. case BCM43225_CHIP_ID:
  132. if (spuravoid == 1) {
  133. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  134. PMU1_PLL0_PLLCTL0);
  135. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  136. 0x11500010);
  137. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  138. PMU1_PLL0_PLLCTL1);
  139. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  140. 0x000C0C06);
  141. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  142. PMU1_PLL0_PLLCTL2);
  143. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  144. 0x0F600a08);
  145. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  146. PMU1_PLL0_PLLCTL3);
  147. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  148. 0x00000000);
  149. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  150. PMU1_PLL0_PLLCTL4);
  151. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  152. 0x2001E920);
  153. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  154. PMU1_PLL0_PLLCTL5);
  155. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  156. 0x88888815);
  157. } else {
  158. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  159. PMU1_PLL0_PLLCTL0);
  160. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  161. 0x11100010);
  162. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  163. PMU1_PLL0_PLLCTL1);
  164. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  165. 0x000c0c06);
  166. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  167. PMU1_PLL0_PLLCTL2);
  168. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  169. 0x03000a08);
  170. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  171. PMU1_PLL0_PLLCTL3);
  172. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  173. 0x00000000);
  174. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  175. PMU1_PLL0_PLLCTL4);
  176. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  177. 0x200005c0);
  178. bcma_write32(core, CHIPCREGOFFS(pllcontrol_addr),
  179. PMU1_PLL0_PLLCTL5);
  180. bcma_write32(core, CHIPCREGOFFS(pllcontrol_data),
  181. 0x88888815);
  182. }
  183. tmp = 1 << 10;
  184. break;
  185. default:
  186. /* bail out */
  187. return;
  188. }
  189. bcma_set32(core, CHIPCREGOFFS(pmucontrol), tmp);
  190. }
  191. u16 si_pmu_fast_pwrup_delay(struct si_pub *sih)
  192. {
  193. uint delay = PMU_MAX_TRANSITION_DLY;
  194. switch (ai_get_chip_id(sih)) {
  195. case BCM43224_CHIP_ID:
  196. case BCM43225_CHIP_ID:
  197. case BCM4313_CHIP_ID:
  198. delay = 3700;
  199. break;
  200. default:
  201. break;
  202. }
  203. return (u16) delay;
  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_cc_reg(sih, offsetof(struct chipcregs, chipcontrol_addr), ~0, reg);
  209. return ai_cc_reg(sih, offsetof(struct chipcregs, chipcontrol_data),
  210. mask, val);
  211. }
  212. /* Read/write a regcontrol reg */
  213. u32 si_pmu_regcontrol(struct si_pub *sih, uint reg, u32 mask, u32 val)
  214. {
  215. ai_cc_reg(sih, offsetof(struct chipcregs, regcontrol_addr), ~0, reg);
  216. return ai_cc_reg(sih, offsetof(struct chipcregs, regcontrol_data),
  217. mask, val);
  218. }
  219. /* Read/write a pllcontrol reg */
  220. u32 si_pmu_pllcontrol(struct si_pub *sih, uint reg, u32 mask, u32 val)
  221. {
  222. ai_cc_reg(sih, offsetof(struct chipcregs, pllcontrol_addr), ~0, reg);
  223. return ai_cc_reg(sih, offsetof(struct chipcregs, pllcontrol_data),
  224. mask, val);
  225. }
  226. /* PMU PLL update */
  227. void si_pmu_pllupd(struct si_pub *sih)
  228. {
  229. ai_cc_reg(sih, offsetof(struct chipcregs, pmucontrol),
  230. PCTL_PLL_PLLCTL_UPD, PCTL_PLL_PLLCTL_UPD);
  231. }
  232. /* query alp/xtal clock frequency */
  233. u32 si_pmu_alp_clock(struct si_pub *sih)
  234. {
  235. u32 clock = ALP_CLOCK;
  236. /* bail out with default */
  237. if (!(ai_get_cccaps(sih) & CC_CAP_PMU))
  238. return clock;
  239. switch (ai_get_chip_id(sih)) {
  240. case BCM43224_CHIP_ID:
  241. case BCM43225_CHIP_ID:
  242. case BCM4313_CHIP_ID:
  243. /* always 20Mhz */
  244. clock = 20000 * 1000;
  245. break;
  246. default:
  247. break;
  248. }
  249. return clock;
  250. }
  251. void si_pmu_spuravoid(struct si_pub *sih, u8 spuravoid)
  252. {
  253. struct bcma_device *cc;
  254. uint origidx, intr_val;
  255. /* switch to chipc */
  256. cc = ai_findcore(sih, BCMA_CORE_CHIPCOMMON, 0);
  257. ai_switch_core(sih, CC_CORE_ID, &origidx, &intr_val);
  258. /* update the pll changes */
  259. si_pmu_spuravoid_pllupdate(sih, cc, spuravoid);
  260. /* Return to original core */
  261. ai_restore_core(sih, origidx, intr_val);
  262. }
  263. /* initialize PMU */
  264. void si_pmu_init(struct si_pub *sih)
  265. {
  266. struct bcma_device *core;
  267. /* select chipc */
  268. core = ai_findcore(sih, BCMA_CORE_CHIPCOMMON, 0);
  269. if (ai_get_pmurev(sih) == 1)
  270. bcma_mask32(core, CHIPCREGOFFS(pmucontrol),
  271. ~PCTL_NOILP_ON_WAIT);
  272. else if (ai_get_pmurev(sih) >= 2)
  273. bcma_set32(core, CHIPCREGOFFS(pmucontrol), PCTL_NOILP_ON_WAIT);
  274. }
  275. /* initialize PMU resources */
  276. void si_pmu_res_init(struct si_pub *sih)
  277. {
  278. struct bcma_device *core;
  279. u32 min_mask = 0, max_mask = 0;
  280. /* select to chipc */
  281. core = ai_findcore(sih, BCMA_CORE_CHIPCOMMON, 0);
  282. /* Determine min/max rsrc masks */
  283. si_pmu_res_masks(sih, &min_mask, &max_mask);
  284. /* It is required to program max_mask first and then min_mask */
  285. /* Program max resource mask */
  286. if (max_mask)
  287. bcma_write32(core, CHIPCREGOFFS(max_res_mask), max_mask);
  288. /* Program min resource mask */
  289. if (min_mask)
  290. bcma_write32(core, CHIPCREGOFFS(min_res_mask), min_mask);
  291. /* Add some delay; allow resources to come up and settle. */
  292. mdelay(2);
  293. }
  294. u32 si_pmu_measure_alpclk(struct si_pub *sih)
  295. {
  296. struct bcma_device *core;
  297. u32 alp_khz;
  298. if (ai_get_pmurev(sih) < 10)
  299. return 0;
  300. /* Remember original core before switch to chipc */
  301. core = ai_findcore(sih, BCMA_CORE_CHIPCOMMON, 0);
  302. if (bcma_read32(core, CHIPCREGOFFS(pmustatus)) & PST_EXTLPOAVAIL) {
  303. u32 ilp_ctr, alp_hz;
  304. /*
  305. * Enable the reg to measure the freq,
  306. * in case it was disabled before
  307. */
  308. bcma_write32(core, CHIPCREGOFFS(pmu_xtalfreq),
  309. 1U << PMU_XTALFREQ_REG_MEASURE_SHIFT);
  310. /* Delay for well over 4 ILP clocks */
  311. udelay(1000);
  312. /* Read the latched number of ALP ticks per 4 ILP ticks */
  313. ilp_ctr = bcma_read32(core, CHIPCREGOFFS(pmu_xtalfreq)) &
  314. PMU_XTALFREQ_REG_ILPCTR_MASK;
  315. /*
  316. * Turn off the PMU_XTALFREQ_REG_MEASURE_SHIFT
  317. * bit to save power
  318. */
  319. bcma_write32(core, CHIPCREGOFFS(pmu_xtalfreq), 0);
  320. /* Calculate ALP frequency */
  321. alp_hz = (ilp_ctr * EXT_ILP_HZ) / 4;
  322. /*
  323. * Round to nearest 100KHz, and at
  324. * the same time convert to KHz
  325. */
  326. alp_khz = (alp_hz + 50000) / 100000 * 100;
  327. } else
  328. alp_khz = 0;
  329. return alp_khz;
  330. }