nv40.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2012 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. #define nv40_clk(p) container_of((p), struct nv40_clk, base)
  25. #include "priv.h"
  26. #include "pll.h"
  27. #include <subdev/bios.h>
  28. #include <subdev/bios/pll.h>
  29. struct nv40_clk {
  30. struct nvkm_clk base;
  31. u32 ctrl;
  32. u32 npll_ctrl;
  33. u32 npll_coef;
  34. u32 spll;
  35. };
  36. static u32
  37. read_pll_1(struct nv40_clk *clk, u32 reg)
  38. {
  39. struct nvkm_device *device = clk->base.subdev.device;
  40. u32 ctrl = nvkm_rd32(device, reg + 0x00);
  41. int P = (ctrl & 0x00070000) >> 16;
  42. int N = (ctrl & 0x0000ff00) >> 8;
  43. int M = (ctrl & 0x000000ff) >> 0;
  44. u32 ref = 27000, khz = 0;
  45. if (ctrl & 0x80000000)
  46. khz = ref * N / M;
  47. return khz >> P;
  48. }
  49. static u32
  50. read_pll_2(struct nv40_clk *clk, u32 reg)
  51. {
  52. struct nvkm_device *device = clk->base.subdev.device;
  53. u32 ctrl = nvkm_rd32(device, reg + 0x00);
  54. u32 coef = nvkm_rd32(device, reg + 0x04);
  55. int N2 = (coef & 0xff000000) >> 24;
  56. int M2 = (coef & 0x00ff0000) >> 16;
  57. int N1 = (coef & 0x0000ff00) >> 8;
  58. int M1 = (coef & 0x000000ff) >> 0;
  59. int P = (ctrl & 0x00070000) >> 16;
  60. u32 ref = 27000, khz = 0;
  61. if ((ctrl & 0x80000000) && M1) {
  62. khz = ref * N1 / M1;
  63. if ((ctrl & 0x40000100) == 0x40000000) {
  64. if (M2)
  65. khz = khz * N2 / M2;
  66. else
  67. khz = 0;
  68. }
  69. }
  70. return khz >> P;
  71. }
  72. static u32
  73. read_clk(struct nv40_clk *clk, u32 src)
  74. {
  75. switch (src) {
  76. case 3:
  77. return read_pll_2(clk, 0x004000);
  78. case 2:
  79. return read_pll_1(clk, 0x004008);
  80. default:
  81. break;
  82. }
  83. return 0;
  84. }
  85. static int
  86. nv40_clk_read(struct nvkm_clk *base, enum nv_clk_src src)
  87. {
  88. struct nv40_clk *clk = nv40_clk(base);
  89. struct nvkm_subdev *subdev = &clk->base.subdev;
  90. struct nvkm_device *device = subdev->device;
  91. u32 mast = nvkm_rd32(device, 0x00c040);
  92. switch (src) {
  93. case nv_clk_src_crystal:
  94. return device->crystal;
  95. case nv_clk_src_href:
  96. return 100000; /*XXX: PCIE/AGP differ*/
  97. case nv_clk_src_core:
  98. return read_clk(clk, (mast & 0x00000003) >> 0);
  99. case nv_clk_src_shader:
  100. return read_clk(clk, (mast & 0x00000030) >> 4);
  101. case nv_clk_src_mem:
  102. return read_pll_2(clk, 0x4020);
  103. default:
  104. break;
  105. }
  106. nvkm_debug(subdev, "unknown clock source %d %08x\n", src, mast);
  107. return -EINVAL;
  108. }
  109. static int
  110. nv40_clk_calc_pll(struct nv40_clk *clk, u32 reg, u32 khz,
  111. int *N1, int *M1, int *N2, int *M2, int *log2P)
  112. {
  113. struct nvkm_subdev *subdev = &clk->base.subdev;
  114. struct nvbios_pll pll;
  115. int ret;
  116. ret = nvbios_pll_parse(subdev->device->bios, reg, &pll);
  117. if (ret)
  118. return ret;
  119. if (khz < pll.vco1.max_freq)
  120. pll.vco2.max_freq = 0;
  121. ret = nv04_pll_calc(subdev, &pll, khz, N1, M1, N2, M2, log2P);
  122. if (ret == 0)
  123. return -ERANGE;
  124. return ret;
  125. }
  126. static int
  127. nv40_clk_calc(struct nvkm_clk *base, struct nvkm_cstate *cstate)
  128. {
  129. struct nv40_clk *clk = nv40_clk(base);
  130. int gclk = cstate->domain[nv_clk_src_core];
  131. int sclk = cstate->domain[nv_clk_src_shader];
  132. int N1, M1, N2, M2, log2P;
  133. int ret;
  134. /* core/geometric clock */
  135. ret = nv40_clk_calc_pll(clk, 0x004000, gclk,
  136. &N1, &M1, &N2, &M2, &log2P);
  137. if (ret < 0)
  138. return ret;
  139. if (N2 == M2) {
  140. clk->npll_ctrl = 0x80000100 | (log2P << 16);
  141. clk->npll_coef = (N1 << 8) | M1;
  142. } else {
  143. clk->npll_ctrl = 0xc0000000 | (log2P << 16);
  144. clk->npll_coef = (N2 << 24) | (M2 << 16) | (N1 << 8) | M1;
  145. }
  146. /* use the second pll for shader/rop clock, if it differs from core */
  147. if (sclk && sclk != gclk) {
  148. ret = nv40_clk_calc_pll(clk, 0x004008, sclk,
  149. &N1, &M1, NULL, NULL, &log2P);
  150. if (ret < 0)
  151. return ret;
  152. clk->spll = 0xc0000000 | (log2P << 16) | (N1 << 8) | M1;
  153. clk->ctrl = 0x00000223;
  154. } else {
  155. clk->spll = 0x00000000;
  156. clk->ctrl = 0x00000333;
  157. }
  158. return 0;
  159. }
  160. static int
  161. nv40_clk_prog(struct nvkm_clk *base)
  162. {
  163. struct nv40_clk *clk = nv40_clk(base);
  164. struct nvkm_device *device = clk->base.subdev.device;
  165. nvkm_mask(device, 0x00c040, 0x00000333, 0x00000000);
  166. nvkm_wr32(device, 0x004004, clk->npll_coef);
  167. nvkm_mask(device, 0x004000, 0xc0070100, clk->npll_ctrl);
  168. nvkm_mask(device, 0x004008, 0xc007ffff, clk->spll);
  169. mdelay(5);
  170. nvkm_mask(device, 0x00c040, 0x00000333, clk->ctrl);
  171. return 0;
  172. }
  173. static void
  174. nv40_clk_tidy(struct nvkm_clk *obj)
  175. {
  176. }
  177. static const struct nvkm_clk_func
  178. nv40_clk = {
  179. .read = nv40_clk_read,
  180. .calc = nv40_clk_calc,
  181. .prog = nv40_clk_prog,
  182. .tidy = nv40_clk_tidy,
  183. .domains = {
  184. { nv_clk_src_crystal, 0xff },
  185. { nv_clk_src_href , 0xff },
  186. { nv_clk_src_core , 0xff, 0, "core", 1000 },
  187. { nv_clk_src_shader , 0xff, 0, "shader", 1000 },
  188. { nv_clk_src_mem , 0xff, 0, "memory", 1000 },
  189. { nv_clk_src_max }
  190. }
  191. };
  192. int
  193. nv40_clk_new(struct nvkm_device *device, int index, struct nvkm_clk **pclk)
  194. {
  195. struct nv40_clk *clk;
  196. if (!(clk = kzalloc(sizeof(*clk), GFP_KERNEL)))
  197. return -ENOMEM;
  198. clk->base.pll_calc = nv04_clk_pll_calc;
  199. clk->base.pll_prog = nv04_clk_pll_prog;
  200. *pclk = &clk->base;
  201. return nvkm_clk_ctor(&nv40_clk, device, index, true, &clk->base);
  202. }