nv04.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Copyright (C) 2010 Francisco Jerez.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include "nv04.h"
  27. #include "fbmem.h"
  28. #include <subdev/bios.h>
  29. #include <subdev/bios/init.h>
  30. #include <subdev/bios/pll.h>
  31. #include <subdev/clk/pll.h>
  32. #include <subdev/vga.h>
  33. static void
  34. nv04_devinit_meminit(struct nvkm_devinit *init)
  35. {
  36. struct nvkm_subdev *subdev = &init->subdev;
  37. struct nvkm_device *device = subdev->device;
  38. u32 patt = 0xdeadbeef;
  39. struct io_mapping *fb;
  40. int i;
  41. /* Map the framebuffer aperture */
  42. fb = fbmem_init(device);
  43. if (!fb) {
  44. nvkm_error(subdev, "failed to map fb\n");
  45. return;
  46. }
  47. /* Sequencer and refresh off */
  48. nvkm_wrvgas(device, 0, 1, nvkm_rdvgas(device, 0, 1) | 0x20);
  49. nvkm_mask(device, NV04_PFB_DEBUG_0, 0, NV04_PFB_DEBUG_0_REFRESH_OFF);
  50. nvkm_mask(device, NV04_PFB_BOOT_0, ~0,
  51. NV04_PFB_BOOT_0_RAM_AMOUNT_16MB |
  52. NV04_PFB_BOOT_0_RAM_WIDTH_128 |
  53. NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_16MBIT);
  54. for (i = 0; i < 4; i++)
  55. fbmem_poke(fb, 4 * i, patt);
  56. fbmem_poke(fb, 0x400000, patt + 1);
  57. if (fbmem_peek(fb, 0) == patt + 1) {
  58. nvkm_mask(device, NV04_PFB_BOOT_0,
  59. NV04_PFB_BOOT_0_RAM_TYPE,
  60. NV04_PFB_BOOT_0_RAM_TYPE_SDRAM_16MBIT);
  61. nvkm_mask(device, NV04_PFB_DEBUG_0,
  62. NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
  63. for (i = 0; i < 4; i++)
  64. fbmem_poke(fb, 4 * i, patt);
  65. if ((fbmem_peek(fb, 0xc) & 0xffff) != (patt & 0xffff))
  66. nvkm_mask(device, NV04_PFB_BOOT_0,
  67. NV04_PFB_BOOT_0_RAM_WIDTH_128 |
  68. NV04_PFB_BOOT_0_RAM_AMOUNT,
  69. NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
  70. } else
  71. if ((fbmem_peek(fb, 0xc) & 0xffff0000) != (patt & 0xffff0000)) {
  72. nvkm_mask(device, NV04_PFB_BOOT_0,
  73. NV04_PFB_BOOT_0_RAM_WIDTH_128 |
  74. NV04_PFB_BOOT_0_RAM_AMOUNT,
  75. NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
  76. } else
  77. if (fbmem_peek(fb, 0) != patt) {
  78. if (fbmem_readback(fb, 0x800000, patt))
  79. nvkm_mask(device, NV04_PFB_BOOT_0,
  80. NV04_PFB_BOOT_0_RAM_AMOUNT,
  81. NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
  82. else
  83. nvkm_mask(device, NV04_PFB_BOOT_0,
  84. NV04_PFB_BOOT_0_RAM_AMOUNT,
  85. NV04_PFB_BOOT_0_RAM_AMOUNT_4MB);
  86. nvkm_mask(device, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_TYPE,
  87. NV04_PFB_BOOT_0_RAM_TYPE_SGRAM_8MBIT);
  88. } else
  89. if (!fbmem_readback(fb, 0x800000, patt)) {
  90. nvkm_mask(device, NV04_PFB_BOOT_0, NV04_PFB_BOOT_0_RAM_AMOUNT,
  91. NV04_PFB_BOOT_0_RAM_AMOUNT_8MB);
  92. }
  93. /* Refresh on, sequencer on */
  94. nvkm_mask(device, NV04_PFB_DEBUG_0, NV04_PFB_DEBUG_0_REFRESH_OFF, 0);
  95. nvkm_wrvgas(device, 0, 1, nvkm_rdvgas(device, 0, 1) & ~0x20);
  96. fbmem_fini(fb);
  97. }
  98. static int
  99. powerctrl_1_shift(int chip_version, int reg)
  100. {
  101. int shift = -4;
  102. if (chip_version < 0x17 || chip_version == 0x1a || chip_version == 0x20)
  103. return shift;
  104. switch (reg) {
  105. case 0x680520:
  106. shift += 4;
  107. case 0x680508:
  108. shift += 4;
  109. case 0x680504:
  110. shift += 4;
  111. case 0x680500:
  112. shift += 4;
  113. }
  114. /*
  115. * the shift for vpll regs is only used for nv3x chips with a single
  116. * stage pll
  117. */
  118. if (shift > 4 && (chip_version < 0x32 || chip_version == 0x35 ||
  119. chip_version == 0x36 || chip_version >= 0x40))
  120. shift = -4;
  121. return shift;
  122. }
  123. void
  124. setPLL_single(struct nvkm_devinit *init, u32 reg,
  125. struct nvkm_pll_vals *pv)
  126. {
  127. struct nvkm_device *device = init->subdev.device;
  128. int chip_version = device->bios->version.chip;
  129. uint32_t oldpll = nvkm_rd32(device, reg);
  130. int oldN = (oldpll >> 8) & 0xff, oldM = oldpll & 0xff;
  131. uint32_t pll = (oldpll & 0xfff80000) | pv->log2P << 16 | pv->NM1;
  132. uint32_t saved_powerctrl_1 = 0;
  133. int shift_powerctrl_1 = powerctrl_1_shift(chip_version, reg);
  134. if (oldpll == pll)
  135. return; /* already set */
  136. if (shift_powerctrl_1 >= 0) {
  137. saved_powerctrl_1 = nvkm_rd32(device, 0x001584);
  138. nvkm_wr32(device, 0x001584,
  139. (saved_powerctrl_1 & ~(0xf << shift_powerctrl_1)) |
  140. 1 << shift_powerctrl_1);
  141. }
  142. if (oldM && pv->M1 && (oldN / oldM < pv->N1 / pv->M1))
  143. /* upclock -- write new post divider first */
  144. nvkm_wr32(device, reg, pv->log2P << 16 | (oldpll & 0xffff));
  145. else
  146. /* downclock -- write new NM first */
  147. nvkm_wr32(device, reg, (oldpll & 0xffff0000) | pv->NM1);
  148. if ((chip_version < 0x17 || chip_version == 0x1a) &&
  149. chip_version != 0x11)
  150. /* wait a bit on older chips */
  151. msleep(64);
  152. nvkm_rd32(device, reg);
  153. /* then write the other half as well */
  154. nvkm_wr32(device, reg, pll);
  155. if (shift_powerctrl_1 >= 0)
  156. nvkm_wr32(device, 0x001584, saved_powerctrl_1);
  157. }
  158. static uint32_t
  159. new_ramdac580(uint32_t reg1, bool ss, uint32_t ramdac580)
  160. {
  161. bool head_a = (reg1 == 0x680508);
  162. if (ss) /* single stage pll mode */
  163. ramdac580 |= head_a ? 0x00000100 : 0x10000000;
  164. else
  165. ramdac580 &= head_a ? 0xfffffeff : 0xefffffff;
  166. return ramdac580;
  167. }
  168. void
  169. setPLL_double_highregs(struct nvkm_devinit *init, u32 reg1,
  170. struct nvkm_pll_vals *pv)
  171. {
  172. struct nvkm_device *device = init->subdev.device;
  173. int chip_version = device->bios->version.chip;
  174. bool nv3035 = chip_version == 0x30 || chip_version == 0x35;
  175. uint32_t reg2 = reg1 + ((reg1 == 0x680520) ? 0x5c : 0x70);
  176. uint32_t oldpll1 = nvkm_rd32(device, reg1);
  177. uint32_t oldpll2 = !nv3035 ? nvkm_rd32(device, reg2) : 0;
  178. uint32_t pll1 = (oldpll1 & 0xfff80000) | pv->log2P << 16 | pv->NM1;
  179. uint32_t pll2 = (oldpll2 & 0x7fff0000) | 1 << 31 | pv->NM2;
  180. uint32_t oldramdac580 = 0, ramdac580 = 0;
  181. bool single_stage = !pv->NM2 || pv->N2 == pv->M2; /* nv41+ only */
  182. uint32_t saved_powerctrl_1 = 0, savedc040 = 0;
  183. int shift_powerctrl_1 = powerctrl_1_shift(chip_version, reg1);
  184. /* model specific additions to generic pll1 and pll2 set up above */
  185. if (nv3035) {
  186. pll1 = (pll1 & 0xfcc7ffff) | (pv->N2 & 0x18) << 21 |
  187. (pv->N2 & 0x7) << 19 | 8 << 4 | (pv->M2 & 7) << 4;
  188. pll2 = 0;
  189. }
  190. if (chip_version > 0x40 && reg1 >= 0x680508) { /* !nv40 */
  191. oldramdac580 = nvkm_rd32(device, 0x680580);
  192. ramdac580 = new_ramdac580(reg1, single_stage, oldramdac580);
  193. if (oldramdac580 != ramdac580)
  194. oldpll1 = ~0; /* force mismatch */
  195. if (single_stage)
  196. /* magic value used by nvidia in single stage mode */
  197. pll2 |= 0x011f;
  198. }
  199. if (chip_version > 0x70)
  200. /* magic bits set by the blob (but not the bios) on g71-73 */
  201. pll1 = (pll1 & 0x7fffffff) | (single_stage ? 0x4 : 0xc) << 28;
  202. if (oldpll1 == pll1 && oldpll2 == pll2)
  203. return; /* already set */
  204. if (shift_powerctrl_1 >= 0) {
  205. saved_powerctrl_1 = nvkm_rd32(device, 0x001584);
  206. nvkm_wr32(device, 0x001584,
  207. (saved_powerctrl_1 & ~(0xf << shift_powerctrl_1)) |
  208. 1 << shift_powerctrl_1);
  209. }
  210. if (chip_version >= 0x40) {
  211. int shift_c040 = 14;
  212. switch (reg1) {
  213. case 0x680504:
  214. shift_c040 += 2;
  215. case 0x680500:
  216. shift_c040 += 2;
  217. case 0x680520:
  218. shift_c040 += 2;
  219. case 0x680508:
  220. shift_c040 += 2;
  221. }
  222. savedc040 = nvkm_rd32(device, 0xc040);
  223. if (shift_c040 != 14)
  224. nvkm_wr32(device, 0xc040, savedc040 & ~(3 << shift_c040));
  225. }
  226. if (oldramdac580 != ramdac580)
  227. nvkm_wr32(device, 0x680580, ramdac580);
  228. if (!nv3035)
  229. nvkm_wr32(device, reg2, pll2);
  230. nvkm_wr32(device, reg1, pll1);
  231. if (shift_powerctrl_1 >= 0)
  232. nvkm_wr32(device, 0x001584, saved_powerctrl_1);
  233. if (chip_version >= 0x40)
  234. nvkm_wr32(device, 0xc040, savedc040);
  235. }
  236. void
  237. setPLL_double_lowregs(struct nvkm_devinit *init, u32 NMNMreg,
  238. struct nvkm_pll_vals *pv)
  239. {
  240. /* When setting PLLs, there is a merry game of disabling and enabling
  241. * various bits of hardware during the process. This function is a
  242. * synthesis of six nv4x traces, nearly each card doing a subtly
  243. * different thing. With luck all the necessary bits for each card are
  244. * combined herein. Without luck it deviates from each card's formula
  245. * so as to not work on any :)
  246. */
  247. struct nvkm_device *device = init->subdev.device;
  248. uint32_t Preg = NMNMreg - 4;
  249. bool mpll = Preg == 0x4020;
  250. uint32_t oldPval = nvkm_rd32(device, Preg);
  251. uint32_t NMNM = pv->NM2 << 16 | pv->NM1;
  252. uint32_t Pval = (oldPval & (mpll ? ~(0x77 << 16) : ~(7 << 16))) |
  253. 0xc << 28 | pv->log2P << 16;
  254. uint32_t saved4600 = 0;
  255. /* some cards have different maskc040s */
  256. uint32_t maskc040 = ~(3 << 14), savedc040;
  257. bool single_stage = !pv->NM2 || pv->N2 == pv->M2;
  258. if (nvkm_rd32(device, NMNMreg) == NMNM && (oldPval & 0xc0070000) == Pval)
  259. return;
  260. if (Preg == 0x4000)
  261. maskc040 = ~0x333;
  262. if (Preg == 0x4058)
  263. maskc040 = ~(0xc << 24);
  264. if (mpll) {
  265. struct nvbios_pll info;
  266. uint8_t Pval2;
  267. if (nvbios_pll_parse(device->bios, Preg, &info))
  268. return;
  269. Pval2 = pv->log2P + info.bias_p;
  270. if (Pval2 > info.max_p)
  271. Pval2 = info.max_p;
  272. Pval |= 1 << 28 | Pval2 << 20;
  273. saved4600 = nvkm_rd32(device, 0x4600);
  274. nvkm_wr32(device, 0x4600, saved4600 | 8 << 28);
  275. }
  276. if (single_stage)
  277. Pval |= mpll ? 1 << 12 : 1 << 8;
  278. nvkm_wr32(device, Preg, oldPval | 1 << 28);
  279. nvkm_wr32(device, Preg, Pval & ~(4 << 28));
  280. if (mpll) {
  281. Pval |= 8 << 20;
  282. nvkm_wr32(device, 0x4020, Pval & ~(0xc << 28));
  283. nvkm_wr32(device, 0x4038, Pval & ~(0xc << 28));
  284. }
  285. savedc040 = nvkm_rd32(device, 0xc040);
  286. nvkm_wr32(device, 0xc040, savedc040 & maskc040);
  287. nvkm_wr32(device, NMNMreg, NMNM);
  288. if (NMNMreg == 0x4024)
  289. nvkm_wr32(device, 0x403c, NMNM);
  290. nvkm_wr32(device, Preg, Pval);
  291. if (mpll) {
  292. Pval &= ~(8 << 20);
  293. nvkm_wr32(device, 0x4020, Pval);
  294. nvkm_wr32(device, 0x4038, Pval);
  295. nvkm_wr32(device, 0x4600, saved4600);
  296. }
  297. nvkm_wr32(device, 0xc040, savedc040);
  298. if (mpll) {
  299. nvkm_wr32(device, 0x4020, Pval & ~(1 << 28));
  300. nvkm_wr32(device, 0x4038, Pval & ~(1 << 28));
  301. }
  302. }
  303. int
  304. nv04_devinit_pll_set(struct nvkm_devinit *devinit, u32 type, u32 freq)
  305. {
  306. struct nvkm_subdev *subdev = &devinit->subdev;
  307. struct nvkm_bios *bios = subdev->device->bios;
  308. struct nvkm_pll_vals pv;
  309. struct nvbios_pll info;
  310. int cv = bios->version.chip;
  311. int N1, M1, N2, M2, P;
  312. int ret;
  313. ret = nvbios_pll_parse(bios, type > 0x405c ? type : type - 4, &info);
  314. if (ret)
  315. return ret;
  316. ret = nv04_pll_calc(subdev, &info, freq, &N1, &M1, &N2, &M2, &P);
  317. if (!ret)
  318. return -EINVAL;
  319. pv.refclk = info.refclk;
  320. pv.N1 = N1;
  321. pv.M1 = M1;
  322. pv.N2 = N2;
  323. pv.M2 = M2;
  324. pv.log2P = P;
  325. if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
  326. cv >= 0x40) {
  327. if (type > 0x405c)
  328. setPLL_double_highregs(devinit, type, &pv);
  329. else
  330. setPLL_double_lowregs(devinit, type, &pv);
  331. } else
  332. setPLL_single(devinit, type, &pv);
  333. return 0;
  334. }
  335. int
  336. nv04_devinit_post(struct nvkm_devinit *init, bool execute)
  337. {
  338. return nvbios_init(&init->subdev, execute);
  339. }
  340. void
  341. nv04_devinit_preinit(struct nvkm_devinit *base)
  342. {
  343. struct nv04_devinit *init = nv04_devinit(base);
  344. struct nvkm_subdev *subdev = &init->base.subdev;
  345. struct nvkm_device *device = subdev->device;
  346. /* make i2c busses accessible */
  347. nvkm_mask(device, 0x000200, 0x00000001, 0x00000001);
  348. /* unslave crtcs */
  349. if (init->owner < 0)
  350. init->owner = nvkm_rdvgaowner(device);
  351. nvkm_wrvgaowner(device, 0);
  352. if (!init->base.post) {
  353. u32 htotal = nvkm_rdvgac(device, 0, 0x06);
  354. htotal |= (nvkm_rdvgac(device, 0, 0x07) & 0x01) << 8;
  355. htotal |= (nvkm_rdvgac(device, 0, 0x07) & 0x20) << 4;
  356. htotal |= (nvkm_rdvgac(device, 0, 0x25) & 0x01) << 10;
  357. htotal |= (nvkm_rdvgac(device, 0, 0x41) & 0x01) << 11;
  358. if (!htotal) {
  359. nvkm_debug(subdev, "adaptor not initialised\n");
  360. init->base.post = true;
  361. }
  362. }
  363. }
  364. void *
  365. nv04_devinit_dtor(struct nvkm_devinit *base)
  366. {
  367. struct nv04_devinit *init = nv04_devinit(base);
  368. /* restore vga owner saved at first init */
  369. nvkm_wrvgaowner(init->base.subdev.device, init->owner);
  370. return init;
  371. }
  372. int
  373. nv04_devinit_new_(const struct nvkm_devinit_func *func,
  374. struct nvkm_device *device, int index,
  375. struct nvkm_devinit **pinit)
  376. {
  377. struct nv04_devinit *init;
  378. if (!(init = kzalloc(sizeof(*init), GFP_KERNEL)))
  379. return -ENOMEM;
  380. *pinit = &init->base;
  381. nvkm_devinit_ctor(func, device, index, &init->base);
  382. init->owner = -1;
  383. return 0;
  384. }
  385. static const struct nvkm_devinit_func
  386. nv04_devinit = {
  387. .dtor = nv04_devinit_dtor,
  388. .preinit = nv04_devinit_preinit,
  389. .post = nv04_devinit_post,
  390. .meminit = nv04_devinit_meminit,
  391. .pll_set = nv04_devinit_pll_set,
  392. };
  393. int
  394. nv04_devinit_new(struct nvkm_device *device, int index,
  395. struct nvkm_devinit **pinit)
  396. {
  397. return nv04_devinit_new_(&nv04_devinit, device, index, pinit);
  398. }