nv50_pm.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_pm.h"
  27. /*XXX: boards using limits 0x40 need fixing, the register layout
  28. * is correct here, but, there's some other funny magic
  29. * that modifies things, so it's not likely we'll set/read
  30. * the correct timings yet.. working on it...
  31. */
  32. struct nv50_pm_state {
  33. struct pll_lims pll;
  34. enum pll_types type;
  35. int N, M, P;
  36. };
  37. int
  38. nv50_pm_clock_get(struct drm_device *dev, u32 id)
  39. {
  40. struct pll_lims pll;
  41. int P, N, M, ret;
  42. u32 reg0, reg1;
  43. ret = get_pll_limits(dev, id, &pll);
  44. if (ret)
  45. return ret;
  46. if (pll.vco2.maxfreq) {
  47. reg0 = nv_rd32(dev, pll.reg + 0);
  48. reg1 = nv_rd32(dev, pll.reg + 4);
  49. P = (reg0 & 0x00070000) >> 16;
  50. N = (reg1 & 0x0000ff00) >> 8;
  51. M = (reg1 & 0x000000ff);
  52. return ((pll.refclk * N / M) >> P);
  53. }
  54. reg0 = nv_rd32(dev, pll.reg + 4);
  55. P = (reg0 & 0x003f0000) >> 16;
  56. N = (reg0 & 0x0000ff00) >> 8;
  57. M = (reg0 & 0x000000ff);
  58. return pll.refclk * N / M / P;
  59. }
  60. void *
  61. nv50_pm_clock_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl,
  62. u32 id, int khz)
  63. {
  64. struct nv50_pm_state *state;
  65. int dummy, ret;
  66. state = kzalloc(sizeof(*state), GFP_KERNEL);
  67. if (!state)
  68. return ERR_PTR(-ENOMEM);
  69. state->type = id;
  70. ret = get_pll_limits(dev, id, &state->pll);
  71. if (ret < 0) {
  72. kfree(state);
  73. return (ret == -ENOENT) ? NULL : ERR_PTR(ret);
  74. }
  75. ret = nv50_calc_pll(dev, &state->pll, khz, &state->N, &state->M,
  76. &dummy, &dummy, &state->P);
  77. if (ret < 0) {
  78. kfree(state);
  79. return ERR_PTR(ret);
  80. }
  81. return state;
  82. }
  83. void
  84. nv50_pm_clock_set(struct drm_device *dev, void *pre_state)
  85. {
  86. struct nv50_pm_state *state = pre_state;
  87. u32 reg = state->pll.reg, tmp;
  88. int N = state->N;
  89. int M = state->M;
  90. int P = state->P;
  91. if (state->pll.vco2.maxfreq) {
  92. if (state->type == PLL_MEMORY) {
  93. nv_wr32(dev, 0x100210, 0);
  94. nv_wr32(dev, 0x1002dc, 1);
  95. }
  96. tmp = nv_rd32(dev, reg + 0) & 0xfff8ffff;
  97. tmp |= 0x80000000 | (P << 16);
  98. nv_wr32(dev, reg + 0, tmp);
  99. nv_wr32(dev, reg + 4, (N << 8) | M);
  100. if (state->type == PLL_MEMORY) {
  101. nv_wr32(dev, 0x1002dc, 0);
  102. nv_wr32(dev, 0x100210, 0x80000000);
  103. }
  104. } else {
  105. nv_wr32(dev, reg + 4, (P << 16) | (N << 8) | M);
  106. }
  107. kfree(state);
  108. }