nv50_pm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_bios.h"
  27. #include "nouveau_pm.h"
  28. struct nv50_pm_state {
  29. struct nouveau_pm_level *perflvl;
  30. struct pll_lims pll;
  31. enum pll_types type;
  32. int N, M, P;
  33. };
  34. int
  35. nv50_pm_clock_get(struct drm_device *dev, u32 id)
  36. {
  37. struct pll_lims pll;
  38. int P, N, M, ret;
  39. u32 reg0, reg1;
  40. ret = get_pll_limits(dev, id, &pll);
  41. if (ret)
  42. return ret;
  43. reg0 = nv_rd32(dev, pll.reg + 0);
  44. reg1 = nv_rd32(dev, pll.reg + 4);
  45. if ((reg0 & 0x80000000) == 0) {
  46. if (id == PLL_SHADER) {
  47. NV_DEBUG(dev, "Shader PLL is disabled. "
  48. "Shader clock is twice the core\n");
  49. ret = nv50_pm_clock_get(dev, PLL_CORE);
  50. if (ret > 0)
  51. return ret << 1;
  52. } else if (id == PLL_MEMORY) {
  53. NV_DEBUG(dev, "Memory PLL is disabled. "
  54. "Memory clock is equal to the ref_clk\n");
  55. return pll.refclk;
  56. }
  57. }
  58. P = (reg0 & 0x00070000) >> 16;
  59. N = (reg1 & 0x0000ff00) >> 8;
  60. M = (reg1 & 0x000000ff);
  61. return ((pll.refclk * N / M) >> P);
  62. }
  63. void *
  64. nv50_pm_clock_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl,
  65. u32 id, int khz)
  66. {
  67. struct nv50_pm_state *state;
  68. int dummy, ret;
  69. state = kzalloc(sizeof(*state), GFP_KERNEL);
  70. if (!state)
  71. return ERR_PTR(-ENOMEM);
  72. state->type = id;
  73. state->perflvl = perflvl;
  74. ret = get_pll_limits(dev, id, &state->pll);
  75. if (ret < 0) {
  76. kfree(state);
  77. return (ret == -ENOENT) ? NULL : ERR_PTR(ret);
  78. }
  79. ret = nv50_calc_pll(dev, &state->pll, khz, &state->N, &state->M,
  80. &dummy, &dummy, &state->P);
  81. if (ret < 0) {
  82. kfree(state);
  83. return ERR_PTR(ret);
  84. }
  85. return state;
  86. }
  87. void
  88. nv50_pm_clock_set(struct drm_device *dev, void *pre_state)
  89. {
  90. struct nv50_pm_state *state = pre_state;
  91. struct nouveau_pm_level *perflvl = state->perflvl;
  92. u32 reg = state->pll.reg, tmp;
  93. struct bit_entry BIT_M;
  94. u16 script;
  95. int N = state->N;
  96. int M = state->M;
  97. int P = state->P;
  98. if (state->type == PLL_MEMORY && perflvl->memscript &&
  99. bit_table(dev, 'M', &BIT_M) == 0 &&
  100. BIT_M.version == 1 && BIT_M.length >= 0x0b) {
  101. script = ROM16(BIT_M.data[0x05]);
  102. if (script)
  103. nouveau_bios_run_init_table(dev, script, NULL, -1);
  104. script = ROM16(BIT_M.data[0x07]);
  105. if (script)
  106. nouveau_bios_run_init_table(dev, script, NULL, -1);
  107. script = ROM16(BIT_M.data[0x09]);
  108. if (script)
  109. nouveau_bios_run_init_table(dev, script, NULL, -1);
  110. nouveau_bios_run_init_table(dev, perflvl->memscript, NULL, -1);
  111. }
  112. if (state->type == PLL_MEMORY) {
  113. nv_wr32(dev, 0x100210, 0);
  114. nv_wr32(dev, 0x1002dc, 1);
  115. }
  116. tmp = nv_rd32(dev, reg + 0) & 0xfff8ffff;
  117. tmp |= 0x80000000 | (P << 16);
  118. nv_wr32(dev, reg + 0, tmp);
  119. nv_wr32(dev, reg + 4, (N << 8) | M);
  120. if (state->type == PLL_MEMORY) {
  121. nv_wr32(dev, 0x1002dc, 0);
  122. nv_wr32(dev, 0x100210, 0x80000000);
  123. }
  124. kfree(state);
  125. }
  126. struct pwm_info {
  127. int id;
  128. int invert;
  129. u8 tag;
  130. u32 ctrl;
  131. int line;
  132. };
  133. static int
  134. nv50_pm_fanspeed_pwm(struct drm_device *dev, struct pwm_info *pwm)
  135. {
  136. struct dcb_gpio_entry *gpio;
  137. gpio = nouveau_bios_gpio_entry(dev, 0x09);
  138. if (gpio) {
  139. pwm->tag = gpio->tag;
  140. pwm->id = (gpio->line == 9) ? 1 : 0;
  141. pwm->invert = gpio->state[0] & 1;
  142. pwm->ctrl = (gpio->line < 16) ? 0xe100 : 0xe28c;
  143. pwm->line = (gpio->line & 0xf);
  144. return 0;
  145. }
  146. return -ENOENT;
  147. }
  148. int
  149. nv50_pm_fanspeed_get(struct drm_device *dev)
  150. {
  151. struct drm_nouveau_private *dev_priv = dev->dev_private;
  152. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  153. struct pwm_info pwm;
  154. int ret;
  155. ret = nv50_pm_fanspeed_pwm(dev, &pwm);
  156. if (ret)
  157. return ret;
  158. if (nv_rd32(dev, pwm.ctrl) & (0x00000001 << pwm.line)) {
  159. u32 divs = nv_rd32(dev, 0x00e114 + (pwm.id * 8));
  160. u32 duty = nv_rd32(dev, 0x00e118 + (pwm.id * 8));
  161. if (divs) {
  162. divs = max(divs, duty);
  163. if (pwm.invert)
  164. duty = divs - duty;
  165. return (duty * 100) / divs;
  166. }
  167. return 0;
  168. }
  169. return pgpio->get(dev, pwm.tag) * 100;
  170. }
  171. int
  172. nv50_pm_fanspeed_set(struct drm_device *dev, int percent)
  173. {
  174. struct pwm_info pwm;
  175. u32 divs, duty;
  176. int ret;
  177. ret = nv50_pm_fanspeed_pwm(dev, &pwm);
  178. if (ret)
  179. return ret;
  180. divs = nv_rd32(dev, 0x00e114 + (pwm.id * 8));
  181. duty = ((divs * percent) + 99) / 100;
  182. if (pwm.invert)
  183. duty = divs - duty;
  184. nv_mask(dev, pwm.ctrl, 0x00010001 << pwm.line, 0x00000001 << pwm.line);
  185. nv_wr32(dev, 0x00e118 + (pwm.id * 8), 0x80000000 | duty);
  186. return 0;
  187. }