pmu.c 9.8 KB

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