gma500: Update the Cedarview clock handling
[platform/kernel/linux-starfive.git] / drivers / gpu / drm / gma500 / cdv_intel_display.c
1 /*
2  * Copyright © 2006-2011 Intel Corporation
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Authors:
18  *      Eric Anholt <eric@anholt.net>
19  */
20
21 #include <linux/i2c.h>
22 #include <linux/pm_runtime.h>
23
24 #include <drm/drmP.h>
25 #include "framebuffer.h"
26 #include "psb_drv.h"
27 #include "psb_intel_drv.h"
28 #include "psb_intel_reg.h"
29 #include "psb_intel_display.h"
30 #include "power.h"
31 #include "cdv_device.h"
32
33
34 struct cdv_intel_range_t {
35         int min, max;
36 };
37
38 struct cdv_intel_p2_t {
39         int dot_limit;
40         int p2_slow, p2_fast;
41 };
42
43 struct cdv_intel_clock_t {
44         /* given values */
45         int n;
46         int m1, m2;
47         int p1, p2;
48         /* derived values */
49         int dot;
50         int vco;
51         int m;
52         int p;
53 };
54
55 #define INTEL_P2_NUM                  2
56
57 struct cdv_intel_limit_t {
58         struct cdv_intel_range_t dot, vco, n, m, m1, m2, p, p1;
59         struct cdv_intel_p2_t p2;
60 };
61
62 #define CDV_LIMIT_SINGLE_LVDS_96        0
63 #define CDV_LIMIT_SINGLE_LVDS_100       1
64 #define CDV_LIMIT_DAC_HDMI_27           2
65 #define CDV_LIMIT_DAC_HDMI_96           3
66
67 static const struct cdv_intel_limit_t cdv_intel_limits[] = {
68         {                       /* CDV_SIGNLE_LVDS_96MHz */
69          .dot = {.min = 20000, .max = 115500},
70          .vco = {.min = 1800000, .max = 3600000},
71          .n = {.min = 2, .max = 6},
72          .m = {.min = 60, .max = 160},
73          .m1 = {.min = 0, .max = 0},
74          .m2 = {.min = 58, .max = 158},
75          .p = {.min = 28, .max = 140},
76          .p1 = {.min = 2, .max = 10},
77          .p2 = {.dot_limit = 200000,
78                 .p2_slow = 14, .p2_fast = 14},
79          },
80         {                       /* CDV_SINGLE_LVDS_100MHz */
81          .dot = {.min = 20000, .max = 115500},
82          .vco = {.min = 1800000, .max = 3600000},
83          .n = {.min = 2, .max = 6},
84          .m = {.min = 60, .max = 160},
85          .m1 = {.min = 0, .max = 0},
86          .m2 = {.min = 58, .max = 158},
87          .p = {.min = 28, .max = 140},
88          .p1 = {.min = 2, .max = 10},
89          /* The single-channel range is 25-112Mhz, and dual-channel
90           * is 80-224Mhz.  Prefer single channel as much as possible.
91           */
92          .p2 = {.dot_limit = 200000, .p2_slow = 14, .p2_fast = 14},
93          },
94         {                       /* CDV_DAC_HDMI_27MHz */
95          .dot = {.min = 20000, .max = 400000},
96          .vco = {.min = 1809000, .max = 3564000},
97          .n = {.min = 1, .max = 1},
98          .m = {.min = 67, .max = 132},
99          .m1 = {.min = 0, .max = 0},
100          .m2 = {.min = 65, .max = 130},
101          .p = {.min = 5, .max = 90},
102          .p1 = {.min = 1, .max = 9},
103          .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 5},
104          },
105         {                       /* CDV_DAC_HDMI_96MHz */
106          .dot = {.min = 20000, .max = 400000},
107          .vco = {.min = 1800000, .max = 3600000},
108          .n = {.min = 2, .max = 6},
109          .m = {.min = 60, .max = 160},
110          .m1 = {.min = 0, .max = 0},
111          .m2 = {.min = 58, .max = 158},
112          .p = {.min = 5, .max = 100},
113          .p1 = {.min = 1, .max = 10},
114          .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 5},
115          },
116 };
117
118 #define _wait_for(COND, MS, W) ({ \
119         unsigned long timeout__ = jiffies + msecs_to_jiffies(MS);       \
120         int ret__ = 0;                                                  \
121         while (!(COND)) {                                               \
122                 if (time_after(jiffies, timeout__)) {                   \
123                         ret__ = -ETIMEDOUT;                             \
124                         break;                                          \
125                 }                                                       \
126                 if (W && !in_dbg_master())                              \
127                         msleep(W);                                      \
128         }                                                               \
129         ret__;                                                          \
130 })
131
132 #define wait_for(COND, MS) _wait_for(COND, MS, 1)
133
134
135 static int cdv_sb_read(struct drm_device *dev, u32 reg, u32 *val)
136 {
137         int ret;
138
139         ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
140         if (ret) {
141                 DRM_ERROR("timeout waiting for SB to idle before read\n");
142                 return ret;
143         }
144
145         REG_WRITE(SB_ADDR, reg);
146         REG_WRITE(SB_PCKT,
147                    SET_FIELD(SB_OPCODE_READ, SB_OPCODE) |
148                    SET_FIELD(SB_DEST_DPLL, SB_DEST) |
149                    SET_FIELD(0xf, SB_BYTE_ENABLE));
150
151         ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
152         if (ret) {
153                 DRM_ERROR("timeout waiting for SB to idle after read\n");
154                 return ret;
155         }
156
157         *val = REG_READ(SB_DATA);
158
159         return 0;
160 }
161
162 static int cdv_sb_write(struct drm_device *dev, u32 reg, u32 val)
163 {
164         int ret;
165         static bool dpio_debug = true;
166         u32 temp;
167
168         if (dpio_debug) {
169                 if (cdv_sb_read(dev, reg, &temp) == 0)
170                         DRM_DEBUG_KMS("0x%08x: 0x%08x (before)\n", reg, temp);
171                 DRM_DEBUG_KMS("0x%08x: 0x%08x\n", reg, val);
172         }
173
174         ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
175         if (ret) {
176                 DRM_ERROR("timeout waiting for SB to idle before write\n");
177                 return ret;
178         }
179
180         REG_WRITE(SB_ADDR, reg);
181         REG_WRITE(SB_DATA, val);
182         REG_WRITE(SB_PCKT,
183                    SET_FIELD(SB_OPCODE_WRITE, SB_OPCODE) |
184                    SET_FIELD(SB_DEST_DPLL, SB_DEST) |
185                    SET_FIELD(0xf, SB_BYTE_ENABLE));
186
187         ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
188         if (ret) {
189                 DRM_ERROR("timeout waiting for SB to idle after write\n");
190                 return ret;
191         }
192
193         if (dpio_debug) {
194                 if (cdv_sb_read(dev, reg, &temp) == 0)
195                         DRM_DEBUG_KMS("0x%08x: 0x%08x (after)\n", reg, temp);
196         }
197
198         return 0;
199 }
200
201 /* Reset the DPIO configuration register.  The BIOS does this at every
202  * mode set.
203  */
204 static void cdv_sb_reset(struct drm_device *dev)
205 {
206
207         REG_WRITE(DPIO_CFG, 0);
208         REG_READ(DPIO_CFG);
209         REG_WRITE(DPIO_CFG, DPIO_MODE_SELECT_0 | DPIO_CMN_RESET_N);
210 }
211
212 /* Unlike most Intel display engines, on Cedarview the DPLL registers
213  * are behind this sideband bus.  They must be programmed while the
214  * DPLL reference clock is on in the DPLL control register, but before
215  * the DPLL is enabled in the DPLL control register.
216  */
217 static int
218 cdv_dpll_set_clock_cdv(struct drm_device *dev, struct drm_crtc *crtc,
219                                struct cdv_intel_clock_t *clock, bool is_lvds)
220 {
221         struct psb_intel_crtc *psb_crtc =
222                                 to_psb_intel_crtc(crtc);
223         int pipe = psb_crtc->pipe;
224         u32 m, n_vco, p;
225         int ret = 0;
226         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
227         int ref_sfr = (pipe == 0) ? SB_REF_DPLLA : SB_REF_DPLLB;
228         u32 ref_value;
229
230         cdv_sb_reset(dev);
231
232         if ((REG_READ(dpll_reg) & DPLL_SYNCLOCK_ENABLE) == 0) {
233                 DRM_ERROR("Attempting to set DPLL with refclk disabled\n");
234                 return -EBUSY;
235         }
236
237         /* Follow the BIOS and write the REF/SFR Register. Hardcoded value */
238         ref_value = 0x68A701;
239
240         cdv_sb_write(dev, SB_REF_SFR(pipe), ref_value);
241
242         /* We don't know what the other fields of these regs are, so
243          * leave them in place.
244          */
245         /* 
246          * The BIT 14:13 of 0x8010/0x8030 is used to select the ref clk
247          * for the pipe A/B. Display spec 1.06 has wrong definition.
248          * Correct definition is like below:
249          *
250          * refclka mean use clock from same PLL
251          *
252          * if DPLLA sets 01 and DPLLB sets 01, they use clock from their pll
253          *
254          * if DPLLA sets 01 and DPLLB sets 02, both use clk from DPLLA
255          *
256          */  
257         ret = cdv_sb_read(dev, ref_sfr, &ref_value);
258         if (ret)
259                 return ret;
260         ref_value &= ~(REF_CLK_MASK);
261
262         /* use DPLL_A for pipeB on CRT/HDMI */
263         if (pipe == 1 && !is_lvds) {
264                 DRM_DEBUG_KMS("use DPLLA for pipe B\n");
265                 ref_value |= REF_CLK_DPLLA;
266         } else {
267                 DRM_DEBUG_KMS("use their DPLL for pipe A/B\n");
268                 ref_value |= REF_CLK_DPLL;
269         }
270         ret = cdv_sb_write(dev, ref_sfr, ref_value);
271         if (ret)
272                 return ret;
273
274         ret = cdv_sb_read(dev, SB_M(pipe), &m);
275         if (ret)
276                 return ret;
277         m &= ~SB_M_DIVIDER_MASK;
278         m |= ((clock->m2) << SB_M_DIVIDER_SHIFT);
279         ret = cdv_sb_write(dev, SB_M(pipe), m);
280         if (ret)
281                 return ret;
282
283         ret = cdv_sb_read(dev, SB_N_VCO(pipe), &n_vco);
284         if (ret)
285                 return ret;
286
287         /* Follow the BIOS to program the N_DIVIDER REG */
288         n_vco &= 0xFFFF;
289         n_vco |= 0x107;
290         n_vco &= ~(SB_N_VCO_SEL_MASK |
291                    SB_N_DIVIDER_MASK |
292                    SB_N_CB_TUNE_MASK);
293
294         n_vco |= ((clock->n) << SB_N_DIVIDER_SHIFT);
295
296         if (clock->vco < 2250000) {
297                 n_vco |= (2 << SB_N_CB_TUNE_SHIFT);
298                 n_vco |= (0 << SB_N_VCO_SEL_SHIFT);
299         } else if (clock->vco < 2750000) {
300                 n_vco |= (1 << SB_N_CB_TUNE_SHIFT);
301                 n_vco |= (1 << SB_N_VCO_SEL_SHIFT);
302         } else if (clock->vco < 3300000) {
303                 n_vco |= (0 << SB_N_CB_TUNE_SHIFT);
304                 n_vco |= (2 << SB_N_VCO_SEL_SHIFT);
305         } else {
306                 n_vco |= (0 << SB_N_CB_TUNE_SHIFT);
307                 n_vco |= (3 << SB_N_VCO_SEL_SHIFT);
308         }
309
310         ret = cdv_sb_write(dev, SB_N_VCO(pipe), n_vco);
311         if (ret)
312                 return ret;
313
314         ret = cdv_sb_read(dev, SB_P(pipe), &p);
315         if (ret)
316                 return ret;
317         p &= ~(SB_P2_DIVIDER_MASK | SB_P1_DIVIDER_MASK);
318         p |= SET_FIELD(clock->p1, SB_P1_DIVIDER);
319         switch (clock->p2) {
320         case 5:
321                 p |= SET_FIELD(SB_P2_5, SB_P2_DIVIDER);
322                 break;
323         case 10:
324                 p |= SET_FIELD(SB_P2_10, SB_P2_DIVIDER);
325                 break;
326         case 14:
327                 p |= SET_FIELD(SB_P2_14, SB_P2_DIVIDER);
328                 break;
329         case 7:
330                 p |= SET_FIELD(SB_P2_7, SB_P2_DIVIDER);
331                 break;
332         default:
333                 DRM_ERROR("Bad P2 clock: %d\n", clock->p2);
334                 return -EINVAL;
335         }
336         ret = cdv_sb_write(dev, SB_P(pipe), p);
337         if (ret)
338                 return ret;
339
340         /* always Program the Lane Register for the Pipe A*/
341 /*      if (pipe == 0) */ {
342                 /* Program the Lane0/1 for HDMI B */
343                 u32 lane_reg, lane_value;
344
345                 lane_reg = PSB_LANE0;
346                 cdv_sb_read(dev, lane_reg, &lane_value);
347                 lane_value &= ~(LANE_PLL_MASK);
348                 lane_value |= LANE_PLL_ENABLE;
349                 cdv_sb_write(dev, lane_reg, lane_value);
350
351                 lane_reg = PSB_LANE1;
352                 cdv_sb_read(dev, lane_reg, &lane_value);
353                 lane_value &= ~(LANE_PLL_MASK);
354                 lane_value |= LANE_PLL_ENABLE;
355                 cdv_sb_write(dev, lane_reg, lane_value);
356
357                 /* Program the Lane2/3 for HDMI C */
358                 lane_reg = PSB_LANE2;
359                 cdv_sb_read(dev, lane_reg, &lane_value);
360                 lane_value &= ~(LANE_PLL_MASK);
361                 lane_value |= LANE_PLL_ENABLE;
362                 cdv_sb_write(dev, lane_reg, lane_value);
363
364                 lane_reg = PSB_LANE3;
365                 cdv_sb_read(dev, lane_reg, &lane_value);
366                 lane_value &= ~(LANE_PLL_MASK);
367                 lane_value |= LANE_PLL_ENABLE;
368                 cdv_sb_write(dev, lane_reg, lane_value);
369         }
370
371         return 0;
372 }
373
374 /*
375  * Returns whether any encoder on the specified pipe is of the specified type
376  */
377 static bool cdv_intel_pipe_has_type(struct drm_crtc *crtc, int type)
378 {
379         struct drm_device *dev = crtc->dev;
380         struct drm_mode_config *mode_config = &dev->mode_config;
381         struct drm_connector *l_entry;
382
383         list_for_each_entry(l_entry, &mode_config->connector_list, head) {
384                 if (l_entry->encoder && l_entry->encoder->crtc == crtc) {
385                         struct psb_intel_encoder *psb_intel_encoder =
386                                         psb_intel_attached_encoder(l_entry);
387                         if (psb_intel_encoder->type == type)
388                                 return true;
389                 }
390         }
391         return false;
392 }
393
394 static const struct cdv_intel_limit_t *cdv_intel_limit(struct drm_crtc *crtc,
395                                                         int refclk)
396 {
397         const struct cdv_intel_limit_t *limit;
398         if (cdv_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
399                 /*
400                  * Now only single-channel LVDS is supported on CDV. If it is
401                  * incorrect, please add the dual-channel LVDS.
402                  */
403                 if (refclk == 96000)
404                         limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_96];
405                 else
406                         limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_100];
407         } else {
408                 if (refclk == 27000)
409                         limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_27];
410                 else
411                         limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_96];
412         }
413         return limit;
414 }
415
416 /* m1 is reserved as 0 in CDV, n is a ring counter */
417 static void cdv_intel_clock(struct drm_device *dev,
418                         int refclk, struct cdv_intel_clock_t *clock)
419 {
420         clock->m = clock->m2 + 2;
421         clock->p = clock->p1 * clock->p2;
422         clock->vco = (refclk * clock->m) / clock->n;
423         clock->dot = clock->vco / clock->p;
424 }
425
426
427 #define INTELPllInvalid(s)   { /* ErrorF (s) */; return false; }
428 static bool cdv_intel_PLL_is_valid(struct drm_crtc *crtc,
429                                 const struct cdv_intel_limit_t *limit,
430                                struct cdv_intel_clock_t *clock)
431 {
432         if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
433                 INTELPllInvalid("p1 out of range\n");
434         if (clock->p < limit->p.min || limit->p.max < clock->p)
435                 INTELPllInvalid("p out of range\n");
436         /* unnecessary to check the range of m(m1/M2)/n again */
437         if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
438                 INTELPllInvalid("vco out of range\n");
439         /* XXX: We may need to be checking "Dot clock"
440          * depending on the multiplier, connector, etc.,
441          * rather than just a single range.
442          */
443         if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
444                 INTELPllInvalid("dot out of range\n");
445
446         return true;
447 }
448
449 static bool cdv_intel_find_best_PLL(struct drm_crtc *crtc, int target,
450                                 int refclk,
451                                 struct cdv_intel_clock_t *best_clock)
452 {
453         struct drm_device *dev = crtc->dev;
454         struct cdv_intel_clock_t clock;
455         const struct cdv_intel_limit_t *limit = cdv_intel_limit(crtc, refclk);
456         int err = target;
457
458
459         if (cdv_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
460             (REG_READ(LVDS) & LVDS_PORT_EN) != 0) {
461                 /*
462                  * For LVDS, if the panel is on, just rely on its current
463                  * settings for dual-channel.  We haven't figured out how to
464                  * reliably set up different single/dual channel state, if we
465                  * even can.
466                  */
467                 if ((REG_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
468                     LVDS_CLKB_POWER_UP)
469                         clock.p2 = limit->p2.p2_fast;
470                 else
471                         clock.p2 = limit->p2.p2_slow;
472         } else {
473                 if (target < limit->p2.dot_limit)
474                         clock.p2 = limit->p2.p2_slow;
475                 else
476                         clock.p2 = limit->p2.p2_fast;
477         }
478
479         memset(best_clock, 0, sizeof(*best_clock));
480         clock.m1 = 0;
481         /* m1 is reserved as 0 in CDV, n is a ring counter.
482            So skip the m1 loop */
483         for (clock.n = limit->n.min; clock.n <= limit->n.max; clock.n++) {
484                 for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max;
485                                              clock.m2++) {
486                         for (clock.p1 = limit->p1.min;
487                                         clock.p1 <= limit->p1.max;
488                                         clock.p1++) {
489                                 int this_err;
490
491                                 cdv_intel_clock(dev, refclk, &clock);
492
493                                 if (!cdv_intel_PLL_is_valid(crtc,
494                                                                 limit, &clock))
495                                                 continue;
496
497                                 this_err = abs(clock.dot - target);
498                                 if (this_err < err) {
499                                         *best_clock = clock;
500                                         err = this_err;
501                                 }
502                         }
503                 }
504         }
505
506         return err != target;
507 }
508
509 static int cdv_intel_pipe_set_base(struct drm_crtc *crtc,
510                             int x, int y, struct drm_framebuffer *old_fb)
511 {
512         struct drm_device *dev = crtc->dev;
513         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
514         struct psb_framebuffer *psbfb = to_psb_fb(crtc->fb);
515         int pipe = psb_intel_crtc->pipe;
516         unsigned long start, offset;
517         int dspbase = (pipe == 0 ? DSPABASE : DSPBBASE);
518         int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
519         int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
520         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
521         u32 dspcntr;
522         int ret = 0;
523
524         if (!gma_power_begin(dev, true))
525                 return 0;
526
527         /* no fb bound */
528         if (!crtc->fb) {
529                 dev_err(dev->dev, "No FB bound\n");
530                 goto psb_intel_pipe_cleaner;
531         }
532
533
534         /* We are displaying this buffer, make sure it is actually loaded
535            into the GTT */
536         ret = psb_gtt_pin(psbfb->gtt);
537         if (ret < 0)
538                 goto psb_intel_pipe_set_base_exit;
539         start = psbfb->gtt->offset;
540         offset = y * crtc->fb->pitches[0] + x * (crtc->fb->bits_per_pixel / 8);
541
542         REG_WRITE(dspstride, crtc->fb->pitches[0]);
543
544         dspcntr = REG_READ(dspcntr_reg);
545         dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
546
547         switch (crtc->fb->bits_per_pixel) {
548         case 8:
549                 dspcntr |= DISPPLANE_8BPP;
550                 break;
551         case 16:
552                 if (crtc->fb->depth == 15)
553                         dspcntr |= DISPPLANE_15_16BPP;
554                 else
555                         dspcntr |= DISPPLANE_16BPP;
556                 break;
557         case 24:
558         case 32:
559                 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
560                 break;
561         default:
562                 dev_err(dev->dev, "Unknown color depth\n");
563                 ret = -EINVAL;
564                 goto psb_intel_pipe_set_base_exit;
565         }
566         REG_WRITE(dspcntr_reg, dspcntr);
567
568         dev_dbg(dev->dev,
569                 "Writing base %08lX %08lX %d %d\n", start, offset, x, y);
570
571         REG_WRITE(dspbase, offset);
572         REG_READ(dspbase);
573         REG_WRITE(dspsurf, start);
574         REG_READ(dspsurf);
575
576 psb_intel_pipe_cleaner:
577         /* If there was a previous display we can now unpin it */
578         if (old_fb)
579                 psb_gtt_unpin(to_psb_fb(old_fb)->gtt);
580
581 psb_intel_pipe_set_base_exit:
582         gma_power_end(dev);
583         return ret;
584 }
585
586 #define         FIFO_PIPEA              (1 << 0)
587 #define         FIFO_PIPEB              (1 << 1)
588
589 static bool cdv_intel_pipe_enabled(struct drm_device *dev, int pipe)
590 {
591         struct drm_crtc *crtc;
592         struct drm_psb_private *dev_priv = dev->dev_private;
593         struct psb_intel_crtc *psb_intel_crtc = NULL;
594
595         crtc = dev_priv->pipe_to_crtc_mapping[pipe];
596         psb_intel_crtc = to_psb_intel_crtc(crtc);
597
598         if (crtc->fb == NULL || !psb_intel_crtc->active)
599                 return false;
600         return true;
601 }
602
603 static bool cdv_intel_single_pipe_active (struct drm_device *dev)
604 {
605         uint32_t pipe_enabled = 0;
606
607         if (cdv_intel_pipe_enabled(dev, 0))
608                 pipe_enabled |= FIFO_PIPEA;
609
610         if (cdv_intel_pipe_enabled(dev, 1))
611                 pipe_enabled |= FIFO_PIPEB;
612
613
614         DRM_DEBUG_KMS("pipe enabled %x\n", pipe_enabled);
615
616         if (pipe_enabled == FIFO_PIPEA || pipe_enabled == FIFO_PIPEB)
617                 return true;
618         else
619                 return false;
620 }
621
622 static bool is_pipeb_lvds(struct drm_device *dev, struct drm_crtc *crtc)
623 {
624         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
625         struct drm_mode_config *mode_config = &dev->mode_config;
626         struct drm_connector *connector;
627
628         if (psb_intel_crtc->pipe != 1)
629                 return false;
630
631         list_for_each_entry(connector, &mode_config->connector_list, head) {
632                 struct psb_intel_encoder *psb_intel_encoder =
633                                         psb_intel_attached_encoder(connector);
634
635                 if (!connector->encoder
636                     || connector->encoder->crtc != crtc)
637                         continue;
638
639                 if (psb_intel_encoder->type == INTEL_OUTPUT_LVDS)
640                         return true;
641         }
642
643         return false;
644 }
645
646 static void cdv_intel_disable_self_refresh (struct drm_device *dev)
647 {
648         if (REG_READ(FW_BLC_SELF) & FW_BLC_SELF_EN) {
649
650                 /* Disable self-refresh before adjust WM */
651                 REG_WRITE(FW_BLC_SELF, (REG_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN));
652                 REG_READ(FW_BLC_SELF);
653
654                 cdv_intel_wait_for_vblank(dev);
655
656                 /* Cedarview workaround to write ovelay plane, which force to leave
657                  * MAX_FIFO state.
658                  */
659                 REG_WRITE(OV_OVADD, 0/*dev_priv->ovl_offset*/);
660                 REG_READ(OV_OVADD);
661
662                 cdv_intel_wait_for_vblank(dev);
663         }
664
665 }
666
667 static void cdv_intel_update_watermark (struct drm_device *dev, struct drm_crtc *crtc)
668 {
669
670         if (cdv_intel_single_pipe_active(dev)) {
671                 u32 fw;
672
673                 fw = REG_READ(DSPFW1);
674                 fw &= ~DSP_FIFO_SR_WM_MASK;
675                 fw |= (0x7e << DSP_FIFO_SR_WM_SHIFT);
676                 fw &= ~CURSOR_B_FIFO_WM_MASK;
677                 fw |= (0x4 << CURSOR_B_FIFO_WM_SHIFT);
678                 REG_WRITE(DSPFW1, fw);
679
680                 fw = REG_READ(DSPFW2);
681                 fw &= ~CURSOR_A_FIFO_WM_MASK;
682                 fw |= (0x6 << CURSOR_A_FIFO_WM_SHIFT);
683                 fw &= ~DSP_PLANE_C_FIFO_WM_MASK;
684                 fw |= (0x8 << DSP_PLANE_C_FIFO_WM_SHIFT);
685                 REG_WRITE(DSPFW2, fw);
686
687                 REG_WRITE(DSPFW3, 0x36000000);
688
689                 /* ignore FW4 */
690
691                 if (is_pipeb_lvds(dev, crtc)) {
692                         REG_WRITE(DSPFW5, 0x00040330);
693                 } else {
694                         fw = (3 << DSP_PLANE_B_FIFO_WM1_SHIFT) |
695                              (4 << DSP_PLANE_A_FIFO_WM1_SHIFT) |
696                              (3 << CURSOR_B_FIFO_WM1_SHIFT) |
697                              (4 << CURSOR_FIFO_SR_WM1_SHIFT);
698                         REG_WRITE(DSPFW5, fw);
699                 }
700
701                 REG_WRITE(DSPFW6, 0x10);
702
703                 cdv_intel_wait_for_vblank(dev);
704
705                 /* enable self-refresh for single pipe active */
706                 REG_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
707                 REG_READ(FW_BLC_SELF);
708                 cdv_intel_wait_for_vblank(dev);
709
710         } else {
711
712                 /* HW team suggested values... */
713                 REG_WRITE(DSPFW1, 0x3f880808);
714                 REG_WRITE(DSPFW2, 0x0b020202);
715                 REG_WRITE(DSPFW3, 0x24000000);
716                 REG_WRITE(DSPFW4, 0x08030202);
717                 REG_WRITE(DSPFW5, 0x01010101);
718                 REG_WRITE(DSPFW6, 0x1d0);
719
720                 cdv_intel_wait_for_vblank(dev);
721
722                 cdv_intel_disable_self_refresh(dev);
723         
724         }
725 }
726
727 /** Loads the palette/gamma unit for the CRTC with the prepared values */
728 static void cdv_intel_crtc_load_lut(struct drm_crtc *crtc)
729 {
730         struct drm_device *dev = crtc->dev;
731         struct drm_psb_private *dev_priv =
732                                 (struct drm_psb_private *)dev->dev_private;
733         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
734         int palreg = PALETTE_A;
735         int i;
736
737         /* The clocks have to be on to load the palette. */
738         if (!crtc->enabled)
739                 return;
740
741         switch (psb_intel_crtc->pipe) {
742         case 0:
743                 break;
744         case 1:
745                 palreg = PALETTE_B;
746                 break;
747         case 2:
748                 palreg = PALETTE_C;
749                 break;
750         default:
751                 dev_err(dev->dev, "Illegal Pipe Number.\n");
752                 return;
753         }
754
755         if (gma_power_begin(dev, false)) {
756                 for (i = 0; i < 256; i++) {
757                         REG_WRITE(palreg + 4 * i,
758                                   ((psb_intel_crtc->lut_r[i] +
759                                   psb_intel_crtc->lut_adj[i]) << 16) |
760                                   ((psb_intel_crtc->lut_g[i] +
761                                   psb_intel_crtc->lut_adj[i]) << 8) |
762                                   (psb_intel_crtc->lut_b[i] +
763                                   psb_intel_crtc->lut_adj[i]));
764                 }
765                 gma_power_end(dev);
766         } else {
767                 for (i = 0; i < 256; i++) {
768                         dev_priv->regs.psb.save_palette_a[i] =
769                                   ((psb_intel_crtc->lut_r[i] +
770                                   psb_intel_crtc->lut_adj[i]) << 16) |
771                                   ((psb_intel_crtc->lut_g[i] +
772                                   psb_intel_crtc->lut_adj[i]) << 8) |
773                                   (psb_intel_crtc->lut_b[i] +
774                                   psb_intel_crtc->lut_adj[i]);
775                 }
776
777         }
778 }
779
780 /**
781  * Sets the power management mode of the pipe and plane.
782  *
783  * This code should probably grow support for turning the cursor off and back
784  * on appropriately at the same time as we're turning the pipe off/on.
785  */
786 static void cdv_intel_crtc_dpms(struct drm_crtc *crtc, int mode)
787 {
788         struct drm_device *dev = crtc->dev;
789         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
790         int pipe = psb_intel_crtc->pipe;
791         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
792         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
793         int dspbase_reg = (pipe == 0) ? DSPABASE : DSPBBASE;
794         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
795         int pipestat_reg = (pipe == 0) ? PIPEASTAT : PIPEBSTAT;
796         u32 temp;
797
798         /* XXX: When our outputs are all unaware of DPMS modes other than off
799          * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
800          */
801         cdv_intel_disable_self_refresh(dev);
802
803         switch (mode) {
804         case DRM_MODE_DPMS_ON:
805         case DRM_MODE_DPMS_STANDBY:
806         case DRM_MODE_DPMS_SUSPEND:
807                 if (psb_intel_crtc->active)
808                         return;
809
810                 psb_intel_crtc->active = true;
811
812                 /* Enable the DPLL */
813                 temp = REG_READ(dpll_reg);
814                 if ((temp & DPLL_VCO_ENABLE) == 0) {
815                         REG_WRITE(dpll_reg, temp);
816                         REG_READ(dpll_reg);
817                         /* Wait for the clocks to stabilize. */
818                         udelay(150);
819                         REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
820                         REG_READ(dpll_reg);
821                         /* Wait for the clocks to stabilize. */
822                         udelay(150);
823                         REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
824                         REG_READ(dpll_reg);
825                         /* Wait for the clocks to stabilize. */
826                         udelay(150);
827                 }
828
829                 /* Jim Bish - switch plan and pipe per scott */
830                 /* Enable the plane */
831                 temp = REG_READ(dspcntr_reg);
832                 if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
833                         REG_WRITE(dspcntr_reg,
834                                   temp | DISPLAY_PLANE_ENABLE);
835                         /* Flush the plane changes */
836                         REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
837                 }
838
839                 udelay(150);
840
841                 /* Enable the pipe */
842                 temp = REG_READ(pipeconf_reg);
843                 if ((temp & PIPEACONF_ENABLE) == 0)
844                         REG_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
845
846                 temp = REG_READ(pipestat_reg);
847                 temp &= ~(0xFFFF);
848                 temp |= PIPE_FIFO_UNDERRUN;
849                 REG_WRITE(pipestat_reg, temp);
850                 REG_READ(pipestat_reg);
851
852                 cdv_intel_update_watermark(dev, crtc);
853                 cdv_intel_crtc_load_lut(crtc);
854
855                 /* Give the overlay scaler a chance to enable
856                  * if it's on this pipe */
857                 /* psb_intel_crtc_dpms_video(crtc, true); TODO */
858                 psb_intel_crtc->crtc_enable = true;
859                 break;
860         case DRM_MODE_DPMS_OFF:
861                 if (!psb_intel_crtc->active)
862                         return;
863
864                 psb_intel_crtc->active = false;
865
866                 /* Give the overlay scaler a chance to disable
867                  * if it's on this pipe */
868                 /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
869
870                 /* Disable the VGA plane that we never use */
871                 REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
872
873                 /* Jim Bish - changed pipe/plane here as well. */
874
875                 drm_vblank_off(dev, pipe);
876                 /* Wait for vblank for the disable to take effect */
877                 cdv_intel_wait_for_vblank(dev);
878
879                 /* Next, disable display pipes */
880                 temp = REG_READ(pipeconf_reg);
881                 if ((temp & PIPEACONF_ENABLE) != 0) {
882                         REG_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
883                         REG_READ(pipeconf_reg);
884                 }
885
886                 /* Wait for vblank for the disable to take effect. */
887                 cdv_intel_wait_for_vblank(dev);
888
889                 udelay(150);
890
891                 /* Disable display plane */
892                 temp = REG_READ(dspcntr_reg);
893                 if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
894                         REG_WRITE(dspcntr_reg,
895                                   temp & ~DISPLAY_PLANE_ENABLE);
896                         /* Flush the plane changes */
897                         REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
898                         REG_READ(dspbase_reg);
899                 }
900
901                 temp = REG_READ(dpll_reg);
902                 if ((temp & DPLL_VCO_ENABLE) != 0) {
903                         REG_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
904                         REG_READ(dpll_reg);
905                 }
906
907                 /* Wait for the clocks to turn off. */
908                 udelay(150);
909                 cdv_intel_update_watermark(dev, crtc);
910                 psb_intel_crtc->crtc_enable = false;
911                 break;
912         }
913         /*Set FIFO Watermarks*/
914         REG_WRITE(DSPARB, 0x3F3E);
915 }
916
917 static void cdv_intel_crtc_prepare(struct drm_crtc *crtc)
918 {
919         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
920         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
921 }
922
923 static void cdv_intel_crtc_commit(struct drm_crtc *crtc)
924 {
925         struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
926         crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
927 }
928
929 static bool cdv_intel_crtc_mode_fixup(struct drm_crtc *crtc,
930                                   struct drm_display_mode *mode,
931                                   struct drm_display_mode *adjusted_mode)
932 {
933         return true;
934 }
935
936
937 /**
938  * Return the pipe currently connected to the panel fitter,
939  * or -1 if the panel fitter is not present or not in use
940  */
941 static int cdv_intel_panel_fitter_pipe(struct drm_device *dev)
942 {
943         u32 pfit_control;
944
945         pfit_control = REG_READ(PFIT_CONTROL);
946
947         /* See if the panel fitter is in use */
948         if ((pfit_control & PFIT_ENABLE) == 0)
949                 return -1;
950         return (pfit_control >> 29) & 0x3;
951 }
952
953 static int cdv_intel_crtc_mode_set(struct drm_crtc *crtc,
954                                struct drm_display_mode *mode,
955                                struct drm_display_mode *adjusted_mode,
956                                int x, int y,
957                                struct drm_framebuffer *old_fb)
958 {
959         struct drm_device *dev = crtc->dev;
960         struct drm_psb_private *dev_priv = dev->dev_private;
961         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
962         int pipe = psb_intel_crtc->pipe;
963         int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
964         int dpll_md_reg = (psb_intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
965         int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
966         int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
967         int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
968         int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
969         int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
970         int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
971         int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
972         int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
973         int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
974         int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
975         int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
976         int refclk;
977         struct cdv_intel_clock_t clock;
978         u32 dpll = 0, dspcntr, pipeconf;
979         bool ok;
980         bool is_crt = false, is_lvds = false, is_tv = false;
981         bool is_hdmi = false;
982         struct drm_mode_config *mode_config = &dev->mode_config;
983         struct drm_connector *connector;
984
985         list_for_each_entry(connector, &mode_config->connector_list, head) {
986                 struct psb_intel_encoder *psb_intel_encoder =
987                                         psb_intel_attached_encoder(connector);
988
989                 if (!connector->encoder
990                     || connector->encoder->crtc != crtc)
991                         continue;
992
993                 switch (psb_intel_encoder->type) {
994                 case INTEL_OUTPUT_LVDS:
995                         is_lvds = true;
996                         break;
997                 case INTEL_OUTPUT_TVOUT:
998                         is_tv = true;
999                         break;
1000                 case INTEL_OUTPUT_ANALOG:
1001                         is_crt = true;
1002                         break;
1003                 case INTEL_OUTPUT_HDMI:
1004                         is_hdmi = true;
1005                         break;
1006                 }
1007         }
1008
1009         if (dev_priv->dplla_96mhz)
1010                 /* low-end sku, 96/100 mhz */
1011                 refclk = 96000;
1012         else
1013                 /* high-end sku, 27/100 mhz */
1014                 refclk = 27000;
1015
1016         if (is_lvds && dev_priv->lvds_use_ssc) {
1017                 refclk = dev_priv->lvds_ssc_freq * 1000;
1018                 DRM_DEBUG_KMS("Use SSC reference clock %d Mhz\n", dev_priv->lvds_ssc_freq);
1019         }
1020
1021         drm_mode_debug_printmodeline(adjusted_mode);
1022
1023         ok = cdv_intel_find_best_PLL(crtc, adjusted_mode->clock, refclk,
1024                                  &clock);
1025         if (!ok) {
1026                 dev_err(dev->dev, "Couldn't find PLL settings for mode!\n");
1027                 return 0;
1028         }
1029
1030         dpll = DPLL_VGA_MODE_DIS;
1031         if (is_tv) {
1032                 /* XXX: just matching BIOS for now */
1033 /*      dpll |= PLL_REF_INPUT_TVCLKINBC; */
1034                 dpll |= 3;
1035         }
1036 /*              dpll |= PLL_REF_INPUT_DREFCLK; */
1037
1038         dpll |= DPLL_SYNCLOCK_ENABLE;
1039 /*      if (is_lvds)
1040                 dpll |= DPLLB_MODE_LVDS;
1041         else
1042                 dpll |= DPLLB_MODE_DAC_SERIAL; */
1043         /* dpll |= (2 << 11); */
1044
1045         /* setup pipeconf */
1046         pipeconf = REG_READ(pipeconf_reg);
1047
1048         /* Set up the display plane register */
1049         dspcntr = DISPPLANE_GAMMA_ENABLE;
1050
1051         if (pipe == 0)
1052                 dspcntr |= DISPPLANE_SEL_PIPE_A;
1053         else
1054                 dspcntr |= DISPPLANE_SEL_PIPE_B;
1055
1056         dspcntr |= DISPLAY_PLANE_ENABLE;
1057         pipeconf |= PIPEACONF_ENABLE;
1058
1059         REG_WRITE(dpll_reg, dpll | DPLL_VGA_MODE_DIS | DPLL_SYNCLOCK_ENABLE);
1060         REG_READ(dpll_reg);
1061
1062         cdv_dpll_set_clock_cdv(dev, crtc, &clock, is_lvds);
1063
1064         udelay(150);
1065
1066
1067         /* The LVDS pin pair needs to be on before the DPLLs are enabled.
1068          * This is an exception to the general rule that mode_set doesn't turn
1069          * things on.
1070          */
1071         if (is_lvds) {
1072                 u32 lvds = REG_READ(LVDS);
1073
1074                 lvds |=
1075                     LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP |
1076                     LVDS_PIPEB_SELECT;
1077                 /* Set the B0-B3 data pairs corresponding to
1078                  * whether we're going to
1079                  * set the DPLLs for dual-channel mode or not.
1080                  */
1081                 if (clock.p2 == 7)
1082                         lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
1083                 else
1084                         lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
1085
1086                 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
1087                  * appropriately here, but we need to look more
1088                  * thoroughly into how panels behave in the two modes.
1089                  */
1090
1091                 REG_WRITE(LVDS, lvds);
1092                 REG_READ(LVDS);
1093         }
1094
1095         dpll |= DPLL_VCO_ENABLE;
1096
1097         /* Disable the panel fitter if it was on our pipe */
1098         if (cdv_intel_panel_fitter_pipe(dev) == pipe)
1099                 REG_WRITE(PFIT_CONTROL, 0);
1100
1101         DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
1102         drm_mode_debug_printmodeline(mode);
1103
1104         REG_WRITE(dpll_reg,
1105                 (REG_READ(dpll_reg) & ~DPLL_LOCK) | DPLL_VCO_ENABLE);
1106         REG_READ(dpll_reg);
1107         /* Wait for the clocks to stabilize. */
1108         udelay(150); /* 42 usec w/o calibration, 110 with.  rounded up. */
1109
1110         if (!(REG_READ(dpll_reg) & DPLL_LOCK)) {
1111                 dev_err(dev->dev, "Failed to get DPLL lock\n");
1112                 return -EBUSY;
1113         }
1114
1115         {
1116                 int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
1117                 REG_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) | ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
1118         }
1119
1120         REG_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
1121                   ((adjusted_mode->crtc_htotal - 1) << 16));
1122         REG_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
1123                   ((adjusted_mode->crtc_hblank_end - 1) << 16));
1124         REG_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
1125                   ((adjusted_mode->crtc_hsync_end - 1) << 16));
1126         REG_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
1127                   ((adjusted_mode->crtc_vtotal - 1) << 16));
1128         REG_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
1129                   ((adjusted_mode->crtc_vblank_end - 1) << 16));
1130         REG_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
1131                   ((adjusted_mode->crtc_vsync_end - 1) << 16));
1132         /* pipesrc and dspsize control the size that is scaled from,
1133          * which should always be the user's requested size.
1134          */
1135         REG_WRITE(dspsize_reg,
1136                   ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
1137         REG_WRITE(dsppos_reg, 0);
1138         REG_WRITE(pipesrc_reg,
1139                   ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
1140         REG_WRITE(pipeconf_reg, pipeconf);
1141         REG_READ(pipeconf_reg);
1142
1143         cdv_intel_wait_for_vblank(dev);
1144
1145         REG_WRITE(dspcntr_reg, dspcntr);
1146
1147         /* Flush the plane changes */
1148         {
1149                 struct drm_crtc_helper_funcs *crtc_funcs =
1150                     crtc->helper_private;
1151                 crtc_funcs->mode_set_base(crtc, x, y, old_fb);
1152         }
1153
1154         cdv_intel_wait_for_vblank(dev);
1155
1156         return 0;
1157 }
1158
1159
1160 /**
1161  * Save HW states of giving crtc
1162  */
1163 static void cdv_intel_crtc_save(struct drm_crtc *crtc)
1164 {
1165         struct drm_device *dev = crtc->dev;
1166         /* struct drm_psb_private *dev_priv =
1167                         (struct drm_psb_private *)dev->dev_private; */
1168         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1169         struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
1170         int pipeA = (psb_intel_crtc->pipe == 0);
1171         uint32_t paletteReg;
1172         int i;
1173
1174         if (!crtc_state) {
1175                 dev_dbg(dev->dev, "No CRTC state found\n");
1176                 return;
1177         }
1178
1179         crtc_state->saveDSPCNTR = REG_READ(pipeA ? DSPACNTR : DSPBCNTR);
1180         crtc_state->savePIPECONF = REG_READ(pipeA ? PIPEACONF : PIPEBCONF);
1181         crtc_state->savePIPESRC = REG_READ(pipeA ? PIPEASRC : PIPEBSRC);
1182         crtc_state->saveFP0 = REG_READ(pipeA ? FPA0 : FPB0);
1183         crtc_state->saveFP1 = REG_READ(pipeA ? FPA1 : FPB1);
1184         crtc_state->saveDPLL = REG_READ(pipeA ? DPLL_A : DPLL_B);
1185         crtc_state->saveHTOTAL = REG_READ(pipeA ? HTOTAL_A : HTOTAL_B);
1186         crtc_state->saveHBLANK = REG_READ(pipeA ? HBLANK_A : HBLANK_B);
1187         crtc_state->saveHSYNC = REG_READ(pipeA ? HSYNC_A : HSYNC_B);
1188         crtc_state->saveVTOTAL = REG_READ(pipeA ? VTOTAL_A : VTOTAL_B);
1189         crtc_state->saveVBLANK = REG_READ(pipeA ? VBLANK_A : VBLANK_B);
1190         crtc_state->saveVSYNC = REG_READ(pipeA ? VSYNC_A : VSYNC_B);
1191         crtc_state->saveDSPSTRIDE = REG_READ(pipeA ? DSPASTRIDE : DSPBSTRIDE);
1192
1193         /*NOTE: DSPSIZE DSPPOS only for psb*/
1194         crtc_state->saveDSPSIZE = REG_READ(pipeA ? DSPASIZE : DSPBSIZE);
1195         crtc_state->saveDSPPOS = REG_READ(pipeA ? DSPAPOS : DSPBPOS);
1196
1197         crtc_state->saveDSPBASE = REG_READ(pipeA ? DSPABASE : DSPBBASE);
1198
1199         DRM_DEBUG("(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
1200                         crtc_state->saveDSPCNTR,
1201                         crtc_state->savePIPECONF,
1202                         crtc_state->savePIPESRC,
1203                         crtc_state->saveFP0,
1204                         crtc_state->saveFP1,
1205                         crtc_state->saveDPLL,
1206                         crtc_state->saveHTOTAL,
1207                         crtc_state->saveHBLANK,
1208                         crtc_state->saveHSYNC,
1209                         crtc_state->saveVTOTAL,
1210                         crtc_state->saveVBLANK,
1211                         crtc_state->saveVSYNC,
1212                         crtc_state->saveDSPSTRIDE,
1213                         crtc_state->saveDSPSIZE,
1214                         crtc_state->saveDSPPOS,
1215                         crtc_state->saveDSPBASE
1216                 );
1217
1218         paletteReg = pipeA ? PALETTE_A : PALETTE_B;
1219         for (i = 0; i < 256; ++i)
1220                 crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
1221 }
1222
1223 /**
1224  * Restore HW states of giving crtc
1225  */
1226 static void cdv_intel_crtc_restore(struct drm_crtc *crtc)
1227 {
1228         struct drm_device *dev = crtc->dev;
1229         /* struct drm_psb_private * dev_priv =
1230                                 (struct drm_psb_private *)dev->dev_private; */
1231         struct psb_intel_crtc *psb_intel_crtc =  to_psb_intel_crtc(crtc);
1232         struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
1233         /* struct drm_crtc_helper_funcs * crtc_funcs = crtc->helper_private; */
1234         int pipeA = (psb_intel_crtc->pipe == 0);
1235         uint32_t paletteReg;
1236         int i;
1237
1238         if (!crtc_state) {
1239                 dev_dbg(dev->dev, "No crtc state\n");
1240                 return;
1241         }
1242
1243         DRM_DEBUG(
1244                 "current:(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
1245                 REG_READ(pipeA ? DSPACNTR : DSPBCNTR),
1246                 REG_READ(pipeA ? PIPEACONF : PIPEBCONF),
1247                 REG_READ(pipeA ? PIPEASRC : PIPEBSRC),
1248                 REG_READ(pipeA ? FPA0 : FPB0),
1249                 REG_READ(pipeA ? FPA1 : FPB1),
1250                 REG_READ(pipeA ? DPLL_A : DPLL_B),
1251                 REG_READ(pipeA ? HTOTAL_A : HTOTAL_B),
1252                 REG_READ(pipeA ? HBLANK_A : HBLANK_B),
1253                 REG_READ(pipeA ? HSYNC_A : HSYNC_B),
1254                 REG_READ(pipeA ? VTOTAL_A : VTOTAL_B),
1255                 REG_READ(pipeA ? VBLANK_A : VBLANK_B),
1256                 REG_READ(pipeA ? VSYNC_A : VSYNC_B),
1257                 REG_READ(pipeA ? DSPASTRIDE : DSPBSTRIDE),
1258                 REG_READ(pipeA ? DSPASIZE : DSPBSIZE),
1259                 REG_READ(pipeA ? DSPAPOS : DSPBPOS),
1260                 REG_READ(pipeA ? DSPABASE : DSPBBASE)
1261                 );
1262
1263         DRM_DEBUG(
1264                 "saved: (%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
1265                 crtc_state->saveDSPCNTR,
1266                 crtc_state->savePIPECONF,
1267                 crtc_state->savePIPESRC,
1268                 crtc_state->saveFP0,
1269                 crtc_state->saveFP1,
1270                 crtc_state->saveDPLL,
1271                 crtc_state->saveHTOTAL,
1272                 crtc_state->saveHBLANK,
1273                 crtc_state->saveHSYNC,
1274                 crtc_state->saveVTOTAL,
1275                 crtc_state->saveVBLANK,
1276                 crtc_state->saveVSYNC,
1277                 crtc_state->saveDSPSTRIDE,
1278                 crtc_state->saveDSPSIZE,
1279                 crtc_state->saveDSPPOS,
1280                 crtc_state->saveDSPBASE
1281                 );
1282
1283
1284         if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
1285                 REG_WRITE(pipeA ? DPLL_A : DPLL_B,
1286                         crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
1287                 REG_READ(pipeA ? DPLL_A : DPLL_B);
1288                 DRM_DEBUG("write dpll: %x\n",
1289                                 REG_READ(pipeA ? DPLL_A : DPLL_B));
1290                 udelay(150);
1291         }
1292
1293         REG_WRITE(pipeA ? FPA0 : FPB0, crtc_state->saveFP0);
1294         REG_READ(pipeA ? FPA0 : FPB0);
1295
1296         REG_WRITE(pipeA ? FPA1 : FPB1, crtc_state->saveFP1);
1297         REG_READ(pipeA ? FPA1 : FPB1);
1298
1299         REG_WRITE(pipeA ? DPLL_A : DPLL_B, crtc_state->saveDPLL);
1300         REG_READ(pipeA ? DPLL_A : DPLL_B);
1301         udelay(150);
1302
1303         REG_WRITE(pipeA ? HTOTAL_A : HTOTAL_B, crtc_state->saveHTOTAL);
1304         REG_WRITE(pipeA ? HBLANK_A : HBLANK_B, crtc_state->saveHBLANK);
1305         REG_WRITE(pipeA ? HSYNC_A : HSYNC_B, crtc_state->saveHSYNC);
1306         REG_WRITE(pipeA ? VTOTAL_A : VTOTAL_B, crtc_state->saveVTOTAL);
1307         REG_WRITE(pipeA ? VBLANK_A : VBLANK_B, crtc_state->saveVBLANK);
1308         REG_WRITE(pipeA ? VSYNC_A : VSYNC_B, crtc_state->saveVSYNC);
1309         REG_WRITE(pipeA ? DSPASTRIDE : DSPBSTRIDE, crtc_state->saveDSPSTRIDE);
1310
1311         REG_WRITE(pipeA ? DSPASIZE : DSPBSIZE, crtc_state->saveDSPSIZE);
1312         REG_WRITE(pipeA ? DSPAPOS : DSPBPOS, crtc_state->saveDSPPOS);
1313
1314         REG_WRITE(pipeA ? PIPEASRC : PIPEBSRC, crtc_state->savePIPESRC);
1315         REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
1316         REG_WRITE(pipeA ? PIPEACONF : PIPEBCONF, crtc_state->savePIPECONF);
1317
1318         cdv_intel_wait_for_vblank(dev);
1319
1320         REG_WRITE(pipeA ? DSPACNTR : DSPBCNTR, crtc_state->saveDSPCNTR);
1321         REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
1322
1323         cdv_intel_wait_for_vblank(dev);
1324
1325         paletteReg = pipeA ? PALETTE_A : PALETTE_B;
1326         for (i = 0; i < 256; ++i)
1327                 REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
1328 }
1329
1330 static int cdv_intel_crtc_cursor_set(struct drm_crtc *crtc,
1331                                  struct drm_file *file_priv,
1332                                  uint32_t handle,
1333                                  uint32_t width, uint32_t height)
1334 {
1335         struct drm_device *dev = crtc->dev;
1336         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1337         int pipe = psb_intel_crtc->pipe;
1338         uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
1339         uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
1340         uint32_t temp;
1341         size_t addr = 0;
1342         struct gtt_range *gt;
1343         struct drm_gem_object *obj;
1344         int ret;
1345
1346         /* if we want to turn of the cursor ignore width and height */
1347         if (!handle) {
1348                 /* turn off the cursor */
1349                 temp = CURSOR_MODE_DISABLE;
1350
1351                 if (gma_power_begin(dev, false)) {
1352                         REG_WRITE(control, temp);
1353                         REG_WRITE(base, 0);
1354                         gma_power_end(dev);
1355                 }
1356
1357                 /* unpin the old GEM object */
1358                 if (psb_intel_crtc->cursor_obj) {
1359                         gt = container_of(psb_intel_crtc->cursor_obj,
1360                                                         struct gtt_range, gem);
1361                         psb_gtt_unpin(gt);
1362                         drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
1363                         psb_intel_crtc->cursor_obj = NULL;
1364                 }
1365
1366                 return 0;
1367         }
1368
1369         /* Currently we only support 64x64 cursors */
1370         if (width != 64 || height != 64) {
1371                 dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
1372                 return -EINVAL;
1373         }
1374
1375         obj = drm_gem_object_lookup(dev, file_priv, handle);
1376         if (!obj)
1377                 return -ENOENT;
1378
1379         if (obj->size < width * height * 4) {
1380                 dev_dbg(dev->dev, "buffer is to small\n");
1381                 return -ENOMEM;
1382         }
1383
1384         gt = container_of(obj, struct gtt_range, gem);
1385
1386         /* Pin the memory into the GTT */
1387         ret = psb_gtt_pin(gt);
1388         if (ret) {
1389                 dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
1390                 return ret;
1391         }
1392
1393         addr = gt->offset;      /* Or resource.start ??? */
1394
1395         psb_intel_crtc->cursor_addr = addr;
1396
1397         temp = 0;
1398         /* set the pipe for the cursor */
1399         temp |= (pipe << 28);
1400         temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
1401
1402         if (gma_power_begin(dev, false)) {
1403                 REG_WRITE(control, temp);
1404                 REG_WRITE(base, addr);
1405                 gma_power_end(dev);
1406         }
1407
1408         /* unpin the old GEM object */
1409         if (psb_intel_crtc->cursor_obj) {
1410                 gt = container_of(psb_intel_crtc->cursor_obj,
1411                                                         struct gtt_range, gem);
1412                 psb_gtt_unpin(gt);
1413                 drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
1414                 psb_intel_crtc->cursor_obj = obj;
1415         }
1416         return 0;
1417 }
1418
1419 static int cdv_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
1420 {
1421         struct drm_device *dev = crtc->dev;
1422         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1423         int pipe = psb_intel_crtc->pipe;
1424         uint32_t temp = 0;
1425         uint32_t adder;
1426
1427
1428         if (x < 0) {
1429                 temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
1430                 x = -x;
1431         }
1432         if (y < 0) {
1433                 temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
1434                 y = -y;
1435         }
1436
1437         temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
1438         temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
1439
1440         adder = psb_intel_crtc->cursor_addr;
1441
1442         if (gma_power_begin(dev, false)) {
1443                 REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
1444                 REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
1445                 gma_power_end(dev);
1446         }
1447         return 0;
1448 }
1449
1450 static void cdv_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
1451                          u16 *green, u16 *blue, uint32_t start, uint32_t size)
1452 {
1453         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1454         int i;
1455         int end = (start + size > 256) ? 256 : start + size;
1456
1457         for (i = start; i < end; i++) {
1458                 psb_intel_crtc->lut_r[i] = red[i] >> 8;
1459                 psb_intel_crtc->lut_g[i] = green[i] >> 8;
1460                 psb_intel_crtc->lut_b[i] = blue[i] >> 8;
1461         }
1462
1463         cdv_intel_crtc_load_lut(crtc);
1464 }
1465
1466 static int cdv_crtc_set_config(struct drm_mode_set *set)
1467 {
1468         int ret = 0;
1469         struct drm_device *dev = set->crtc->dev;
1470         struct drm_psb_private *dev_priv = dev->dev_private;
1471
1472         if (!dev_priv->rpm_enabled)
1473                 return drm_crtc_helper_set_config(set);
1474
1475         pm_runtime_forbid(&dev->pdev->dev);
1476
1477         ret = drm_crtc_helper_set_config(set);
1478
1479         pm_runtime_allow(&dev->pdev->dev);
1480
1481         return ret;
1482 }
1483
1484 /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
1485
1486 /* FIXME: why are we using this, should it be cdv_ in this tree ? */
1487
1488 static void i8xx_clock(int refclk, struct cdv_intel_clock_t *clock)
1489 {
1490         clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
1491         clock->p = clock->p1 * clock->p2;
1492         clock->vco = refclk * clock->m / (clock->n + 2);
1493         clock->dot = clock->vco / clock->p;
1494 }
1495
1496 /* Returns the clock of the currently programmed mode of the given pipe. */
1497 static int cdv_intel_crtc_clock_get(struct drm_device *dev,
1498                                 struct drm_crtc *crtc)
1499 {
1500         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1501         int pipe = psb_intel_crtc->pipe;
1502         u32 dpll;
1503         u32 fp;
1504         struct cdv_intel_clock_t clock;
1505         bool is_lvds;
1506         struct drm_psb_private *dev_priv = dev->dev_private;
1507
1508         if (gma_power_begin(dev, false)) {
1509                 dpll = REG_READ((pipe == 0) ? DPLL_A : DPLL_B);
1510                 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
1511                         fp = REG_READ((pipe == 0) ? FPA0 : FPB0);
1512                 else
1513                         fp = REG_READ((pipe == 0) ? FPA1 : FPB1);
1514                 is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
1515                 gma_power_end(dev);
1516         } else {
1517                 dpll = (pipe == 0) ?
1518                         dev_priv->regs.psb.saveDPLL_A :
1519                         dev_priv->regs.psb.saveDPLL_B;
1520
1521                 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
1522                         fp = (pipe == 0) ?
1523                                 dev_priv->regs.psb.saveFPA0 :
1524                                 dev_priv->regs.psb.saveFPB0;
1525                 else
1526                         fp = (pipe == 0) ?
1527                                 dev_priv->regs.psb.saveFPA1 :
1528                                 dev_priv->regs.psb.saveFPB1;
1529
1530                 is_lvds = (pipe == 1) &&
1531                                 (dev_priv->regs.psb.saveLVDS & LVDS_PORT_EN);
1532         }
1533
1534         clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
1535         clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
1536         clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
1537
1538         if (is_lvds) {
1539                 clock.p1 =
1540                     ffs((dpll &
1541                          DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
1542                         DPLL_FPA01_P1_POST_DIV_SHIFT);
1543                 if (clock.p1 == 0) {
1544                         clock.p1 = 4;
1545                         dev_err(dev->dev, "PLL %d\n", dpll);
1546                 }
1547                 clock.p2 = 14;
1548
1549                 if ((dpll & PLL_REF_INPUT_MASK) ==
1550                     PLLB_REF_INPUT_SPREADSPECTRUMIN) {
1551                         /* XXX: might not be 66MHz */
1552                         i8xx_clock(66000, &clock);
1553                 } else
1554                         i8xx_clock(48000, &clock);
1555         } else {
1556                 if (dpll & PLL_P1_DIVIDE_BY_TWO)
1557                         clock.p1 = 2;
1558                 else {
1559                         clock.p1 =
1560                             ((dpll &
1561                               DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
1562                              DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
1563                 }
1564                 if (dpll & PLL_P2_DIVIDE_BY_4)
1565                         clock.p2 = 4;
1566                 else
1567                         clock.p2 = 2;
1568
1569                 i8xx_clock(48000, &clock);
1570         }
1571
1572         /* XXX: It would be nice to validate the clocks, but we can't reuse
1573          * i830PllIsValid() because it relies on the xf86_config connector
1574          * configuration being accurate, which it isn't necessarily.
1575          */
1576
1577         return clock.dot;
1578 }
1579
1580 /** Returns the currently programmed mode of the given pipe. */
1581 struct drm_display_mode *cdv_intel_crtc_mode_get(struct drm_device *dev,
1582                                              struct drm_crtc *crtc)
1583 {
1584         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1585         int pipe = psb_intel_crtc->pipe;
1586         struct drm_display_mode *mode;
1587         int htot;
1588         int hsync;
1589         int vtot;
1590         int vsync;
1591         struct drm_psb_private *dev_priv = dev->dev_private;
1592
1593         if (gma_power_begin(dev, false)) {
1594                 htot = REG_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
1595                 hsync = REG_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
1596                 vtot = REG_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
1597                 vsync = REG_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
1598                 gma_power_end(dev);
1599         } else {
1600                 htot = (pipe == 0) ?
1601                         dev_priv->regs.psb.saveHTOTAL_A :
1602                         dev_priv->regs.psb.saveHTOTAL_B;
1603                 hsync = (pipe == 0) ?
1604                         dev_priv->regs.psb.saveHSYNC_A :
1605                         dev_priv->regs.psb.saveHSYNC_B;
1606                 vtot = (pipe == 0) ?
1607                         dev_priv->regs.psb.saveVTOTAL_A :
1608                         dev_priv->regs.psb.saveVTOTAL_B;
1609                 vsync = (pipe == 0) ?
1610                         dev_priv->regs.psb.saveVSYNC_A :
1611                         dev_priv->regs.psb.saveVSYNC_B;
1612         }
1613
1614         mode = kzalloc(sizeof(*mode), GFP_KERNEL);
1615         if (!mode)
1616                 return NULL;
1617
1618         mode->clock = cdv_intel_crtc_clock_get(dev, crtc);
1619         mode->hdisplay = (htot & 0xffff) + 1;
1620         mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
1621         mode->hsync_start = (hsync & 0xffff) + 1;
1622         mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
1623         mode->vdisplay = (vtot & 0xffff) + 1;
1624         mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
1625         mode->vsync_start = (vsync & 0xffff) + 1;
1626         mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
1627
1628         drm_mode_set_name(mode);
1629         drm_mode_set_crtcinfo(mode, 0);
1630
1631         return mode;
1632 }
1633
1634 static void cdv_intel_crtc_destroy(struct drm_crtc *crtc)
1635 {
1636         struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
1637
1638         kfree(psb_intel_crtc->crtc_state);
1639         drm_crtc_cleanup(crtc);
1640         kfree(psb_intel_crtc);
1641 }
1642
1643 const struct drm_crtc_helper_funcs cdv_intel_helper_funcs = {
1644         .dpms = cdv_intel_crtc_dpms,
1645         .mode_fixup = cdv_intel_crtc_mode_fixup,
1646         .mode_set = cdv_intel_crtc_mode_set,
1647         .mode_set_base = cdv_intel_pipe_set_base,
1648         .prepare = cdv_intel_crtc_prepare,
1649         .commit = cdv_intel_crtc_commit,
1650 };
1651
1652 const struct drm_crtc_funcs cdv_intel_crtc_funcs = {
1653         .save = cdv_intel_crtc_save,
1654         .restore = cdv_intel_crtc_restore,
1655         .cursor_set = cdv_intel_crtc_cursor_set,
1656         .cursor_move = cdv_intel_crtc_cursor_move,
1657         .gamma_set = cdv_intel_crtc_gamma_set,
1658         .set_config = cdv_crtc_set_config,
1659         .destroy = cdv_intel_crtc_destroy,
1660 };