nouveau_mem.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
  3. * Copyright 2005 Stephane Marchesin
  4. *
  5. * The Weather Channel (TM) funded Tungsten Graphics to develop the
  6. * initial release of the Radeon 8500 driver under the XFree86 license.
  7. * This notice must be preserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS IN THE SOFTWARE.
  27. *
  28. * Authors:
  29. * Ben Skeggs <bskeggs@redhat.com>
  30. * Roy Spliet <r.spliet@student.tudelft.nl>
  31. */
  32. #include "nouveau_drm.h"
  33. #include "nouveau_pm.h"
  34. #include <subdev/fb.h>
  35. static int
  36. nv40_mem_timing_calc(struct drm_device *dev, u32 freq,
  37. struct nouveau_pm_tbl_entry *e, u8 len,
  38. struct nouveau_pm_memtiming *boot,
  39. struct nouveau_pm_memtiming *t)
  40. {
  41. struct nouveau_drm *drm = nouveau_drm(dev);
  42. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  43. /* XXX: I don't trust the -1's and +1's... they must come
  44. * from somewhere! */
  45. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  46. 1 << 16 |
  47. (e->tWTR + 2 + (t->tCWL - 1)) << 8 |
  48. (e->tCL + 2 - (t->tCWL - 1));
  49. t->reg[2] = 0x20200000 |
  50. ((t->tCWL - 1) << 24 |
  51. e->tRRD << 16 |
  52. e->tRCDWR << 8 |
  53. e->tRCDRD);
  54. NV_DEBUG(drm, "Entry %d: 220: %08x %08x %08x\n", t->id,
  55. t->reg[0], t->reg[1], t->reg[2]);
  56. return 0;
  57. }
  58. static int
  59. nv50_mem_timing_calc(struct drm_device *dev, u32 freq,
  60. struct nouveau_pm_tbl_entry *e, u8 len,
  61. struct nouveau_pm_memtiming *boot,
  62. struct nouveau_pm_memtiming *t)
  63. {
  64. struct nouveau_device *device = nouveau_dev(dev);
  65. struct nouveau_fb *pfb = nouveau_fb(device);
  66. struct nouveau_drm *drm = nouveau_drm(dev);
  67. struct bit_entry P;
  68. uint8_t unk18 = 1, unk20 = 0, unk21 = 0, tmp7_3;
  69. if (bit_table(dev, 'P', &P))
  70. return -EINVAL;
  71. switch (min(len, (u8) 22)) {
  72. case 22:
  73. unk21 = e->tUNK_21;
  74. case 21:
  75. unk20 = e->tUNK_20;
  76. case 20:
  77. if (e->tCWL > 0)
  78. t->tCWL = e->tCWL;
  79. case 19:
  80. unk18 = e->tUNK_18;
  81. break;
  82. }
  83. t->reg[0] = (e->tRP << 24 | e->tRAS << 16 | e->tRFC << 8 | e->tRC);
  84. t->reg[1] = (e->tWR + 2 + (t->tCWL - 1)) << 24 |
  85. max(unk18, (u8) 1) << 16 |
  86. (e->tWTR + 2 + (t->tCWL - 1)) << 8;
  87. t->reg[2] = ((t->tCWL - 1) << 24 |
  88. e->tRRD << 16 |
  89. e->tRCDWR << 8 |
  90. e->tRCDRD);
  91. t->reg[4] = e->tUNK_13 << 8 | e->tUNK_13;
  92. t->reg[5] = (e->tRFC << 24 | max(e->tRCDRD, e->tRCDWR) << 16 | e->tRP);
  93. t->reg[8] = boot->reg[8] & 0xffffff00;
  94. if (P.version == 1) {
  95. t->reg[1] |= (e->tCL + 2 - (t->tCWL - 1));
  96. t->reg[3] = (0x14 + e->tCL) << 24 |
  97. 0x16 << 16 |
  98. (e->tCL - 1) << 8 |
  99. (e->tCL - 1);
  100. t->reg[4] |= boot->reg[4] & 0xffff0000;
  101. t->reg[6] = (0x33 - t->tCWL) << 16 |
  102. t->tCWL << 8 |
  103. (0x2e + e->tCL - t->tCWL);
  104. t->reg[7] = 0x4000202 | (e->tCL - 1) << 16;
  105. /* XXX: P.version == 1 only has DDR2 and GDDR3? */
  106. if (pfb->ram.type == NV_MEM_TYPE_DDR2) {
  107. t->reg[5] |= (e->tCL + 3) << 8;
  108. t->reg[6] |= (t->tCWL - 2) << 8;
  109. t->reg[8] |= (e->tCL - 4);
  110. } else {
  111. t->reg[5] |= (e->tCL + 2) << 8;
  112. t->reg[6] |= t->tCWL << 8;
  113. t->reg[8] |= (e->tCL - 2);
  114. }
  115. } else {
  116. t->reg[1] |= (5 + e->tCL - (t->tCWL));
  117. /* XXX: 0xb? 0x30? */
  118. t->reg[3] = (0x30 + e->tCL) << 24 |
  119. (boot->reg[3] & 0x00ff0000)|
  120. (0xb + e->tCL) << 8 |
  121. (e->tCL - 1);
  122. t->reg[4] |= (unk20 << 24 | unk21 << 16);
  123. /* XXX: +6? */
  124. t->reg[5] |= (t->tCWL + 6) << 8;
  125. t->reg[6] = (0x5a + e->tCL) << 16 |
  126. (6 - e->tCL + t->tCWL) << 8 |
  127. (0x50 + e->tCL - t->tCWL);
  128. tmp7_3 = (boot->reg[7] & 0xff000000) >> 24;
  129. t->reg[7] = (tmp7_3 << 24) |
  130. ((tmp7_3 - 6 + e->tCL) << 16) |
  131. 0x202;
  132. }
  133. NV_DEBUG(drm, "Entry %d: 220: %08x %08x %08x %08x\n", t->id,
  134. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  135. NV_DEBUG(drm, " 230: %08x %08x %08x %08x\n",
  136. t->reg[4], t->reg[5], t->reg[6], t->reg[7]);
  137. NV_DEBUG(drm, " 240: %08x\n", t->reg[8]);
  138. return 0;
  139. }
  140. static int
  141. nvc0_mem_timing_calc(struct drm_device *dev, u32 freq,
  142. struct nouveau_pm_tbl_entry *e, u8 len,
  143. struct nouveau_pm_memtiming *boot,
  144. struct nouveau_pm_memtiming *t)
  145. {
  146. struct nouveau_drm *drm = nouveau_drm(dev);
  147. if (e->tCWL > 0)
  148. t->tCWL = e->tCWL;
  149. t->reg[0] = (e->tRP << 24 | (e->tRAS & 0x7f) << 17 |
  150. e->tRFC << 8 | e->tRC);
  151. t->reg[1] = (boot->reg[1] & 0xff000000) |
  152. (e->tRCDWR & 0x0f) << 20 |
  153. (e->tRCDRD & 0x0f) << 14 |
  154. (t->tCWL << 7) |
  155. (e->tCL & 0x0f);
  156. t->reg[2] = (boot->reg[2] & 0xff0000ff) |
  157. e->tWR << 16 | e->tWTR << 8;
  158. t->reg[3] = (e->tUNK_20 & 0x1f) << 9 |
  159. (e->tUNK_21 & 0xf) << 5 |
  160. (e->tUNK_13 & 0x1f);
  161. t->reg[4] = (boot->reg[4] & 0xfff00fff) |
  162. (e->tRRD&0x1f) << 15;
  163. NV_DEBUG(drm, "Entry %d: 290: %08x %08x %08x %08x\n", t->id,
  164. t->reg[0], t->reg[1], t->reg[2], t->reg[3]);
  165. NV_DEBUG(drm, " 2a0: %08x\n", t->reg[4]);
  166. return 0;
  167. }
  168. /**
  169. * MR generation methods
  170. */
  171. static int
  172. nouveau_mem_ddr2_mr(struct drm_device *dev, u32 freq,
  173. struct nouveau_pm_tbl_entry *e, u8 len,
  174. struct nouveau_pm_memtiming *boot,
  175. struct nouveau_pm_memtiming *t)
  176. {
  177. struct nouveau_drm *drm = nouveau_drm(dev);
  178. t->drive_strength = 0;
  179. if (len < 15) {
  180. t->odt = boot->odt;
  181. } else {
  182. t->odt = e->RAM_FT1 & 0x07;
  183. }
  184. if (e->tCL >= NV_MEM_CL_DDR2_MAX) {
  185. NV_WARN(drm, "(%u) Invalid tCL: %u", t->id, e->tCL);
  186. return -ERANGE;
  187. }
  188. if (e->tWR >= NV_MEM_WR_DDR2_MAX) {
  189. NV_WARN(drm, "(%u) Invalid tWR: %u", t->id, e->tWR);
  190. return -ERANGE;
  191. }
  192. if (t->odt > 3) {
  193. NV_WARN(drm, "(%u) Invalid odt value, assuming disabled: %x",
  194. t->id, t->odt);
  195. t->odt = 0;
  196. }
  197. t->mr[0] = (boot->mr[0] & 0x100f) |
  198. (e->tCL) << 4 |
  199. (e->tWR - 1) << 9;
  200. t->mr[1] = (boot->mr[1] & 0x101fbb) |
  201. (t->odt & 0x1) << 2 |
  202. (t->odt & 0x2) << 5;
  203. NV_DEBUG(drm, "(%u) MR: %08x", t->id, t->mr[0]);
  204. return 0;
  205. }
  206. static const uint8_t nv_mem_wr_lut_ddr3[NV_MEM_WR_DDR3_MAX] = {
  207. 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
  208. static int
  209. nouveau_mem_ddr3_mr(struct drm_device *dev, u32 freq,
  210. struct nouveau_pm_tbl_entry *e, u8 len,
  211. struct nouveau_pm_memtiming *boot,
  212. struct nouveau_pm_memtiming *t)
  213. {
  214. struct nouveau_drm *drm = nouveau_drm(dev);
  215. u8 cl = e->tCL - 4;
  216. t->drive_strength = 0;
  217. if (len < 15) {
  218. t->odt = boot->odt;
  219. } else {
  220. t->odt = e->RAM_FT1 & 0x07;
  221. }
  222. if (e->tCL >= NV_MEM_CL_DDR3_MAX || e->tCL < 4) {
  223. NV_WARN(drm, "(%u) Invalid tCL: %u", t->id, e->tCL);
  224. return -ERANGE;
  225. }
  226. if (e->tWR >= NV_MEM_WR_DDR3_MAX || e->tWR < 4) {
  227. NV_WARN(drm, "(%u) Invalid tWR: %u", t->id, e->tWR);
  228. return -ERANGE;
  229. }
  230. if (e->tCWL < 5) {
  231. NV_WARN(drm, "(%u) Invalid tCWL: %u", t->id, e->tCWL);
  232. return -ERANGE;
  233. }
  234. t->mr[0] = (boot->mr[0] & 0x180b) |
  235. /* CAS */
  236. (cl & 0x7) << 4 |
  237. (cl & 0x8) >> 1 |
  238. (nv_mem_wr_lut_ddr3[e->tWR]) << 9;
  239. t->mr[1] = (boot->mr[1] & 0x101dbb) |
  240. (t->odt & 0x1) << 2 |
  241. (t->odt & 0x2) << 5 |
  242. (t->odt & 0x4) << 7;
  243. t->mr[2] = (boot->mr[2] & 0x20ffb7) | (e->tCWL - 5) << 3;
  244. NV_DEBUG(drm, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[2]);
  245. return 0;
  246. }
  247. static const uint8_t nv_mem_cl_lut_gddr3[NV_MEM_CL_GDDR3_MAX] = {
  248. 0, 0, 0, 0, 4, 5, 6, 7, 0, 1, 2, 3, 8, 9, 10, 11};
  249. static const uint8_t nv_mem_wr_lut_gddr3[NV_MEM_WR_GDDR3_MAX] = {
  250. 0, 0, 0, 0, 0, 2, 3, 8, 9, 10, 11, 0, 0, 1, 1, 0, 3};
  251. static int
  252. nouveau_mem_gddr3_mr(struct drm_device *dev, u32 freq,
  253. struct nouveau_pm_tbl_entry *e, u8 len,
  254. struct nouveau_pm_memtiming *boot,
  255. struct nouveau_pm_memtiming *t)
  256. {
  257. struct nouveau_drm *drm = nouveau_drm(dev);
  258. if (len < 15) {
  259. t->drive_strength = boot->drive_strength;
  260. t->odt = boot->odt;
  261. } else {
  262. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  263. t->odt = e->RAM_FT1 & 0x07;
  264. }
  265. if (e->tCL >= NV_MEM_CL_GDDR3_MAX) {
  266. NV_WARN(drm, "(%u) Invalid tCL: %u", t->id, e->tCL);
  267. return -ERANGE;
  268. }
  269. if (e->tWR >= NV_MEM_WR_GDDR3_MAX) {
  270. NV_WARN(drm, "(%u) Invalid tWR: %u", t->id, e->tWR);
  271. return -ERANGE;
  272. }
  273. if (t->odt > 3) {
  274. NV_WARN(drm, "(%u) Invalid odt value, assuming autocal: %x",
  275. t->id, t->odt);
  276. t->odt = 0;
  277. }
  278. t->mr[0] = (boot->mr[0] & 0xe0b) |
  279. /* CAS */
  280. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x7) << 4) |
  281. ((nv_mem_cl_lut_gddr3[e->tCL] & 0x8) >> 2);
  282. t->mr[1] = (boot->mr[1] & 0x100f40) | t->drive_strength |
  283. (t->odt << 2) |
  284. (nv_mem_wr_lut_gddr3[e->tWR] & 0xf) << 4;
  285. t->mr[2] = boot->mr[2];
  286. NV_DEBUG(drm, "(%u) MR: %08x %08x %08x", t->id,
  287. t->mr[0], t->mr[1], t->mr[2]);
  288. return 0;
  289. }
  290. static int
  291. nouveau_mem_gddr5_mr(struct drm_device *dev, u32 freq,
  292. struct nouveau_pm_tbl_entry *e, u8 len,
  293. struct nouveau_pm_memtiming *boot,
  294. struct nouveau_pm_memtiming *t)
  295. {
  296. struct nouveau_drm *drm = nouveau_drm(dev);
  297. if (len < 15) {
  298. t->drive_strength = boot->drive_strength;
  299. t->odt = boot->odt;
  300. } else {
  301. t->drive_strength = (e->RAM_FT1 & 0x30) >> 4;
  302. t->odt = e->RAM_FT1 & 0x03;
  303. }
  304. if (e->tCL >= NV_MEM_CL_GDDR5_MAX) {
  305. NV_WARN(drm, "(%u) Invalid tCL: %u", t->id, e->tCL);
  306. return -ERANGE;
  307. }
  308. if (e->tWR >= NV_MEM_WR_GDDR5_MAX) {
  309. NV_WARN(drm, "(%u) Invalid tWR: %u", t->id, e->tWR);
  310. return -ERANGE;
  311. }
  312. if (t->odt > 3) {
  313. NV_WARN(drm, "(%u) Invalid odt value, assuming autocal: %x",
  314. t->id, t->odt);
  315. t->odt = 0;
  316. }
  317. t->mr[0] = (boot->mr[0] & 0x007) |
  318. ((e->tCL - 5) << 3) |
  319. ((e->tWR - 4) << 8);
  320. t->mr[1] = (boot->mr[1] & 0x1007f0) |
  321. t->drive_strength |
  322. (t->odt << 2);
  323. NV_DEBUG(drm, "(%u) MR: %08x %08x", t->id, t->mr[0], t->mr[1]);
  324. return 0;
  325. }
  326. int
  327. nouveau_mem_timing_calc(struct drm_device *dev, u32 freq,
  328. struct nouveau_pm_memtiming *t)
  329. {
  330. struct nouveau_device *device = nouveau_dev(dev);
  331. struct nouveau_fb *pfb = nouveau_fb(device);
  332. struct nouveau_pm *pm = nouveau_pm(dev);
  333. struct nouveau_pm_memtiming *boot = &pm->boot.timing;
  334. struct nouveau_pm_tbl_entry *e;
  335. u8 ver, len, *ptr, *ramcfg;
  336. int ret;
  337. ptr = nouveau_perf_timing(dev, freq, &ver, &len);
  338. if (!ptr || ptr[0] == 0x00) {
  339. *t = *boot;
  340. return 0;
  341. }
  342. e = (struct nouveau_pm_tbl_entry *)ptr;
  343. t->tCWL = boot->tCWL;
  344. switch (device->card_type) {
  345. case NV_40:
  346. ret = nv40_mem_timing_calc(dev, freq, e, len, boot, t);
  347. break;
  348. case NV_50:
  349. ret = nv50_mem_timing_calc(dev, freq, e, len, boot, t);
  350. break;
  351. case NV_C0:
  352. case NV_D0:
  353. ret = nvc0_mem_timing_calc(dev, freq, e, len, boot, t);
  354. break;
  355. default:
  356. ret = -ENODEV;
  357. break;
  358. }
  359. switch (pfb->ram.type * !ret) {
  360. case NV_MEM_TYPE_GDDR3:
  361. ret = nouveau_mem_gddr3_mr(dev, freq, e, len, boot, t);
  362. break;
  363. case NV_MEM_TYPE_GDDR5:
  364. ret = nouveau_mem_gddr5_mr(dev, freq, e, len, boot, t);
  365. break;
  366. case NV_MEM_TYPE_DDR2:
  367. ret = nouveau_mem_ddr2_mr(dev, freq, e, len, boot, t);
  368. break;
  369. case NV_MEM_TYPE_DDR3:
  370. ret = nouveau_mem_ddr3_mr(dev, freq, e, len, boot, t);
  371. break;
  372. default:
  373. ret = -EINVAL;
  374. break;
  375. }
  376. ramcfg = nouveau_perf_ramcfg(dev, freq, &ver, &len);
  377. if (ramcfg) {
  378. int dll_off;
  379. if (ver == 0x00)
  380. dll_off = !!(ramcfg[3] & 0x04);
  381. else
  382. dll_off = !!(ramcfg[2] & 0x40);
  383. switch (pfb->ram.type) {
  384. case NV_MEM_TYPE_GDDR3:
  385. t->mr[1] &= ~0x00000040;
  386. t->mr[1] |= 0x00000040 * dll_off;
  387. break;
  388. default:
  389. t->mr[1] &= ~0x00000001;
  390. t->mr[1] |= 0x00000001 * dll_off;
  391. break;
  392. }
  393. }
  394. return ret;
  395. }
  396. void
  397. nouveau_mem_timing_read(struct drm_device *dev, struct nouveau_pm_memtiming *t)
  398. {
  399. struct nouveau_device *device = nouveau_dev(dev);
  400. struct nouveau_fb *pfb = nouveau_fb(device);
  401. u32 timing_base, timing_regs, mr_base;
  402. int i;
  403. if (device->card_type >= 0xC0) {
  404. timing_base = 0x10f290;
  405. mr_base = 0x10f300;
  406. } else {
  407. timing_base = 0x100220;
  408. mr_base = 0x1002c0;
  409. }
  410. t->id = -1;
  411. switch (device->card_type) {
  412. case NV_50:
  413. timing_regs = 9;
  414. break;
  415. case NV_C0:
  416. case NV_D0:
  417. timing_regs = 5;
  418. break;
  419. case NV_30:
  420. case NV_40:
  421. timing_regs = 3;
  422. break;
  423. default:
  424. timing_regs = 0;
  425. return;
  426. }
  427. for(i = 0; i < timing_regs; i++)
  428. t->reg[i] = nv_rd32(device, timing_base + (0x04 * i));
  429. t->tCWL = 0;
  430. if (device->card_type < NV_C0) {
  431. t->tCWL = ((nv_rd32(device, 0x100228) & 0x0f000000) >> 24) + 1;
  432. } else if (device->card_type <= NV_D0) {
  433. t->tCWL = ((nv_rd32(device, 0x10f294) & 0x00000f80) >> 7);
  434. }
  435. t->mr[0] = nv_rd32(device, mr_base);
  436. t->mr[1] = nv_rd32(device, mr_base + 0x04);
  437. t->mr[2] = nv_rd32(device, mr_base + 0x20);
  438. t->mr[3] = nv_rd32(device, mr_base + 0x24);
  439. t->odt = 0;
  440. t->drive_strength = 0;
  441. switch (pfb->ram.type) {
  442. case NV_MEM_TYPE_DDR3:
  443. t->odt |= (t->mr[1] & 0x200) >> 7;
  444. case NV_MEM_TYPE_DDR2:
  445. t->odt |= (t->mr[1] & 0x04) >> 2 |
  446. (t->mr[1] & 0x40) >> 5;
  447. break;
  448. case NV_MEM_TYPE_GDDR3:
  449. case NV_MEM_TYPE_GDDR5:
  450. t->drive_strength = t->mr[1] & 0x03;
  451. t->odt = (t->mr[1] & 0x0c) >> 2;
  452. break;
  453. default:
  454. break;
  455. }
  456. }
  457. int
  458. nouveau_mem_exec(struct nouveau_mem_exec_func *exec,
  459. struct nouveau_pm_level *perflvl)
  460. {
  461. struct nouveau_drm *drm = nouveau_drm(exec->dev);
  462. struct nouveau_device *device = nouveau_dev(exec->dev);
  463. struct nouveau_fb *pfb = nouveau_fb(device);
  464. struct nouveau_pm_memtiming *info = &perflvl->timing;
  465. u32 tMRD = 1000, tCKSRE = 0, tCKSRX = 0, tXS = 0, tDLLK = 0;
  466. u32 mr[3] = { info->mr[0], info->mr[1], info->mr[2] };
  467. u32 mr1_dlloff;
  468. switch (pfb->ram.type) {
  469. case NV_MEM_TYPE_DDR2:
  470. tDLLK = 2000;
  471. mr1_dlloff = 0x00000001;
  472. break;
  473. case NV_MEM_TYPE_DDR3:
  474. tDLLK = 12000;
  475. tCKSRE = 2000;
  476. tXS = 1000;
  477. mr1_dlloff = 0x00000001;
  478. break;
  479. case NV_MEM_TYPE_GDDR3:
  480. tDLLK = 40000;
  481. mr1_dlloff = 0x00000040;
  482. break;
  483. default:
  484. NV_ERROR(drm, "cannot reclock unsupported memtype\n");
  485. return -ENODEV;
  486. }
  487. /* fetch current MRs */
  488. switch (pfb->ram.type) {
  489. case NV_MEM_TYPE_GDDR3:
  490. case NV_MEM_TYPE_DDR3:
  491. mr[2] = exec->mrg(exec, 2);
  492. default:
  493. mr[1] = exec->mrg(exec, 1);
  494. mr[0] = exec->mrg(exec, 0);
  495. break;
  496. }
  497. /* DLL 'on' -> DLL 'off' mode, disable before entering self-refresh */
  498. if (!(mr[1] & mr1_dlloff) && (info->mr[1] & mr1_dlloff)) {
  499. exec->precharge(exec);
  500. exec->mrs (exec, 1, mr[1] | mr1_dlloff);
  501. exec->wait(exec, tMRD);
  502. }
  503. /* enter self-refresh mode */
  504. exec->precharge(exec);
  505. exec->refresh(exec);
  506. exec->refresh(exec);
  507. exec->refresh_auto(exec, false);
  508. exec->refresh_self(exec, true);
  509. exec->wait(exec, tCKSRE);
  510. /* modify input clock frequency */
  511. exec->clock_set(exec);
  512. /* exit self-refresh mode */
  513. exec->wait(exec, tCKSRX);
  514. exec->precharge(exec);
  515. exec->refresh_self(exec, false);
  516. exec->refresh_auto(exec, true);
  517. exec->wait(exec, tXS);
  518. exec->wait(exec, tXS);
  519. /* update MRs */
  520. if (mr[2] != info->mr[2]) {
  521. exec->mrs (exec, 2, info->mr[2]);
  522. exec->wait(exec, tMRD);
  523. }
  524. if (mr[1] != info->mr[1]) {
  525. /* need to keep DLL off until later, at least on GDDR3 */
  526. exec->mrs (exec, 1, info->mr[1] | (mr[1] & mr1_dlloff));
  527. exec->wait(exec, tMRD);
  528. }
  529. if (mr[0] != info->mr[0]) {
  530. exec->mrs (exec, 0, info->mr[0]);
  531. exec->wait(exec, tMRD);
  532. }
  533. /* update PFB timing registers */
  534. exec->timing_set(exec);
  535. /* DLL (enable + ) reset */
  536. if (!(info->mr[1] & mr1_dlloff)) {
  537. if (mr[1] & mr1_dlloff) {
  538. exec->mrs (exec, 1, info->mr[1]);
  539. exec->wait(exec, tMRD);
  540. }
  541. exec->mrs (exec, 0, info->mr[0] | 0x00000100);
  542. exec->wait(exec, tMRD);
  543. exec->mrs (exec, 0, info->mr[0] | 0x00000000);
  544. exec->wait(exec, tMRD);
  545. exec->wait(exec, tDLLK);
  546. if (pfb->ram.type == NV_MEM_TYPE_GDDR3)
  547. exec->precharge(exec);
  548. }
  549. return 0;
  550. }