pll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright 2005-2006 Erik Waling
  3. * Copyright 2006 Stephane Marchesin
  4. * Copyright 2007-2009 Stuart Bennett
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  21. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include <subdev/bios.h>
  25. #include <subdev/bios/bit.h>
  26. #include <subdev/bios/bmp.h>
  27. #include <subdev/bios/pll.h>
  28. #include <subdev/vga.h>
  29. struct pll_mapping {
  30. u8 type;
  31. u32 reg;
  32. };
  33. static struct pll_mapping
  34. nv04_pll_mapping[] = {
  35. { PLL_CORE , 0x680500 },
  36. { PLL_MEMORY, 0x680504 },
  37. { PLL_VPLL0 , 0x680508 },
  38. { PLL_VPLL1 , 0x680520 },
  39. {}
  40. };
  41. static struct pll_mapping
  42. nv40_pll_mapping[] = {
  43. { PLL_CORE , 0x004000 },
  44. { PLL_MEMORY, 0x004020 },
  45. { PLL_VPLL0 , 0x680508 },
  46. { PLL_VPLL1 , 0x680520 },
  47. {}
  48. };
  49. static struct pll_mapping
  50. nv50_pll_mapping[] = {
  51. { PLL_CORE , 0x004028 },
  52. { PLL_SHADER, 0x004020 },
  53. { PLL_UNK03 , 0x004000 },
  54. { PLL_MEMORY, 0x004008 },
  55. { PLL_UNK40 , 0x00e810 },
  56. { PLL_UNK41 , 0x00e818 },
  57. { PLL_UNK42 , 0x00e824 },
  58. { PLL_VPLL0 , 0x614100 },
  59. { PLL_VPLL1 , 0x614900 },
  60. {}
  61. };
  62. static struct pll_mapping
  63. g84_pll_mapping[] = {
  64. { PLL_CORE , 0x004028 },
  65. { PLL_SHADER, 0x004020 },
  66. { PLL_MEMORY, 0x004008 },
  67. { PLL_VDEC , 0x004030 },
  68. { PLL_UNK41 , 0x00e818 },
  69. { PLL_VPLL0 , 0x614100 },
  70. { PLL_VPLL1 , 0x614900 },
  71. {}
  72. };
  73. static u32
  74. pll_limits_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
  75. {
  76. struct bit_entry bit_C;
  77. u32 data = 0x0000;
  78. if (!bit_entry(bios, 'C', &bit_C)) {
  79. if (bit_C.version == 1 && bit_C.length >= 10)
  80. data = nvbios_rd16(bios, bit_C.offset + 8);
  81. if (bit_C.version == 2 && bit_C.length >= 4)
  82. data = nvbios_rd32(bios, bit_C.offset + 0);
  83. if (data) {
  84. *ver = nvbios_rd08(bios, data + 0);
  85. *hdr = nvbios_rd08(bios, data + 1);
  86. *len = nvbios_rd08(bios, data + 2);
  87. *cnt = nvbios_rd08(bios, data + 3);
  88. return data;
  89. }
  90. }
  91. if (bmp_version(bios) >= 0x0524) {
  92. data = nvbios_rd16(bios, bios->bmp_offset + 142);
  93. if (data) {
  94. *ver = nvbios_rd08(bios, data + 0);
  95. *hdr = 1;
  96. *cnt = 1;
  97. *len = 0x18;
  98. return data;
  99. }
  100. }
  101. *ver = 0x00;
  102. return data;
  103. }
  104. static struct pll_mapping *
  105. pll_map(struct nvkm_bios *bios)
  106. {
  107. struct nvkm_device *device = bios->subdev.device;
  108. switch (device->card_type) {
  109. case NV_04:
  110. case NV_10:
  111. case NV_11:
  112. case NV_20:
  113. case NV_30:
  114. return nv04_pll_mapping;
  115. break;
  116. case NV_40:
  117. return nv40_pll_mapping;
  118. case NV_50:
  119. if (device->chipset == 0x50)
  120. return nv50_pll_mapping;
  121. else
  122. if (device->chipset < 0xa3 ||
  123. device->chipset == 0xaa ||
  124. device->chipset == 0xac)
  125. return g84_pll_mapping;
  126. default:
  127. return NULL;
  128. }
  129. }
  130. static u32
  131. pll_map_reg(struct nvkm_bios *bios, u32 reg, u32 *type, u8 *ver, u8 *len)
  132. {
  133. struct pll_mapping *map;
  134. u8 hdr, cnt;
  135. u32 data;
  136. data = pll_limits_table(bios, ver, &hdr, &cnt, len);
  137. if (data && *ver >= 0x30) {
  138. data += hdr;
  139. while (cnt--) {
  140. if (nvbios_rd32(bios, data + 3) == reg) {
  141. *type = nvbios_rd08(bios, data + 0);
  142. return data;
  143. }
  144. data += *len;
  145. }
  146. return 0x0000;
  147. }
  148. map = pll_map(bios);
  149. while (map && map->reg) {
  150. if (map->reg == reg && *ver >= 0x20) {
  151. u32 addr = (data += hdr);
  152. *type = map->type;
  153. while (cnt--) {
  154. if (nvbios_rd32(bios, data) == map->reg)
  155. return data;
  156. data += *len;
  157. }
  158. return addr;
  159. } else
  160. if (map->reg == reg) {
  161. *type = map->type;
  162. return data + 1;
  163. }
  164. map++;
  165. }
  166. return 0x0000;
  167. }
  168. static u32
  169. pll_map_type(struct nvkm_bios *bios, u8 type, u32 *reg, u8 *ver, u8 *len)
  170. {
  171. struct pll_mapping *map;
  172. u8 hdr, cnt;
  173. u32 data;
  174. data = pll_limits_table(bios, ver, &hdr, &cnt, len);
  175. if (data && *ver >= 0x30) {
  176. data += hdr;
  177. while (cnt--) {
  178. if (nvbios_rd08(bios, data + 0) == type) {
  179. *reg = nvbios_rd32(bios, data + 3);
  180. return data;
  181. }
  182. data += *len;
  183. }
  184. return 0x0000;
  185. }
  186. map = pll_map(bios);
  187. while (map && map->reg) {
  188. if (map->type == type && *ver >= 0x20) {
  189. u32 addr = (data += hdr);
  190. *reg = map->reg;
  191. while (cnt--) {
  192. if (nvbios_rd32(bios, data) == map->reg)
  193. return data;
  194. data += *len;
  195. }
  196. return addr;
  197. } else
  198. if (map->type == type) {
  199. *reg = map->reg;
  200. return data + 1;
  201. }
  202. map++;
  203. }
  204. return 0x0000;
  205. }
  206. int
  207. nvbios_pll_parse(struct nvkm_bios *bios, u32 type, struct nvbios_pll *info)
  208. {
  209. struct nvkm_subdev *subdev = &bios->subdev;
  210. struct nvkm_device *device = subdev->device;
  211. u8 ver, len;
  212. u32 reg = type;
  213. u32 data;
  214. if (type > PLL_MAX) {
  215. reg = type;
  216. data = pll_map_reg(bios, reg, &type, &ver, &len);
  217. } else {
  218. data = pll_map_type(bios, type, &reg, &ver, &len);
  219. }
  220. if (ver && !data)
  221. return -ENOENT;
  222. memset(info, 0, sizeof(*info));
  223. info->type = type;
  224. info->reg = reg;
  225. switch (ver) {
  226. case 0x00:
  227. break;
  228. case 0x10:
  229. case 0x11:
  230. info->vco1.min_freq = nvbios_rd32(bios, data + 0);
  231. info->vco1.max_freq = nvbios_rd32(bios, data + 4);
  232. info->vco2.min_freq = nvbios_rd32(bios, data + 8);
  233. info->vco2.max_freq = nvbios_rd32(bios, data + 12);
  234. info->vco1.min_inputfreq = nvbios_rd32(bios, data + 16);
  235. info->vco2.min_inputfreq = nvbios_rd32(bios, data + 20);
  236. info->vco1.max_inputfreq = INT_MAX;
  237. info->vco2.max_inputfreq = INT_MAX;
  238. info->max_p = 0x7;
  239. info->max_p_usable = 0x6;
  240. /* these values taken from nv30/31/36 */
  241. switch (bios->version.chip) {
  242. case 0x36:
  243. info->vco1.min_n = 0x5;
  244. break;
  245. default:
  246. info->vco1.min_n = 0x1;
  247. break;
  248. }
  249. info->vco1.max_n = 0xff;
  250. info->vco1.min_m = 0x1;
  251. info->vco1.max_m = 0xd;
  252. /*
  253. * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
  254. * table version (apart from nv35)), N2 is compared to
  255. * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
  256. * save a comparison
  257. */
  258. info->vco2.min_n = 0x4;
  259. switch (bios->version.chip) {
  260. case 0x30:
  261. case 0x35:
  262. info->vco2.max_n = 0x1f;
  263. break;
  264. default:
  265. info->vco2.max_n = 0x28;
  266. break;
  267. }
  268. info->vco2.min_m = 0x1;
  269. info->vco2.max_m = 0x4;
  270. break;
  271. case 0x20:
  272. case 0x21:
  273. info->vco1.min_freq = nvbios_rd16(bios, data + 4) * 1000;
  274. info->vco1.max_freq = nvbios_rd16(bios, data + 6) * 1000;
  275. info->vco2.min_freq = nvbios_rd16(bios, data + 8) * 1000;
  276. info->vco2.max_freq = nvbios_rd16(bios, data + 10) * 1000;
  277. info->vco1.min_inputfreq = nvbios_rd16(bios, data + 12) * 1000;
  278. info->vco2.min_inputfreq = nvbios_rd16(bios, data + 14) * 1000;
  279. info->vco1.max_inputfreq = nvbios_rd16(bios, data + 16) * 1000;
  280. info->vco2.max_inputfreq = nvbios_rd16(bios, data + 18) * 1000;
  281. info->vco1.min_n = nvbios_rd08(bios, data + 20);
  282. info->vco1.max_n = nvbios_rd08(bios, data + 21);
  283. info->vco1.min_m = nvbios_rd08(bios, data + 22);
  284. info->vco1.max_m = nvbios_rd08(bios, data + 23);
  285. info->vco2.min_n = nvbios_rd08(bios, data + 24);
  286. info->vco2.max_n = nvbios_rd08(bios, data + 25);
  287. info->vco2.min_m = nvbios_rd08(bios, data + 26);
  288. info->vco2.max_m = nvbios_rd08(bios, data + 27);
  289. info->max_p = nvbios_rd08(bios, data + 29);
  290. info->max_p_usable = info->max_p;
  291. if (bios->version.chip < 0x60)
  292. info->max_p_usable = 0x6;
  293. info->bias_p = nvbios_rd08(bios, data + 30);
  294. if (len > 0x22)
  295. info->refclk = nvbios_rd32(bios, data + 31);
  296. break;
  297. case 0x30:
  298. data = nvbios_rd16(bios, data + 1);
  299. info->vco1.min_freq = nvbios_rd16(bios, data + 0) * 1000;
  300. info->vco1.max_freq = nvbios_rd16(bios, data + 2) * 1000;
  301. info->vco2.min_freq = nvbios_rd16(bios, data + 4) * 1000;
  302. info->vco2.max_freq = nvbios_rd16(bios, data + 6) * 1000;
  303. info->vco1.min_inputfreq = nvbios_rd16(bios, data + 8) * 1000;
  304. info->vco2.min_inputfreq = nvbios_rd16(bios, data + 10) * 1000;
  305. info->vco1.max_inputfreq = nvbios_rd16(bios, data + 12) * 1000;
  306. info->vco2.max_inputfreq = nvbios_rd16(bios, data + 14) * 1000;
  307. info->vco1.min_n = nvbios_rd08(bios, data + 16);
  308. info->vco1.max_n = nvbios_rd08(bios, data + 17);
  309. info->vco1.min_m = nvbios_rd08(bios, data + 18);
  310. info->vco1.max_m = nvbios_rd08(bios, data + 19);
  311. info->vco2.min_n = nvbios_rd08(bios, data + 20);
  312. info->vco2.max_n = nvbios_rd08(bios, data + 21);
  313. info->vco2.min_m = nvbios_rd08(bios, data + 22);
  314. info->vco2.max_m = nvbios_rd08(bios, data + 23);
  315. info->max_p_usable = info->max_p = nvbios_rd08(bios, data + 25);
  316. info->bias_p = nvbios_rd08(bios, data + 27);
  317. info->refclk = nvbios_rd32(bios, data + 28);
  318. break;
  319. case 0x40:
  320. info->refclk = nvbios_rd16(bios, data + 9) * 1000;
  321. data = nvbios_rd16(bios, data + 1);
  322. info->vco1.min_freq = nvbios_rd16(bios, data + 0) * 1000;
  323. info->vco1.max_freq = nvbios_rd16(bios, data + 2) * 1000;
  324. info->vco1.min_inputfreq = nvbios_rd16(bios, data + 4) * 1000;
  325. info->vco1.max_inputfreq = nvbios_rd16(bios, data + 6) * 1000;
  326. info->vco1.min_m = nvbios_rd08(bios, data + 8);
  327. info->vco1.max_m = nvbios_rd08(bios, data + 9);
  328. info->vco1.min_n = nvbios_rd08(bios, data + 10);
  329. info->vco1.max_n = nvbios_rd08(bios, data + 11);
  330. info->min_p = nvbios_rd08(bios, data + 12);
  331. info->max_p = nvbios_rd08(bios, data + 13);
  332. break;
  333. default:
  334. nvkm_error(subdev, "unknown pll limits version 0x%02x\n", ver);
  335. return -EINVAL;
  336. }
  337. if (!info->refclk) {
  338. info->refclk = device->crystal;
  339. if (bios->version.chip == 0x51) {
  340. u32 sel_clk = nvkm_rd32(device, 0x680524);
  341. if ((info->reg == 0x680508 && sel_clk & 0x20) ||
  342. (info->reg == 0x680520 && sel_clk & 0x80)) {
  343. if (nvkm_rdvgac(device, 0, 0x27) < 0xa3)
  344. info->refclk = 200000;
  345. else
  346. info->refclk = 25000;
  347. }
  348. }
  349. }
  350. /*
  351. * By now any valid limit table ought to have set a max frequency for
  352. * vco1, so if it's zero it's either a pre limit table bios, or one
  353. * with an empty limit table (seen on nv18)
  354. */
  355. if (!info->vco1.max_freq) {
  356. info->vco1.max_freq = nvbios_rd32(bios, bios->bmp_offset + 67);
  357. info->vco1.min_freq = nvbios_rd32(bios, bios->bmp_offset + 71);
  358. if (bmp_version(bios) < 0x0506) {
  359. info->vco1.max_freq = 256000;
  360. info->vco1.min_freq = 128000;
  361. }
  362. info->vco1.min_inputfreq = 0;
  363. info->vco1.max_inputfreq = INT_MAX;
  364. info->vco1.min_n = 0x1;
  365. info->vco1.max_n = 0xff;
  366. info->vco1.min_m = 0x1;
  367. if (device->crystal == 13500) {
  368. /* nv05 does this, nv11 doesn't, nv10 unknown */
  369. if (bios->version.chip < 0x11)
  370. info->vco1.min_m = 0x7;
  371. info->vco1.max_m = 0xd;
  372. } else {
  373. if (bios->version.chip < 0x11)
  374. info->vco1.min_m = 0x8;
  375. info->vco1.max_m = 0xe;
  376. }
  377. if (bios->version.chip < 0x17 ||
  378. bios->version.chip == 0x1a ||
  379. bios->version.chip == 0x20)
  380. info->max_p = 4;
  381. else
  382. info->max_p = 5;
  383. info->max_p_usable = info->max_p;
  384. }
  385. return 0;
  386. }