pll.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 u16
  74. pll_limits_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
  75. {
  76. struct bit_entry bit_C;
  77. if (!bit_entry(bios, 'C', &bit_C) && bit_C.length >= 10) {
  78. u16 data = nvbios_rd16(bios, bit_C.offset + 8);
  79. if (data) {
  80. *ver = nvbios_rd08(bios, data + 0);
  81. *hdr = nvbios_rd08(bios, data + 1);
  82. *len = nvbios_rd08(bios, data + 2);
  83. *cnt = nvbios_rd08(bios, data + 3);
  84. return data;
  85. }
  86. }
  87. if (bmp_version(bios) >= 0x0524) {
  88. u16 data = nvbios_rd16(bios, bios->bmp_offset + 142);
  89. if (data) {
  90. *ver = nvbios_rd08(bios, data + 0);
  91. *hdr = 1;
  92. *cnt = 1;
  93. *len = 0x18;
  94. return data;
  95. }
  96. }
  97. *ver = 0x00;
  98. return 0x0000;
  99. }
  100. static struct pll_mapping *
  101. pll_map(struct nvkm_bios *bios)
  102. {
  103. struct nvkm_device *device = bios->subdev.device;
  104. switch (device->card_type) {
  105. case NV_04:
  106. case NV_10:
  107. case NV_11:
  108. case NV_20:
  109. case NV_30:
  110. return nv04_pll_mapping;
  111. break;
  112. case NV_40:
  113. return nv40_pll_mapping;
  114. case NV_50:
  115. if (device->chipset == 0x50)
  116. return nv50_pll_mapping;
  117. else
  118. if (device->chipset < 0xa3 ||
  119. device->chipset == 0xaa ||
  120. device->chipset == 0xac)
  121. return g84_pll_mapping;
  122. default:
  123. return NULL;
  124. }
  125. }
  126. static u16
  127. pll_map_reg(struct nvkm_bios *bios, u32 reg, u32 *type, u8 *ver, u8 *len)
  128. {
  129. struct pll_mapping *map;
  130. u8 hdr, cnt;
  131. u16 data;
  132. data = pll_limits_table(bios, ver, &hdr, &cnt, len);
  133. if (data && *ver >= 0x30) {
  134. data += hdr;
  135. while (cnt--) {
  136. if (nvbios_rd32(bios, data + 3) == reg) {
  137. *type = nvbios_rd08(bios, data + 0);
  138. return data;
  139. }
  140. data += *len;
  141. }
  142. return 0x0000;
  143. }
  144. map = pll_map(bios);
  145. while (map->reg) {
  146. if (map->reg == reg && *ver >= 0x20) {
  147. u16 addr = (data += hdr);
  148. *type = map->type;
  149. while (cnt--) {
  150. if (nvbios_rd32(bios, data) == map->reg)
  151. return data;
  152. data += *len;
  153. }
  154. return addr;
  155. } else
  156. if (map->reg == reg) {
  157. *type = map->type;
  158. return data + 1;
  159. }
  160. map++;
  161. }
  162. return 0x0000;
  163. }
  164. static u16
  165. pll_map_type(struct nvkm_bios *bios, u8 type, u32 *reg, u8 *ver, u8 *len)
  166. {
  167. struct pll_mapping *map;
  168. u8 hdr, cnt;
  169. u16 data;
  170. data = pll_limits_table(bios, ver, &hdr, &cnt, len);
  171. if (data && *ver >= 0x30) {
  172. data += hdr;
  173. while (cnt--) {
  174. if (nvbios_rd08(bios, data + 0) == type) {
  175. *reg = nvbios_rd32(bios, data + 3);
  176. return data;
  177. }
  178. data += *len;
  179. }
  180. return 0x0000;
  181. }
  182. map = pll_map(bios);
  183. while (map->reg) {
  184. if (map->type == type && *ver >= 0x20) {
  185. u16 addr = (data += hdr);
  186. *reg = map->reg;
  187. while (cnt--) {
  188. if (nvbios_rd32(bios, data) == map->reg)
  189. return data;
  190. data += *len;
  191. }
  192. return addr;
  193. } else
  194. if (map->type == type) {
  195. *reg = map->reg;
  196. return data + 1;
  197. }
  198. map++;
  199. }
  200. return 0x0000;
  201. }
  202. int
  203. nvbios_pll_parse(struct nvkm_bios *bios, u32 type, struct nvbios_pll *info)
  204. {
  205. struct nvkm_subdev *subdev = &bios->subdev;
  206. struct nvkm_device *device = subdev->device;
  207. u8 ver, len;
  208. u32 reg = type;
  209. u16 data;
  210. if (type > PLL_MAX) {
  211. reg = type;
  212. data = pll_map_reg(bios, reg, &type, &ver, &len);
  213. } else {
  214. data = pll_map_type(bios, type, &reg, &ver, &len);
  215. }
  216. if (ver && !data)
  217. return -ENOENT;
  218. memset(info, 0, sizeof(*info));
  219. info->type = type;
  220. info->reg = reg;
  221. switch (ver) {
  222. case 0x00:
  223. break;
  224. case 0x10:
  225. case 0x11:
  226. info->vco1.min_freq = nvbios_rd32(bios, data + 0);
  227. info->vco1.max_freq = nvbios_rd32(bios, data + 4);
  228. info->vco2.min_freq = nvbios_rd32(bios, data + 8);
  229. info->vco2.max_freq = nvbios_rd32(bios, data + 12);
  230. info->vco1.min_inputfreq = nvbios_rd32(bios, data + 16);
  231. info->vco2.min_inputfreq = nvbios_rd32(bios, data + 20);
  232. info->vco1.max_inputfreq = INT_MAX;
  233. info->vco2.max_inputfreq = INT_MAX;
  234. info->max_p = 0x7;
  235. info->max_p_usable = 0x6;
  236. /* these values taken from nv30/31/36 */
  237. switch (bios->version.chip) {
  238. case 0x36:
  239. info->vco1.min_n = 0x5;
  240. break;
  241. default:
  242. info->vco1.min_n = 0x1;
  243. break;
  244. }
  245. info->vco1.max_n = 0xff;
  246. info->vco1.min_m = 0x1;
  247. info->vco1.max_m = 0xd;
  248. /*
  249. * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
  250. * table version (apart from nv35)), N2 is compared to
  251. * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
  252. * save a comparison
  253. */
  254. info->vco2.min_n = 0x4;
  255. switch (bios->version.chip) {
  256. case 0x30:
  257. case 0x35:
  258. info->vco2.max_n = 0x1f;
  259. break;
  260. default:
  261. info->vco2.max_n = 0x28;
  262. break;
  263. }
  264. info->vco2.min_m = 0x1;
  265. info->vco2.max_m = 0x4;
  266. break;
  267. case 0x20:
  268. case 0x21:
  269. info->vco1.min_freq = nvbios_rd16(bios, data + 4) * 1000;
  270. info->vco1.max_freq = nvbios_rd16(bios, data + 6) * 1000;
  271. info->vco2.min_freq = nvbios_rd16(bios, data + 8) * 1000;
  272. info->vco2.max_freq = nvbios_rd16(bios, data + 10) * 1000;
  273. info->vco1.min_inputfreq = nvbios_rd16(bios, data + 12) * 1000;
  274. info->vco2.min_inputfreq = nvbios_rd16(bios, data + 14) * 1000;
  275. info->vco1.max_inputfreq = nvbios_rd16(bios, data + 16) * 1000;
  276. info->vco2.max_inputfreq = nvbios_rd16(bios, data + 18) * 1000;
  277. info->vco1.min_n = nvbios_rd08(bios, data + 20);
  278. info->vco1.max_n = nvbios_rd08(bios, data + 21);
  279. info->vco1.min_m = nvbios_rd08(bios, data + 22);
  280. info->vco1.max_m = nvbios_rd08(bios, data + 23);
  281. info->vco2.min_n = nvbios_rd08(bios, data + 24);
  282. info->vco2.max_n = nvbios_rd08(bios, data + 25);
  283. info->vco2.min_m = nvbios_rd08(bios, data + 26);
  284. info->vco2.max_m = nvbios_rd08(bios, data + 27);
  285. info->max_p = nvbios_rd08(bios, data + 29);
  286. info->max_p_usable = info->max_p;
  287. if (bios->version.chip < 0x60)
  288. info->max_p_usable = 0x6;
  289. info->bias_p = nvbios_rd08(bios, data + 30);
  290. if (len > 0x22)
  291. info->refclk = nvbios_rd32(bios, data + 31);
  292. break;
  293. case 0x30:
  294. data = nvbios_rd16(bios, data + 1);
  295. info->vco1.min_freq = nvbios_rd16(bios, data + 0) * 1000;
  296. info->vco1.max_freq = nvbios_rd16(bios, data + 2) * 1000;
  297. info->vco2.min_freq = nvbios_rd16(bios, data + 4) * 1000;
  298. info->vco2.max_freq = nvbios_rd16(bios, data + 6) * 1000;
  299. info->vco1.min_inputfreq = nvbios_rd16(bios, data + 8) * 1000;
  300. info->vco2.min_inputfreq = nvbios_rd16(bios, data + 10) * 1000;
  301. info->vco1.max_inputfreq = nvbios_rd16(bios, data + 12) * 1000;
  302. info->vco2.max_inputfreq = nvbios_rd16(bios, data + 14) * 1000;
  303. info->vco1.min_n = nvbios_rd08(bios, data + 16);
  304. info->vco1.max_n = nvbios_rd08(bios, data + 17);
  305. info->vco1.min_m = nvbios_rd08(bios, data + 18);
  306. info->vco1.max_m = nvbios_rd08(bios, data + 19);
  307. info->vco2.min_n = nvbios_rd08(bios, data + 20);
  308. info->vco2.max_n = nvbios_rd08(bios, data + 21);
  309. info->vco2.min_m = nvbios_rd08(bios, data + 22);
  310. info->vco2.max_m = nvbios_rd08(bios, data + 23);
  311. info->max_p_usable = info->max_p = nvbios_rd08(bios, data + 25);
  312. info->bias_p = nvbios_rd08(bios, data + 27);
  313. info->refclk = nvbios_rd32(bios, data + 28);
  314. break;
  315. case 0x40:
  316. info->refclk = nvbios_rd16(bios, data + 9) * 1000;
  317. data = nvbios_rd16(bios, data + 1);
  318. info->vco1.min_freq = nvbios_rd16(bios, data + 0) * 1000;
  319. info->vco1.max_freq = nvbios_rd16(bios, data + 2) * 1000;
  320. info->vco1.min_inputfreq = nvbios_rd16(bios, data + 4) * 1000;
  321. info->vco1.max_inputfreq = nvbios_rd16(bios, data + 6) * 1000;
  322. info->vco1.min_m = nvbios_rd08(bios, data + 8);
  323. info->vco1.max_m = nvbios_rd08(bios, data + 9);
  324. info->vco1.min_n = nvbios_rd08(bios, data + 10);
  325. info->vco1.max_n = nvbios_rd08(bios, data + 11);
  326. info->min_p = nvbios_rd08(bios, data + 12);
  327. info->max_p = nvbios_rd08(bios, data + 13);
  328. break;
  329. default:
  330. nvkm_error(subdev, "unknown pll limits version 0x%02x\n", ver);
  331. return -EINVAL;
  332. }
  333. if (!info->refclk) {
  334. info->refclk = device->crystal;
  335. if (bios->version.chip == 0x51) {
  336. u32 sel_clk = nvkm_rd32(device, 0x680524);
  337. if ((info->reg == 0x680508 && sel_clk & 0x20) ||
  338. (info->reg == 0x680520 && sel_clk & 0x80)) {
  339. if (nvkm_rdvgac(device, 0, 0x27) < 0xa3)
  340. info->refclk = 200000;
  341. else
  342. info->refclk = 25000;
  343. }
  344. }
  345. }
  346. /*
  347. * By now any valid limit table ought to have set a max frequency for
  348. * vco1, so if it's zero it's either a pre limit table bios, or one
  349. * with an empty limit table (seen on nv18)
  350. */
  351. if (!info->vco1.max_freq) {
  352. info->vco1.max_freq = nvbios_rd32(bios, bios->bmp_offset + 67);
  353. info->vco1.min_freq = nvbios_rd32(bios, bios->bmp_offset + 71);
  354. if (bmp_version(bios) < 0x0506) {
  355. info->vco1.max_freq = 256000;
  356. info->vco1.min_freq = 128000;
  357. }
  358. info->vco1.min_inputfreq = 0;
  359. info->vco1.max_inputfreq = INT_MAX;
  360. info->vco1.min_n = 0x1;
  361. info->vco1.max_n = 0xff;
  362. info->vco1.min_m = 0x1;
  363. if (device->crystal == 13500) {
  364. /* nv05 does this, nv11 doesn't, nv10 unknown */
  365. if (bios->version.chip < 0x11)
  366. info->vco1.min_m = 0x7;
  367. info->vco1.max_m = 0xd;
  368. } else {
  369. if (bios->version.chip < 0x11)
  370. info->vco1.min_m = 0x8;
  371. info->vco1.max_m = 0xe;
  372. }
  373. if (bios->version.chip < 0x17 ||
  374. bios->version.chip == 0x1a ||
  375. bios->version.chip == 0x20)
  376. info->max_p = 4;
  377. else
  378. info->max_p = 5;
  379. info->max_p_usable = info->max_p;
  380. }
  381. return 0;
  382. }