ramnv40.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright 2013 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 "ramnv40.h"
  25. #include <subdev/bios.h>
  26. #include <subdev/bios/bit.h>
  27. #include <subdev/bios/init.h>
  28. #include <subdev/bios/pll.h>
  29. #include <subdev/clk/pll.h>
  30. #include <subdev/timer.h>
  31. static int
  32. nv40_ram_calc(struct nvkm_ram *base, u32 freq)
  33. {
  34. struct nv40_ram *ram = nv40_ram(base);
  35. struct nvkm_subdev *subdev = &ram->base.fb->subdev;
  36. struct nvkm_bios *bios = subdev->device->bios;
  37. struct nvbios_pll pll;
  38. int N1, M1, N2, M2;
  39. int log2P, ret;
  40. ret = nvbios_pll_parse(bios, 0x04, &pll);
  41. if (ret) {
  42. nvkm_error(subdev, "mclk pll data not found\n");
  43. return ret;
  44. }
  45. ret = nv04_pll_calc(subdev, &pll, freq, &N1, &M1, &N2, &M2, &log2P);
  46. if (ret < 0)
  47. return ret;
  48. ram->ctrl = 0x80000000 | (log2P << 16);
  49. ram->ctrl |= min(pll.bias_p + log2P, (int)pll.max_p) << 20;
  50. if (N2 == M2) {
  51. ram->ctrl |= 0x00000100;
  52. ram->coef = (N1 << 8) | M1;
  53. } else {
  54. ram->ctrl |= 0x40000000;
  55. ram->coef = (N2 << 24) | (M2 << 16) | (N1 << 8) | M1;
  56. }
  57. return 0;
  58. }
  59. static int
  60. nv40_ram_prog(struct nvkm_ram *base)
  61. {
  62. struct nv40_ram *ram = nv40_ram(base);
  63. struct nvkm_subdev *subdev = &ram->base.fb->subdev;
  64. struct nvkm_device *device = subdev->device;
  65. struct nvkm_bios *bios = device->bios;
  66. struct bit_entry M;
  67. u32 crtc_mask = 0;
  68. u8 sr1[2];
  69. int i;
  70. /* determine which CRTCs are active, fetch VGA_SR1 for each */
  71. for (i = 0; i < 2; i++) {
  72. u32 vbl = nvkm_rd32(device, 0x600808 + (i * 0x2000));
  73. u32 cnt = 0;
  74. do {
  75. if (vbl != nvkm_rd32(device, 0x600808 + (i * 0x2000))) {
  76. nvkm_wr08(device, 0x0c03c4 + (i * 0x2000), 0x01);
  77. sr1[i] = nvkm_rd08(device, 0x0c03c5 + (i * 0x2000));
  78. if (!(sr1[i] & 0x20))
  79. crtc_mask |= (1 << i);
  80. break;
  81. }
  82. udelay(1);
  83. } while (cnt++ < 32);
  84. }
  85. /* wait for vblank start on active crtcs, disable memory access */
  86. for (i = 0; i < 2; i++) {
  87. if (!(crtc_mask & (1 << i)))
  88. continue;
  89. nvkm_msec(device, 2000,
  90. u32 tmp = nvkm_rd32(device, 0x600808 + (i * 0x2000));
  91. if (!(tmp & 0x00010000))
  92. break;
  93. );
  94. nvkm_msec(device, 2000,
  95. u32 tmp = nvkm_rd32(device, 0x600808 + (i * 0x2000));
  96. if ( (tmp & 0x00010000))
  97. break;
  98. );
  99. nvkm_wr08(device, 0x0c03c4 + (i * 0x2000), 0x01);
  100. nvkm_wr08(device, 0x0c03c5 + (i * 0x2000), sr1[i] | 0x20);
  101. }
  102. /* prepare ram for reclocking */
  103. nvkm_wr32(device, 0x1002d4, 0x00000001); /* precharge */
  104. nvkm_wr32(device, 0x1002d0, 0x00000001); /* refresh */
  105. nvkm_wr32(device, 0x1002d0, 0x00000001); /* refresh */
  106. nvkm_mask(device, 0x100210, 0x80000000, 0x00000000); /* no auto refresh */
  107. nvkm_wr32(device, 0x1002dc, 0x00000001); /* enable self-refresh */
  108. /* change the PLL of each memory partition */
  109. nvkm_mask(device, 0x00c040, 0x0000c000, 0x00000000);
  110. switch (device->chipset) {
  111. case 0x40:
  112. case 0x45:
  113. case 0x41:
  114. case 0x42:
  115. case 0x47:
  116. nvkm_mask(device, 0x004044, 0xc0771100, ram->ctrl);
  117. nvkm_mask(device, 0x00402c, 0xc0771100, ram->ctrl);
  118. nvkm_wr32(device, 0x004048, ram->coef);
  119. nvkm_wr32(device, 0x004030, ram->coef);
  120. case 0x43:
  121. case 0x49:
  122. case 0x4b:
  123. nvkm_mask(device, 0x004038, 0xc0771100, ram->ctrl);
  124. nvkm_wr32(device, 0x00403c, ram->coef);
  125. default:
  126. nvkm_mask(device, 0x004020, 0xc0771100, ram->ctrl);
  127. nvkm_wr32(device, 0x004024, ram->coef);
  128. break;
  129. }
  130. udelay(100);
  131. nvkm_mask(device, 0x00c040, 0x0000c000, 0x0000c000);
  132. /* re-enable normal operation of memory controller */
  133. nvkm_wr32(device, 0x1002dc, 0x00000000);
  134. nvkm_mask(device, 0x100210, 0x80000000, 0x80000000);
  135. udelay(100);
  136. /* execute memory reset script from vbios */
  137. if (!bit_entry(bios, 'M', &M)) {
  138. struct nvbios_init init = {
  139. .subdev = subdev,
  140. .bios = bios,
  141. .offset = nvbios_rd16(bios, M.offset + 0x00),
  142. .execute = 1,
  143. };
  144. nvbios_exec(&init);
  145. }
  146. /* make sure we're in vblank (hopefully the same one as before), and
  147. * then re-enable crtc memory access
  148. */
  149. for (i = 0; i < 2; i++) {
  150. if (!(crtc_mask & (1 << i)))
  151. continue;
  152. nvkm_msec(device, 2000,
  153. u32 tmp = nvkm_rd32(device, 0x600808 + (i * 0x2000));
  154. if ( (tmp & 0x00010000))
  155. break;
  156. );
  157. nvkm_wr08(device, 0x0c03c4 + (i * 0x2000), 0x01);
  158. nvkm_wr08(device, 0x0c03c5 + (i * 0x2000), sr1[i]);
  159. }
  160. return 0;
  161. }
  162. static void
  163. nv40_ram_tidy(struct nvkm_ram *base)
  164. {
  165. }
  166. static const struct nvkm_ram_func
  167. nv40_ram_func = {
  168. .calc = nv40_ram_calc,
  169. .prog = nv40_ram_prog,
  170. .tidy = nv40_ram_tidy,
  171. };
  172. int
  173. nv40_ram_new_(struct nvkm_fb *fb, enum nvkm_ram_type type, u64 size,
  174. u32 tags, struct nvkm_ram **pram)
  175. {
  176. struct nv40_ram *ram;
  177. if (!(ram = kzalloc(sizeof(*ram), GFP_KERNEL)))
  178. return -ENOMEM;
  179. *pram = &ram->base;
  180. return nvkm_ram_ctor(&nv40_ram_func, fb, type, size, tags, &ram->base);
  181. }
  182. int
  183. nv40_ram_new(struct nvkm_fb *fb, struct nvkm_ram **pram)
  184. {
  185. struct nvkm_device *device = fb->subdev.device;
  186. u32 pbus1218 = nvkm_rd32(device, 0x001218);
  187. u32 size = nvkm_rd32(device, 0x10020c) & 0xff000000;
  188. u32 tags = nvkm_rd32(device, 0x100320);
  189. enum nvkm_ram_type type = NVKM_RAM_TYPE_UNKNOWN;
  190. int ret;
  191. switch (pbus1218 & 0x00000300) {
  192. case 0x00000000: type = NVKM_RAM_TYPE_SDRAM; break;
  193. case 0x00000100: type = NVKM_RAM_TYPE_DDR1 ; break;
  194. case 0x00000200: type = NVKM_RAM_TYPE_GDDR3; break;
  195. case 0x00000300: type = NVKM_RAM_TYPE_DDR2 ; break;
  196. }
  197. ret = nv40_ram_new_(fb, type, size, tags, pram);
  198. if (ret)
  199. return ret;
  200. (*pram)->parts = (nvkm_rd32(device, 0x100200) & 0x00000003) + 1;
  201. return 0;
  202. }