Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
radeon_clocks.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Jerome Glisse.
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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Dave Airlie
25  * Alex Deucher
26  * Jerome Glisse
27  */
28 #include <drm/drmP.h>
29 #include <drm/radeon_drm.h>
30 #include "radeon_reg.h"
31 #include "radeon.h"
32 #include "atom.h"
33 
34 /* 10 khz */
36 {
37  struct radeon_pll *spll = &rdev->clock.spll;
38  uint32_t fb_div, ref_div, post_div, sclk;
39 
42  fb_div <<= 1;
43  fb_div *= spll->reference_freq;
44 
45  ref_div =
47 
48  if (ref_div == 0)
49  return 0;
50 
51  sclk = fb_div / ref_div;
52 
54  if (post_div == 2)
55  sclk >>= 1;
56  else if (post_div == 3)
57  sclk >>= 2;
58  else if (post_div == 4)
59  sclk >>= 3;
60 
61  return sclk;
62 }
63 
64 /* 10 khz */
66 {
67  struct radeon_pll *mpll = &rdev->clock.mpll;
68  uint32_t fb_div, ref_div, post_div, mclk;
69 
72  fb_div <<= 1;
73  fb_div *= mpll->reference_freq;
74 
75  ref_div =
77 
78  if (ref_div == 0)
79  return 0;
80 
81  mclk = fb_div / ref_div;
82 
83  post_div = RREG32_PLL(RADEON_MCLK_CNTL) & 0x7;
84  if (post_div == 2)
85  mclk >>= 1;
86  else if (post_div == 3)
87  mclk >>= 2;
88  else if (post_div == 4)
89  mclk >>= 3;
90 
91  return mclk;
92 }
93 
94 #ifdef CONFIG_OF
95 /*
96  * Read XTAL (ref clock), SCLK and MCLK from Open Firmware device
97  * tree. Hopefully, ATI OF driver is kind enough to fill these
98  */
99 static bool radeon_read_clocks_OF(struct drm_device *dev)
100 {
101  struct radeon_device *rdev = dev->dev_private;
102  struct device_node *dp = rdev->pdev->dev.of_node;
103  const u32 *val;
104  struct radeon_pll *p1pll = &rdev->clock.p1pll;
105  struct radeon_pll *p2pll = &rdev->clock.p2pll;
106  struct radeon_pll *spll = &rdev->clock.spll;
107  struct radeon_pll *mpll = &rdev->clock.mpll;
108 
109  if (dp == NULL)
110  return false;
111  val = of_get_property(dp, "ATY,RefCLK", NULL);
112  if (!val || !*val) {
113  printk(KERN_WARNING "radeonfb: No ATY,RefCLK property !\n");
114  return false;
115  }
116  p1pll->reference_freq = p2pll->reference_freq = (*val) / 10;
117  p1pll->reference_div = RREG32_PLL(RADEON_PPLL_REF_DIV) & 0x3ff;
118  if (p1pll->reference_div < 2)
119  p1pll->reference_div = 12;
120  p2pll->reference_div = p1pll->reference_div;
121 
122  /* These aren't in the device-tree */
123  if (rdev->family >= CHIP_R420) {
124  p1pll->pll_in_min = 100;
125  p1pll->pll_in_max = 1350;
126  p1pll->pll_out_min = 20000;
127  p1pll->pll_out_max = 50000;
128  p2pll->pll_in_min = 100;
129  p2pll->pll_in_max = 1350;
130  p2pll->pll_out_min = 20000;
131  p2pll->pll_out_max = 50000;
132  } else {
133  p1pll->pll_in_min = 40;
134  p1pll->pll_in_max = 500;
135  p1pll->pll_out_min = 12500;
136  p1pll->pll_out_max = 35000;
137  p2pll->pll_in_min = 40;
138  p2pll->pll_in_max = 500;
139  p2pll->pll_out_min = 12500;
140  p2pll->pll_out_max = 35000;
141  }
142  /* not sure what the max should be in all cases */
143  rdev->clock.max_pixel_clock = 35000;
144 
145  spll->reference_freq = mpll->reference_freq = p1pll->reference_freq;
146  spll->reference_div = mpll->reference_div =
149 
150  val = of_get_property(dp, "ATY,SCLK", NULL);
151  if (val && *val)
152  rdev->clock.default_sclk = (*val) / 10;
153  else
154  rdev->clock.default_sclk =
156 
157  val = of_get_property(dp, "ATY,MCLK", NULL);
158  if (val && *val)
159  rdev->clock.default_mclk = (*val) / 10;
160  else
161  rdev->clock.default_mclk =
163 
164  DRM_INFO("Using device-tree clock info\n");
165 
166  return true;
167 }
168 #else
169 static bool radeon_read_clocks_OF(struct drm_device *dev)
170 {
171  return false;
172 }
173 #endif /* CONFIG_OF */
174 
176 {
177  struct radeon_device *rdev = dev->dev_private;
178  struct radeon_pll *p1pll = &rdev->clock.p1pll;
179  struct radeon_pll *p2pll = &rdev->clock.p2pll;
180  struct radeon_pll *dcpll = &rdev->clock.dcpll;
181  struct radeon_pll *spll = &rdev->clock.spll;
182  struct radeon_pll *mpll = &rdev->clock.mpll;
183  int ret;
184 
185  if (rdev->is_atom_bios)
186  ret = radeon_atom_get_clock_info(dev);
187  else
189  if (!ret)
190  ret = radeon_read_clocks_OF(dev);
191 
192  if (ret) {
193  if (p1pll->reference_div < 2) {
194  if (!ASIC_IS_AVIVO(rdev)) {
196  if (ASIC_IS_R300(rdev))
197  p1pll->reference_div =
199  else
201  if (p1pll->reference_div < 2)
202  p1pll->reference_div = 12;
203  } else
204  p1pll->reference_div = 12;
205  }
206  if (p2pll->reference_div < 2)
207  p2pll->reference_div = 12;
208  if (rdev->family < CHIP_RS600) {
209  if (spll->reference_div < 2)
210  spll->reference_div =
213  }
214  if (mpll->reference_div < 2)
215  mpll->reference_div = spll->reference_div;
216  } else {
217  if (ASIC_IS_AVIVO(rdev)) {
218  /* TODO FALLBACK */
219  } else {
220  DRM_INFO("Using generic clock info\n");
221 
222  /* may need to be per card */
223  rdev->clock.max_pixel_clock = 35000;
224 
225  if (rdev->flags & RADEON_IS_IGP) {
226  p1pll->reference_freq = 1432;
227  p2pll->reference_freq = 1432;
228  spll->reference_freq = 1432;
229  mpll->reference_freq = 1432;
230  } else {
231  p1pll->reference_freq = 2700;
232  p2pll->reference_freq = 2700;
233  spll->reference_freq = 2700;
234  mpll->reference_freq = 2700;
235  }
236  p1pll->reference_div =
238  if (p1pll->reference_div < 2)
239  p1pll->reference_div = 12;
240  p2pll->reference_div = p1pll->reference_div;
241 
242  if (rdev->family >= CHIP_R420) {
243  p1pll->pll_in_min = 100;
244  p1pll->pll_in_max = 1350;
245  p1pll->pll_out_min = 20000;
246  p1pll->pll_out_max = 50000;
247  p2pll->pll_in_min = 100;
248  p2pll->pll_in_max = 1350;
249  p2pll->pll_out_min = 20000;
250  p2pll->pll_out_max = 50000;
251  } else {
252  p1pll->pll_in_min = 40;
253  p1pll->pll_in_max = 500;
254  p1pll->pll_out_min = 12500;
255  p1pll->pll_out_max = 35000;
256  p2pll->pll_in_min = 40;
257  p2pll->pll_in_max = 500;
258  p2pll->pll_out_min = 12500;
259  p2pll->pll_out_max = 35000;
260  }
261 
262  spll->reference_div =
265  mpll->reference_div = spll->reference_div;
266  rdev->clock.default_sclk =
268  rdev->clock.default_mclk =
270  }
271  }
272 
273  /* pixel clocks */
274  if (ASIC_IS_AVIVO(rdev)) {
275  p1pll->min_post_div = 2;
276  p1pll->max_post_div = 0x7f;
277  p1pll->min_frac_feedback_div = 0;
278  p1pll->max_frac_feedback_div = 9;
279  p2pll->min_post_div = 2;
280  p2pll->max_post_div = 0x7f;
281  p2pll->min_frac_feedback_div = 0;
282  p2pll->max_frac_feedback_div = 9;
283  } else {
284  p1pll->min_post_div = 1;
285  p1pll->max_post_div = 16;
286  p1pll->min_frac_feedback_div = 0;
287  p1pll->max_frac_feedback_div = 0;
288  p2pll->min_post_div = 1;
289  p2pll->max_post_div = 12;
290  p2pll->min_frac_feedback_div = 0;
291  p2pll->max_frac_feedback_div = 0;
292  }
293 
294  /* dcpll is DCE4 only */
295  dcpll->min_post_div = 2;
296  dcpll->max_post_div = 0x7f;
297  dcpll->min_frac_feedback_div = 0;
298  dcpll->max_frac_feedback_div = 9;
299  dcpll->min_ref_div = 2;
300  dcpll->max_ref_div = 0x3ff;
301  dcpll->min_feedback_div = 4;
302  dcpll->max_feedback_div = 0xfff;
303  dcpll->best_vco = 0;
304 
305  p1pll->min_ref_div = 2;
306  p1pll->max_ref_div = 0x3ff;
307  p1pll->min_feedback_div = 4;
308  p1pll->max_feedback_div = 0x7ff;
309  p1pll->best_vco = 0;
310 
311  p2pll->min_ref_div = 2;
312  p2pll->max_ref_div = 0x3ff;
313  p2pll->min_feedback_div = 4;
314  p2pll->max_feedback_div = 0x7ff;
315  p2pll->best_vco = 0;
316 
317  /* system clock */
318  spll->min_post_div = 1;
319  spll->max_post_div = 1;
320  spll->min_ref_div = 2;
321  spll->max_ref_div = 0xff;
322  spll->min_feedback_div = 4;
323  spll->max_feedback_div = 0xff;
324  spll->best_vco = 0;
325 
326  /* memory clock */
327  mpll->min_post_div = 1;
328  mpll->max_post_div = 1;
329  mpll->min_ref_div = 2;
330  mpll->max_ref_div = 0xff;
331  mpll->min_feedback_div = 4;
332  mpll->max_feedback_div = 0xff;
333  mpll->best_vco = 0;
334 
335  if (!rdev->clock.default_sclk)
336  rdev->clock.default_sclk = radeon_get_engine_clock(rdev);
337  if ((!rdev->clock.default_mclk) && rdev->asic->pm.get_memory_clock)
338  rdev->clock.default_mclk = radeon_get_memory_clock(rdev);
339 
340  rdev->pm.current_sclk = rdev->clock.default_sclk;
341  rdev->pm.current_mclk = rdev->clock.default_mclk;
342 
343 }
344 
345 /* 10 khz */
346 static uint32_t calc_eng_mem_clock(struct radeon_device *rdev,
347  uint32_t req_clock,
348  int *fb_div, int *post_div)
349 {
350  struct radeon_pll *spll = &rdev->clock.spll;
351  int ref_div = spll->reference_div;
352 
353  if (!ref_div)
354  ref_div =
357 
358  if (req_clock < 15000) {
359  *post_div = 8;
360  req_clock *= 8;
361  } else if (req_clock < 30000) {
362  *post_div = 4;
363  req_clock *= 4;
364  } else if (req_clock < 60000) {
365  *post_div = 2;
366  req_clock *= 2;
367  } else
368  *post_div = 1;
369 
370  req_clock *= ref_div;
371  req_clock += spll->reference_freq;
372  req_clock /= (2 * spll->reference_freq);
373 
374  *fb_div = req_clock & 0xff;
375 
376  req_clock = (req_clock & 0xffff) << 1;
377  req_clock *= spll->reference_freq;
378  req_clock /= ref_div;
379  req_clock /= *post_div;
380 
381  return req_clock;
382 }
383 
384 /* 10 khz */
386  uint32_t eng_clock)
387 {
388  uint32_t tmp;
389  int fb_div, post_div;
390 
391  /* XXX: wait for idle */
392 
393  eng_clock = calc_eng_mem_clock(rdev, eng_clock, &fb_div, &post_div);
394 
396  tmp &= ~RADEON_DONT_USE_XTALIN;
398 
400  tmp &= ~RADEON_SCLK_SRC_SEL_MASK;
402 
403  udelay(10);
404 
406  tmp |= RADEON_SPLL_SLEEP;
408 
409  udelay(2);
410 
412  tmp |= RADEON_SPLL_RESET;
414 
415  udelay(200);
416 
421 
422  /* XXX: verify on different asics */
424  tmp &= ~RADEON_SPLL_PVG_MASK;
425  if ((eng_clock * post_div) >= 90000)
426  tmp |= (0x7 << RADEON_SPLL_PVG_SHIFT);
427  else
428  tmp |= (0x4 << RADEON_SPLL_PVG_SHIFT);
430 
432  tmp &= ~RADEON_SPLL_SLEEP;
434 
435  udelay(2);
436 
438  tmp &= ~RADEON_SPLL_RESET;
440 
441  udelay(200);
442 
444  tmp &= ~RADEON_SCLK_SRC_SEL_MASK;
445  switch (post_div) {
446  case 1:
447  default:
448  tmp |= 1;
449  break;
450  case 2:
451  tmp |= 2;
452  break;
453  case 4:
454  tmp |= 3;
455  break;
456  case 8:
457  tmp |= 4;
458  break;
459  }
461 
462  udelay(20);
463 
465  tmp |= RADEON_DONT_USE_XTALIN;
467 
468  udelay(10);
469 }
470 
472 {
473  uint32_t tmp;
474 
475  if (enable) {
476  if (rdev->flags & RADEON_SINGLE_CRTC) {
478  if ((RREG32(RADEON_CONFIG_CNTL) &
481  tmp &=
484  }
485  tmp &=
492  } else if (ASIC_IS_R300(rdev)) {
493  if ((rdev->family == CHIP_RS400) ||
494  (rdev->family == CHIP_RS480)) {
496  tmp &=
511  tmp |=
515 
517  tmp &= ~RADEON_SCLK_MORE_FORCEON;
520 
522  tmp |= (RADEON_PIXCLK_ALWAYS_ONb |
525 
527  tmp |= (RADEON_PIX2CLK_ALWAYS_ONb |
541  } else if (rdev->family >= CHIP_RV350) {
543  tmp &= ~(R300_SCLK_FORCE_TCL |
550 
552  tmp &=
568 
570  tmp &= ~RADEON_SCLK_MORE_FORCEON;
573 
575  tmp |= (RADEON_PIXCLK_ALWAYS_ONb |
578 
580  tmp |= (RADEON_PIX2CLK_ALWAYS_ONb |
594 
596  tmp |= (RADEON_MC_MCLK_DYN_ENABLE |
599 
601  tmp |= (RADEON_FORCEON_MCLKA |
603 
604  tmp &= ~(RADEON_FORCEON_YCLKA |
607 
608  /* Some releases of vbios have set DISABLE_MC_MCLKA
609  and DISABLE_MC_MCLKB bits in the vbios table. Setting these
610  bits will cause H/W hang when reading video memory with dynamic clocking
611  enabled. */
612  if ((tmp & R300_DISABLE_MC_MCLKA) &&
613  (tmp & R300_DISABLE_MC_MCLKB)) {
614  /* If both bits are set, then check the active channels */
616  if (rdev->mc.vram_width == 64) {
617  if (RREG32(RADEON_MEM_CNTL) &
619  tmp &=
621  else
622  tmp &=
623  ~R300_DISABLE_MC_MCLKA;
624  } else {
625  tmp &= ~(R300_DISABLE_MC_MCLKA |
627  }
628  }
629 
631  } else {
633  tmp &= ~(R300_SCLK_FORCE_VAP);
634  tmp |= RADEON_SCLK_FORCE_CP;
636  mdelay(15);
637 
639  tmp &= ~(R300_SCLK_FORCE_TCL |
643  }
644  } else {
646 
647  tmp &= ~(RADEON_ACTIVE_HILO_LAT_MASK |
650 
651  tmp |= (RADEON_ENGIN_DYNCLK_MODE |
652  (0x01 << RADEON_ACTIVE_HILO_LAT_SHIFT));
654  mdelay(15);
655 
659  mdelay(15);
660 
661  /* When DRI is enabled, setting DYN_STOP_LAT to zero can cause some R200
662  to lockup randomly, leave them as set by BIOS.
663  */
665  /*tmp &= RADEON_SCLK_SRC_SEL_MASK; */
666  tmp &= ~RADEON_SCLK_FORCEON_MASK;
667 
668  /*RAGE_6::A11 A12 A12N1 A13, RV250::A11 A12, R300 */
669  if (((rdev->family == CHIP_RV250) &&
673  || ((rdev->family == CHIP_RV100)
674  &&
676  RADEON_CFG_ATI_REV_ID_MASK) <=
678  tmp |= RADEON_SCLK_FORCE_CP;
679  tmp |= RADEON_SCLK_FORCE_VIP;
680  }
681 
683 
684  if ((rdev->family == CHIP_RV200) ||
685  (rdev->family == CHIP_RV250) ||
686  (rdev->family == CHIP_RV280)) {
688  tmp &= ~RADEON_SCLK_MORE_FORCEON;
689 
690  /* RV200::A11 A12 RV250::A11 A12 */
691  if (((rdev->family == CHIP_RV200) ||
692  (rdev->family == CHIP_RV250)) &&
694  RADEON_CFG_ATI_REV_ID_MASK) <
697  }
699  mdelay(15);
700  }
701 
702  /* RV200::A11 A12, RV250::A11 A12 */
703  if (((rdev->family == CHIP_RV200) ||
704  (rdev->family == CHIP_RV250)) &&
706  RADEON_CFG_ATI_REV_ID_MASK) <
711  }
712  mdelay(15);
713 
714  /*enable dynamic mode for display clocks (PIXCLK and PIX2CLK) */
716  tmp |= (RADEON_PIX2CLK_ALWAYS_ONb |
723 
725  mdelay(15);
726 
728  tmp |= (RADEON_PIXCLK_ALWAYS_ONb |
730 
732  mdelay(15);
733  }
734  } else {
735  /* Turn everything OFF (ForceON to everything) */
736  if (rdev->flags & RADEON_SINGLE_CRTC) {
746  } else if ((rdev->family == CHIP_RS400) ||
747  (rdev->family == CHIP_RS480)) {
758 
762 
764  tmp &= ~(RADEON_PIXCLK_ALWAYS_ONb |
768 
770  tmp &= ~(RADEON_PIX2CLK_ALWAYS_ONb |
785  } else if (rdev->family >= CHIP_RV350) {
786  /* for RV350/M10, no delays are required. */
788  tmp |= (R300_SCLK_FORCE_TCL |
791 
802 
806 
808  tmp |= (RADEON_FORCEON_MCLKA |
813 
815  tmp &= ~(RADEON_PIXCLK_ALWAYS_ONb |
819 
821  tmp &= ~(RADEON_PIX2CLK_ALWAYS_ONb |
836  } else {
839  tmp |= RADEON_SCLK_FORCE_SE;
840 
841  if (rdev->flags & RADEON_SINGLE_CRTC) {
842  tmp |= (RADEON_SCLK_FORCE_RB |
853  } else if ((rdev->family == CHIP_R300) ||
854  (rdev->family == CHIP_R350)) {
855  tmp |= (RADEON_SCLK_FORCE_HDP |
861  }
863 
864  mdelay(16);
865 
866  if ((rdev->family == CHIP_R300) ||
867  (rdev->family == CHIP_R350)) {
869  tmp |= (R300_SCLK_FORCE_TCL |
873  mdelay(16);
874  }
875 
876  if (rdev->flags & RADEON_IS_IGP) {
878  tmp &= ~(RADEON_FORCEON_MCLKA |
881  mdelay(16);
882  }
883 
884  if ((rdev->family == CHIP_RV200) ||
885  (rdev->family == CHIP_RV250) ||
886  (rdev->family == CHIP_RV280)) {
890  mdelay(16);
891  }
892 
894  tmp &= ~(RADEON_PIX2CLK_ALWAYS_ONb |
901 
903  mdelay(16);
904 
906  tmp &= ~(RADEON_PIXCLK_ALWAYS_ONb |
909  }
910  }
911 }
912