gt215.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright 2012 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. * Roy Spliet
  24. */
  25. #define gt215_clk(p) container_of((p), struct gt215_clk, base)
  26. #include "gt215.h"
  27. #include "pll.h"
  28. #include <engine/fifo.h>
  29. #include <subdev/bios.h>
  30. #include <subdev/bios/pll.h>
  31. #include <subdev/timer.h>
  32. struct gt215_clk {
  33. struct nvkm_clk base;
  34. struct gt215_clk_info eng[nv_clk_src_max];
  35. };
  36. static u32 read_clk(struct gt215_clk *, int, bool);
  37. static u32 read_pll(struct gt215_clk *, int, u32);
  38. static u32
  39. read_vco(struct gt215_clk *clk, int idx)
  40. {
  41. struct nvkm_device *device = clk->base.subdev.device;
  42. u32 sctl = nvkm_rd32(device, 0x4120 + (idx * 4));
  43. switch (sctl & 0x00000030) {
  44. case 0x00000000:
  45. return device->crystal;
  46. case 0x00000020:
  47. return read_pll(clk, 0x41, 0x00e820);
  48. case 0x00000030:
  49. return read_pll(clk, 0x42, 0x00e8a0);
  50. default:
  51. return 0;
  52. }
  53. }
  54. static u32
  55. read_clk(struct gt215_clk *clk, int idx, bool ignore_en)
  56. {
  57. struct nvkm_device *device = clk->base.subdev.device;
  58. u32 sctl, sdiv, sclk;
  59. /* refclk for the 0xe8xx plls is a fixed frequency */
  60. if (idx >= 0x40) {
  61. if (device->chipset == 0xaf) {
  62. /* no joke.. seriously.. sigh.. */
  63. return nvkm_rd32(device, 0x00471c) * 1000;
  64. }
  65. return device->crystal;
  66. }
  67. sctl = nvkm_rd32(device, 0x4120 + (idx * 4));
  68. if (!ignore_en && !(sctl & 0x00000100))
  69. return 0;
  70. /* out_alt */
  71. if (sctl & 0x00000400)
  72. return 108000;
  73. /* vco_out */
  74. switch (sctl & 0x00003000) {
  75. case 0x00000000:
  76. if (!(sctl & 0x00000200))
  77. return device->crystal;
  78. return 0;
  79. case 0x00002000:
  80. if (sctl & 0x00000040)
  81. return 108000;
  82. return 100000;
  83. case 0x00003000:
  84. /* vco_enable */
  85. if (!(sctl & 0x00000001))
  86. return 0;
  87. sclk = read_vco(clk, idx);
  88. sdiv = ((sctl & 0x003f0000) >> 16) + 2;
  89. return (sclk * 2) / sdiv;
  90. default:
  91. return 0;
  92. }
  93. }
  94. static u32
  95. read_pll(struct gt215_clk *clk, int idx, u32 pll)
  96. {
  97. struct nvkm_device *device = clk->base.subdev.device;
  98. u32 ctrl = nvkm_rd32(device, pll + 0);
  99. u32 sclk = 0, P = 1, N = 1, M = 1;
  100. if (!(ctrl & 0x00000008)) {
  101. if (ctrl & 0x00000001) {
  102. u32 coef = nvkm_rd32(device, pll + 4);
  103. M = (coef & 0x000000ff) >> 0;
  104. N = (coef & 0x0000ff00) >> 8;
  105. P = (coef & 0x003f0000) >> 16;
  106. /* no post-divider on these..
  107. * XXX: it looks more like two post-"dividers" that
  108. * cross each other out in the default RPLL config */
  109. if ((pll & 0x00ff00) == 0x00e800)
  110. P = 1;
  111. sclk = read_clk(clk, 0x00 + idx, false);
  112. }
  113. } else {
  114. sclk = read_clk(clk, 0x10 + idx, false);
  115. }
  116. if (M * P)
  117. return sclk * N / (M * P);
  118. return 0;
  119. }
  120. static int
  121. gt215_clk_read(struct nvkm_clk *base, enum nv_clk_src src)
  122. {
  123. struct gt215_clk *clk = gt215_clk(base);
  124. struct nvkm_subdev *subdev = &clk->base.subdev;
  125. struct nvkm_device *device = subdev->device;
  126. u32 hsrc;
  127. switch (src) {
  128. case nv_clk_src_crystal:
  129. return device->crystal;
  130. case nv_clk_src_core:
  131. case nv_clk_src_core_intm:
  132. return read_pll(clk, 0x00, 0x4200);
  133. case nv_clk_src_shader:
  134. return read_pll(clk, 0x01, 0x4220);
  135. case nv_clk_src_mem:
  136. return read_pll(clk, 0x02, 0x4000);
  137. case nv_clk_src_disp:
  138. return read_clk(clk, 0x20, false);
  139. case nv_clk_src_vdec:
  140. return read_clk(clk, 0x21, false);
  141. case nv_clk_src_pmu:
  142. return read_clk(clk, 0x25, false);
  143. case nv_clk_src_host:
  144. hsrc = (nvkm_rd32(device, 0xc040) & 0x30000000) >> 28;
  145. switch (hsrc) {
  146. case 0:
  147. return read_clk(clk, 0x1d, false);
  148. case 2:
  149. case 3:
  150. return 277000;
  151. default:
  152. nvkm_error(subdev, "unknown HOST clock source %d\n", hsrc);
  153. return -EINVAL;
  154. }
  155. default:
  156. nvkm_error(subdev, "invalid clock source %d\n", src);
  157. return -EINVAL;
  158. }
  159. return 0;
  160. }
  161. static int
  162. gt215_clk_info(struct nvkm_clk *base, int idx, u32 khz,
  163. struct gt215_clk_info *info)
  164. {
  165. struct gt215_clk *clk = gt215_clk(base);
  166. u32 oclk, sclk, sdiv;
  167. s32 diff;
  168. info->clk = 0;
  169. switch (khz) {
  170. case 27000:
  171. info->clk = 0x00000100;
  172. return khz;
  173. case 100000:
  174. info->clk = 0x00002100;
  175. return khz;
  176. case 108000:
  177. info->clk = 0x00002140;
  178. return khz;
  179. default:
  180. sclk = read_vco(clk, idx);
  181. sdiv = min((sclk * 2) / khz, (u32)65);
  182. oclk = (sclk * 2) / sdiv;
  183. diff = ((khz + 3000) - oclk);
  184. /* When imprecise, play it safe and aim for a clock lower than
  185. * desired rather than higher */
  186. if (diff < 0) {
  187. sdiv++;
  188. oclk = (sclk * 2) / sdiv;
  189. }
  190. /* divider can go as low as 2, limited here because NVIDIA
  191. * and the VBIOS on my NVA8 seem to prefer using the PLL
  192. * for 810MHz - is there a good reason?
  193. * XXX: PLLs with refclk 810MHz? */
  194. if (sdiv > 4) {
  195. info->clk = (((sdiv - 2) << 16) | 0x00003100);
  196. return oclk;
  197. }
  198. break;
  199. }
  200. return -ERANGE;
  201. }
  202. int
  203. gt215_pll_info(struct nvkm_clk *base, int idx, u32 pll, u32 khz,
  204. struct gt215_clk_info *info)
  205. {
  206. struct gt215_clk *clk = gt215_clk(base);
  207. struct nvkm_subdev *subdev = &clk->base.subdev;
  208. struct nvbios_pll limits;
  209. int P, N, M, diff;
  210. int ret;
  211. info->pll = 0;
  212. /* If we can get a within [-2, 3) MHz of a divider, we'll disable the
  213. * PLL and use the divider instead. */
  214. ret = gt215_clk_info(&clk->base, idx, khz, info);
  215. diff = khz - ret;
  216. if (!pll || (diff >= -2000 && diff < 3000)) {
  217. goto out;
  218. }
  219. /* Try with PLL */
  220. ret = nvbios_pll_parse(subdev->device->bios, pll, &limits);
  221. if (ret)
  222. return ret;
  223. ret = gt215_clk_info(&clk->base, idx - 0x10, limits.refclk, info);
  224. if (ret != limits.refclk)
  225. return -EINVAL;
  226. ret = gt215_pll_calc(subdev, &limits, khz, &N, NULL, &M, &P);
  227. if (ret >= 0) {
  228. info->pll = (P << 16) | (N << 8) | M;
  229. }
  230. out:
  231. info->fb_delay = max(((khz + 7566) / 15133), (u32) 18);
  232. return ret ? ret : -ERANGE;
  233. }
  234. static int
  235. calc_clk(struct gt215_clk *clk, struct nvkm_cstate *cstate,
  236. int idx, u32 pll, int dom)
  237. {
  238. int ret = gt215_pll_info(&clk->base, idx, pll, cstate->domain[dom],
  239. &clk->eng[dom]);
  240. if (ret >= 0)
  241. return 0;
  242. return ret;
  243. }
  244. static int
  245. calc_host(struct gt215_clk *clk, struct nvkm_cstate *cstate)
  246. {
  247. int ret = 0;
  248. u32 kHz = cstate->domain[nv_clk_src_host];
  249. struct gt215_clk_info *info = &clk->eng[nv_clk_src_host];
  250. if (kHz == 277000) {
  251. info->clk = 0;
  252. info->host_out = NVA3_HOST_277;
  253. return 0;
  254. }
  255. info->host_out = NVA3_HOST_CLK;
  256. ret = gt215_clk_info(&clk->base, 0x1d, kHz, info);
  257. if (ret >= 0)
  258. return 0;
  259. return ret;
  260. }
  261. int
  262. gt215_clk_pre(struct nvkm_clk *clk, unsigned long *flags)
  263. {
  264. struct nvkm_device *device = clk->subdev.device;
  265. struct nvkm_fifo *fifo = device->fifo;
  266. /* halt and idle execution engines */
  267. nvkm_mask(device, 0x020060, 0x00070000, 0x00000000);
  268. nvkm_mask(device, 0x002504, 0x00000001, 0x00000001);
  269. /* Wait until the interrupt handler is finished */
  270. if (nvkm_msec(device, 2000,
  271. if (!nvkm_rd32(device, 0x000100))
  272. break;
  273. ) < 0)
  274. return -EBUSY;
  275. if (fifo)
  276. nvkm_fifo_pause(fifo, flags);
  277. if (nvkm_msec(device, 2000,
  278. if (nvkm_rd32(device, 0x002504) & 0x00000010)
  279. break;
  280. ) < 0)
  281. return -EIO;
  282. if (nvkm_msec(device, 2000,
  283. u32 tmp = nvkm_rd32(device, 0x00251c) & 0x0000003f;
  284. if (tmp == 0x0000003f)
  285. break;
  286. ) < 0)
  287. return -EIO;
  288. return 0;
  289. }
  290. void
  291. gt215_clk_post(struct nvkm_clk *clk, unsigned long *flags)
  292. {
  293. struct nvkm_device *device = clk->subdev.device;
  294. struct nvkm_fifo *fifo = device->fifo;
  295. if (fifo && flags)
  296. nvkm_fifo_start(fifo, flags);
  297. nvkm_mask(device, 0x002504, 0x00000001, 0x00000000);
  298. nvkm_mask(device, 0x020060, 0x00070000, 0x00040000);
  299. }
  300. static void
  301. disable_clk_src(struct gt215_clk *clk, u32 src)
  302. {
  303. struct nvkm_device *device = clk->base.subdev.device;
  304. nvkm_mask(device, src, 0x00000100, 0x00000000);
  305. nvkm_mask(device, src, 0x00000001, 0x00000000);
  306. }
  307. static void
  308. prog_pll(struct gt215_clk *clk, int idx, u32 pll, int dom)
  309. {
  310. struct gt215_clk_info *info = &clk->eng[dom];
  311. struct nvkm_device *device = clk->base.subdev.device;
  312. const u32 src0 = 0x004120 + (idx * 4);
  313. const u32 src1 = 0x004160 + (idx * 4);
  314. const u32 ctrl = pll + 0;
  315. const u32 coef = pll + 4;
  316. u32 bypass;
  317. if (info->pll) {
  318. /* Always start from a non-PLL clock */
  319. bypass = nvkm_rd32(device, ctrl) & 0x00000008;
  320. if (!bypass) {
  321. nvkm_mask(device, src1, 0x00000101, 0x00000101);
  322. nvkm_mask(device, ctrl, 0x00000008, 0x00000008);
  323. udelay(20);
  324. }
  325. nvkm_mask(device, src0, 0x003f3141, 0x00000101 | info->clk);
  326. nvkm_wr32(device, coef, info->pll);
  327. nvkm_mask(device, ctrl, 0x00000015, 0x00000015);
  328. nvkm_mask(device, ctrl, 0x00000010, 0x00000000);
  329. if (nvkm_msec(device, 2000,
  330. if (nvkm_rd32(device, ctrl) & 0x00020000)
  331. break;
  332. ) < 0) {
  333. nvkm_mask(device, ctrl, 0x00000010, 0x00000010);
  334. nvkm_mask(device, src0, 0x00000101, 0x00000000);
  335. return;
  336. }
  337. nvkm_mask(device, ctrl, 0x00000010, 0x00000010);
  338. nvkm_mask(device, ctrl, 0x00000008, 0x00000000);
  339. disable_clk_src(clk, src1);
  340. } else {
  341. nvkm_mask(device, src1, 0x003f3141, 0x00000101 | info->clk);
  342. nvkm_mask(device, ctrl, 0x00000018, 0x00000018);
  343. udelay(20);
  344. nvkm_mask(device, ctrl, 0x00000001, 0x00000000);
  345. disable_clk_src(clk, src0);
  346. }
  347. }
  348. static void
  349. prog_clk(struct gt215_clk *clk, int idx, int dom)
  350. {
  351. struct gt215_clk_info *info = &clk->eng[dom];
  352. struct nvkm_device *device = clk->base.subdev.device;
  353. nvkm_mask(device, 0x004120 + (idx * 4), 0x003f3141, 0x00000101 | info->clk);
  354. }
  355. static void
  356. prog_host(struct gt215_clk *clk)
  357. {
  358. struct gt215_clk_info *info = &clk->eng[nv_clk_src_host];
  359. struct nvkm_device *device = clk->base.subdev.device;
  360. u32 hsrc = (nvkm_rd32(device, 0xc040));
  361. switch (info->host_out) {
  362. case NVA3_HOST_277:
  363. if ((hsrc & 0x30000000) == 0) {
  364. nvkm_wr32(device, 0xc040, hsrc | 0x20000000);
  365. disable_clk_src(clk, 0x4194);
  366. }
  367. break;
  368. case NVA3_HOST_CLK:
  369. prog_clk(clk, 0x1d, nv_clk_src_host);
  370. if ((hsrc & 0x30000000) >= 0x20000000) {
  371. nvkm_wr32(device, 0xc040, hsrc & ~0x30000000);
  372. }
  373. break;
  374. default:
  375. break;
  376. }
  377. /* This seems to be a clock gating factor on idle, always set to 64 */
  378. nvkm_wr32(device, 0xc044, 0x3e);
  379. }
  380. static void
  381. prog_core(struct gt215_clk *clk, int dom)
  382. {
  383. struct gt215_clk_info *info = &clk->eng[dom];
  384. struct nvkm_device *device = clk->base.subdev.device;
  385. u32 fb_delay = nvkm_rd32(device, 0x10002c);
  386. if (fb_delay < info->fb_delay)
  387. nvkm_wr32(device, 0x10002c, info->fb_delay);
  388. prog_pll(clk, 0x00, 0x004200, dom);
  389. if (fb_delay > info->fb_delay)
  390. nvkm_wr32(device, 0x10002c, info->fb_delay);
  391. }
  392. static int
  393. gt215_clk_calc(struct nvkm_clk *base, struct nvkm_cstate *cstate)
  394. {
  395. struct gt215_clk *clk = gt215_clk(base);
  396. struct gt215_clk_info *core = &clk->eng[nv_clk_src_core];
  397. int ret;
  398. if ((ret = calc_clk(clk, cstate, 0x10, 0x4200, nv_clk_src_core)) ||
  399. (ret = calc_clk(clk, cstate, 0x11, 0x4220, nv_clk_src_shader)) ||
  400. (ret = calc_clk(clk, cstate, 0x20, 0x0000, nv_clk_src_disp)) ||
  401. (ret = calc_clk(clk, cstate, 0x21, 0x0000, nv_clk_src_vdec)) ||
  402. (ret = calc_host(clk, cstate)))
  403. return ret;
  404. /* XXX: Should be reading the highest bit in the VBIOS clock to decide
  405. * whether to use a PLL or not... but using a PLL defeats the purpose */
  406. if (core->pll) {
  407. ret = gt215_clk_info(&clk->base, 0x10,
  408. cstate->domain[nv_clk_src_core_intm],
  409. &clk->eng[nv_clk_src_core_intm]);
  410. if (ret < 0)
  411. return ret;
  412. }
  413. return 0;
  414. }
  415. static int
  416. gt215_clk_prog(struct nvkm_clk *base)
  417. {
  418. struct gt215_clk *clk = gt215_clk(base);
  419. struct gt215_clk_info *core = &clk->eng[nv_clk_src_core];
  420. int ret = 0;
  421. unsigned long flags;
  422. unsigned long *f = &flags;
  423. ret = gt215_clk_pre(&clk->base, f);
  424. if (ret)
  425. goto out;
  426. if (core->pll)
  427. prog_core(clk, nv_clk_src_core_intm);
  428. prog_core(clk, nv_clk_src_core);
  429. prog_pll(clk, 0x01, 0x004220, nv_clk_src_shader);
  430. prog_clk(clk, 0x20, nv_clk_src_disp);
  431. prog_clk(clk, 0x21, nv_clk_src_vdec);
  432. prog_host(clk);
  433. out:
  434. if (ret == -EBUSY)
  435. f = NULL;
  436. gt215_clk_post(&clk->base, f);
  437. return ret;
  438. }
  439. static void
  440. gt215_clk_tidy(struct nvkm_clk *base)
  441. {
  442. }
  443. static const struct nvkm_clk_func
  444. gt215_clk = {
  445. .read = gt215_clk_read,
  446. .calc = gt215_clk_calc,
  447. .prog = gt215_clk_prog,
  448. .tidy = gt215_clk_tidy,
  449. .domains = {
  450. { nv_clk_src_crystal , 0xff },
  451. { nv_clk_src_core , 0x00, 0, "core", 1000 },
  452. { nv_clk_src_shader , 0x01, 0, "shader", 1000 },
  453. { nv_clk_src_mem , 0x02, 0, "memory", 1000 },
  454. { nv_clk_src_vdec , 0x03 },
  455. { nv_clk_src_disp , 0x04 },
  456. { nv_clk_src_host , 0x05 },
  457. { nv_clk_src_core_intm, 0x06 },
  458. { nv_clk_src_max }
  459. }
  460. };
  461. int
  462. gt215_clk_new(struct nvkm_device *device, int index, struct nvkm_clk **pclk)
  463. {
  464. struct gt215_clk *clk;
  465. if (!(clk = kzalloc(sizeof(*clk), GFP_KERNEL)))
  466. return -ENOMEM;
  467. *pclk = &clk->base;
  468. return nvkm_clk_ctor(&gt215_clk, device, index, true, &clk->base);
  469. }