pmu.c 10 KB

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