summaryrefslogtreecommitdiffstats
path: root/src/mrvl_crtc.c
blob: 7f42441f9c652ea8bb24560dc493a88a8783278e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678

#include "dovefb.h"
#include "dovefb_driver.h"
  
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>

#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/kd.h>

#include "mrvl_cursor.h"

/* H/W register offset. */
#define DMA_CONTROL0	0x190

#define CRTC_DUMP_RAW_VIDEO         0

#if CRTC_DUMP_RAW_VIDEO
static char *g_dumpEXAFileName = "//crtc.rgb";
static unsigned int g_dumpEXAFrameIndex = 0;
#endif

#if MRVL_CRTC_SUPPORT_ROTATION
static void *
mrvl_crtc_shadow_allocate (xf86CrtcPtr crtc, int width, int height)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    FBDevRec* pDev = FBDEVPTR(pScrn);
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    unsigned long rotate_pitch, clear_pitch;
    //unsigned long rotate_offset;
    //int rotate_size, clear_size;
    int i;

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_shadow_allocate, crtc %d, width %d, height %d, xoff %d, yoff %d, scrn virtual x %d, y %d, config virtual x %d, y %d \n", pCrtcPriv->crtc_id, width, height, crtc->x, crtc->y, pScrn->virtualX, pScrn->virtualY, pCrtcPriv->configVirtualX, pCrtcPriv->configVirtualY);

    rotate_pitch = pScrn->displayWidth * pScrn->bitsPerPixel / 8;
    //rotate_size = rotate_pitch * height;

    clear_pitch = width * pScrn->bitsPerPixel / 8;
    //clear_size = clear_pitch * height;

    /* We could get close to what we want here by just creating a pixmap like
     * normal, but we have to lock it down in framebuffer, and there is no
     * setter for offscreen area locking in B/driver/X/EXA currently.  So, we just
     * allocate offscreen memory and fake up a pixmap header for it.
     */    

    // Point to the second frame buffer on base layer.
    pCrtcPriv->crtc_rotate_mem = pDev->fbstart + pScrn->displayWidth * pCrtcPriv->configVirtualY * (pCrtcPriv->crtc_id + 1) * pScrn->bitsPerPixel / 8;
    if (pCrtcPriv->crtc_rotate_mem == 0)
	    return NULL;

    // Need to clear new shadow buffer.
    for (i = 0; i < height; i++)
    {
        memset(((char *)pCrtcPriv->crtc_rotate_mem) + i * rotate_pitch, 0, clear_pitch);
    }

    pCrtcPriv->crtc_roate_phy_off = pScrn->displayWidth * pCrtcPriv->configVirtualY * (pCrtcPriv->crtc_id + 1) * pScrn->bitsPerPixel / 8;
  
    return pCrtcPriv->crtc_rotate_mem;
}

/* Copy some logic from the Geode driver to fix (or work around) corrupted
 * pixmaps after rotate:
 * http://lists.x.org/archives/xorg-devel/2013-April/036011.html
 *
 * The Xserver has a scratch pixmap allocation routine that will
 * try to use the existing scratch pixmap if possible. When the driver
 * or any other user stops using it, it need to clear out any pixmap
 * state (private data etc) otherwise the next user may get stale data.
 *
 * Use our own wrapper to allocate a pixmap for wrapping a buffer object.
 * This way we don't use X's own scratc pixmap for rotation.
 */
static PixmapPtr create_bo_pixmap(ScreenPtr pScreen,
				  int width, int height,
				  int depth, int bpp,
				  int pitch, pointer pPixData)
{
    PixmapPtr pixmap = (*pScreen->CreatePixmap)(pScreen, 0, 0, depth, 0);
    if (!pixmap)
	return NULL;
    if (!(*pScreen->ModifyPixmapHeader)(pixmap, width, height,
					depth, bpp, pitch, pPixData)) {
	(*pScreen->DestroyPixmap)(pixmap);
	return NULL;
    }

    return pixmap;
}

static void destroy_bo_pixmap(PixmapPtr pixmap)
{
    ScreenPtr pScreen = pixmap->drawable.pScreen;
    (*pScreen->DestroyPixmap)(pixmap);
}

static PixmapPtr
mrvl_crtc_shadow_create(xf86CrtcPtr crtc, void *data, int width, int height)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    unsigned long rotate_pitch;
    PixmapPtr rotate_pixmap;
    int cpp = pScrn->bitsPerPixel / 8;
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_shadow_create, crtc [%d][%p], width %d, height %d, xoff %d, yoff %d, data %p\n", pCrtcPriv->crtc_id, crtc, width, height, crtc->x, crtc->y, data);
    
    if (!data)
	    data = mrvl_crtc_shadow_allocate(crtc, width, height);

    rotate_pitch = pScrn->displayWidth * cpp;

    rotate_pixmap = create_bo_pixmap(pScrn->pScreen,
				     width, height,
				     pScrn->depth,
				     pScrn->bitsPerPixel,
				     rotate_pitch, data);

    if (rotate_pixmap == NULL) {
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		   "Couldn't allocate shadow pixmap for rotated CRTC\n");
    }

    return rotate_pixmap;
}

static void
mrvl_crtc_shadow_destroy(xf86CrtcPtr crtc, PixmapPtr rotate_pixmap, void *data)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_shadow_destroy\n");

    if (rotate_pixmap) {
            xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvl_crtc_shadow_destroy have a rotate_pixmap\n");
	    destroy_bo_pixmap(rotate_pixmap);
    }

#if CRTC_DUMP_RAW_VIDEO
    VIVDumpImage(g_dumpEXAFileName,
                 &g_dumpEXAFrameIndex, 
                 pCrtcPriv->crtc_rotate_mem, 
                 800, 
                 480,
                 16);
#endif

    if (data) {
	    pCrtcPriv->crtc_rotate_mem = NULL;
    }
}
#endif

int mrvl_crtc_config(xf86CrtcPtr crtc, int off)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLGetPrivateByScrn(pScrn);
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    int fd;
    //struct fb_var_screeninfo var;
    struct fb_fix_screeninfo 	fb_finfo;
    int fb_size;
    char *mmio_regbase = 0;
    volatile unsigned int *ptr = 0;	
    
    /* open device */
    fd = pDev->gfx_fd[pCrtcPriv->crtc_id];

    if (fd < 0) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvl_crtc_config: crtc%d fd isn't valid\n", pCrtcPriv->crtc_id);
        return -2;
    }

    if (ioctl(fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvl_crtc_config: fail to get fscreeninfo for crtc [%p]\n", crtc);
        return -1;
    } else {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvl_crtc_config: crtc [%p] get fscreeninfo successfully\n", crtc);
    }

    fb_size = fb_finfo.smem_len;
    /* map registers. */
    if ((mmio_regbase = (char *) mmap(0, 0x1C4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, fb_size)) == (char *) -1) {
    //if ((mmio_regbase = (char *) mmap(0, fb_finfo.mmio_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, fb_finfo.mmio_start)) == (char *) -1) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "fail to mmap io regs in mrvl_crtc_config\n");
	return -1;
    }

    pDev->mmio[pCrtcPriv->crtc_id] = mmio_regbase;
    ptr = (unsigned int*)(mmio_regbase+DMA_CONTROL0);
    if (off) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "turn off crct%d\n", pCrtcPriv->crtc_id);
        *ptr &= ~(0x1 << 8 | 0x1);
    } else {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "turn on crtc%d\n", pCrtcPriv->crtc_id);
        *ptr |= 0x1 << 8;
    }

    return 0;
}

void 
mrvl_crtc_dpms(xf86CrtcPtr crtc, int mode)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    FBDevRec* pDev = FBDEVPTR(pScrn);

    if (3 == mode) {  //off the output
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_dpms, mode %d\n", mode);
        /* set crtc to off status. */
        mrvl_crtc_config(crtc, 1);	
    }

    // Hide h/w cursor when prepare to switch mode.
    if (pDev->HWcursor)
    {
        DovefbHideCursor(pScrn, pCrtcPriv->crtc_id);
    }
}

static Bool
mrvl_crtc_mode_fixup(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_mode_fixup, orig: width %d, height %d, ajust: width %d, height %d\n", 
                        mode->HDisplay, 
                        mode->VDisplay, 
                        adjusted_mode->HDisplay, 
                        adjusted_mode->VDisplay);
    return TRUE;
}

static void
mrvl_crtc_mode_prepare(xf86CrtcPtr crtc)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    FBDevRec* pDev = FBDEVPTR(pScrn);

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_mode_prepare\n");
    
    // Hide h/w cursor when prepare to switch mode.
    if (pDev->HWcursor)
    {
        DovefbHideCursor(pScrn, pCrtcPriv->crtc_id);
    }
}

int mrvl_crtc_set_pos(xf86CrtcPtr crtc, int x, int y)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLGetPrivateByScrn(pScrn);
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    int fd;
    struct fb_var_screeninfo var;
    int err;

    if (!pDev || !pCrtcPriv) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvl_crtc_set_pos: pDev or pCrtcPriv isn't valid.\n");
        return -2;
    }

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvl_crtc_set_pos: set crtc%d position (%d, %d)\n",
        pCrtcPriv->crtc_id, x, y);
    fd = pDev->gfx_fd[pCrtcPriv->crtc_id];

    if(fd < 0) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvl_crtc_set_pos: fd isn't valid\n");
        return -1;
    } else {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvl_crtc_set_pos: Open fb for crtc%d\n", pCrtcPriv->crtc_id);
    }

    /*get "var" screeninfo*/
    err = ioctl(fd, FBIOGET_VSCREENINFO, &var);
    if (err) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvl_crtc_set_pos: fail to get base layer info\n");
        return -1;
    }

    var.xoffset = x;
    var.activate = FB_ACTIVATE_NOW;

    if (crtc->rotation == RR_Rotate_0)
        var.yoffset = y;
    else {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvl_crtc_set_pos: var virtual x %d, y %d, scrn virtual x %d, y %d\n", var.xres_virtual, var.yres_virtual, pScrn->virtualX, pScrn->virtualY);

        var.yres_virtual = pCrtcPriv->configVirtualY * (pCrtcPriv->crtc_num + 1);
        var.xoffset = 0;
        var.yoffset = pCrtcPriv->configVirtualY * (pCrtcPriv->crtc_id + 1);
    }

    if (ioctl(fd, FBIOPUT_VSCREENINFO, &var) == -1) {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvl_crtc_set_pos:FBIOPUT_VSCREENINFO FAILED \n");
        return -1;
    }

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "leaving mrvl_crtc_set_pos: set crtc%d position (%d, %d)\n",
        pCrtcPriv->crtc_id, x, y);

    return 0;
} 

static void
mrvl_crtc_mode_set(xf86CrtcPtr crtc, DisplayModePtr mode, DisplayModePtr adjusted_mode, int x, int y)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLGetPrivateByScrn(pScrn);
    //MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;

#if 1
    /* set crct to on status. */
    mrvl_crtc_config(crtc, 0);
#endif
    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "set mode_set x=%d, y=%d\n", x, y);

    if (pDev && crtc == pDev->pCrtc[0]) {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
            "LCD0 crtc mode = <%dx%d>, desired mode <%dx%d>\n",
            crtc->mode.HDisplay,
            crtc->mode.VDisplay,
            crtc->desiredMode.HDisplay,
            crtc->desiredMode.VDisplay);
    } else {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
            "LCD1 crtc mode = <%dx%d>, desired mode <%dx%d>\n",
            crtc->mode.HDisplay,
            crtc->mode.VDisplay,
            crtc->desiredMode.HDisplay,
            crtc->desiredMode.VDisplay);
    }
    mrvl_crtc_set_pos(crtc, x, y);   
    pDev->rotation = crtc->rotation;
}

static void
mrvl_crtc_mode_commit(xf86CrtcPtr crtc)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    FBDevRec* pDev = FBDEVPTR(pScrn);

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_mode_commit\n");

    // Reload h/w cursor after mode commit.
    if (pDev->HWcursor && crtc->scrn->pScreen != NULL)
    {
        xf86_reload_cursors(crtc->scrn->pScreen);
    }
}

static void
mrvl_crtc_gamma_set(xf86CrtcPtr crtc, uint16_t *red, uint16_t *green, uint16_t *blue, int size)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_gamma_set\n");
}

static Bool
mrvl_crtc_lock(xf86CrtcPtr crtc)
{
    ScrnInfoPtr pScrn = crtc->scrn;

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_lock\n");    
    
    return TRUE;
}

static void 
mrvl_crtc_unlock(xf86CrtcPtr crtc)
{
    ScrnInfoPtr pScrn = crtc->scrn;  

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_unlock\n");
}

static void
mrvl_crtc_set_cursor_colors(xf86CrtcPtr crtc, int bg, int fg)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    FBDevRec* pDev = FBDEVPTR(pScrn);

    //xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_set_cursor_colors, %d\n", pCrtcPriv->crtc_id);

    if (pDev->HWcursor)
    {
        DovefbSetCursorColors(pScrn, bg, fg, pCrtcPriv->crtc_id);
    }
}

static void
mrvl_crtc_set_cursor_position(xf86CrtcPtr crtc, int x, int y)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    FBDevRec* pDev = FBDEVPTR(pScrn);

    //xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_set_cursor_position, crtc [%d], x %d, y %d\n", pCrtcPriv->crtc_id, x, y);

    if (pDev->HWcursor)
    {
        DovefbSetCursorPosition(pScrn, x, y, pCrtcPriv->crtc_id);
    }
}

static void
mrvl_crtc_show_cursor(xf86CrtcPtr crtc)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    FBDevRec* pDev = FBDEVPTR(pScrn);

    //xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_show_cursor, crtc [%d]\n", pCrtcPriv->crtc_id);

    if (pDev->HWcursor)
    {
        DovefbShowCursor(pScrn, pCrtcPriv->crtc_id);
    }
}

static void 
mrvl_crtc_hide_cursor(xf86CrtcPtr crtc)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    FBDevRec* pDev = FBDEVPTR(pScrn);

    //xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_hide_cursor, crtc [%d]\n", pCrtcPriv->crtc_id);

    if (pDev->HWcursor)
    {
        DovefbHideCursor(pScrn, pCrtcPriv->crtc_id);
    }
}

static void 
mrvl_crtc_load_cursor_argb(xf86CrtcPtr crtc, CARD32 *image)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;
    FBDevRec* pDev = FBDEVPTR(pScrn);

    //xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_load_cursor_argb, crtc [%d][%p]\n", pCrtcPriv->crtc_id, crtc);

    if (pDev->HWcursor)
    {
        DovefbLoadCursorARGB(pScrn, image, pCrtcPriv->crtc_id);
    }

#if CRTC_DUMP_RAW_VIDEO
    VIVDumpImage(g_dumpEXAFileName,
                 &g_dumpEXAFrameIndex, 
                 image, 
                 64, 
                 64,
                 32);
#endif

}

#if 0
static void
mrvl_crtc_panning_frame(xf86CrtcPtr crtc)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    MRVLGetPrivateByScrn(pScrn);
    MRVLCrtcPrivatePtr pCrtcPriv = (MRVLCrtcPrivatePtr)crtc->driver_private;

    volatile unsigned int *ptr_lcd = 0;
    volatile unsigned int *ptr_tv = 0;
    volatile unsigned int *ptr_TVDMA = 0;
    unsigned int tv_base;
    unsigned int tv_offset;
    unsigned int lcd_base;

    ptr_lcd = (unsigned int*)(pDev->mmio[pCrtcPriv->crtc_id]+0xf4); 
    ptr_tv = (unsigned int*)(pDev->mmio[pCrtcPriv->crtc_id]+0x34);
    lcd_base = *ptr_lcd & 0xffff0000;
    tv_base = *ptr_tv & 0xffff0000;
    tv_offset = *ptr_tv & 0xffff;
    *ptr_tv = (lcd_base | tv_offset);
    ptr_TVDMA = (unsigned int*)(pDev->mmio[pCrtcPriv->crtc_id]+0x80);
    *ptr_TVDMA |= 0x100;

}
#endif

static void
mrvl_crtc_set_origin(xf86CrtcPtr crtc, int x, int y)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "weizhang entering into mrvl_crtc_set_origin\n");
    mrvl_crtc_set_pos(crtc, x, y);   
#if 0
    mrvl_crtc_panning_frame(crtc);
#endif
} 

static void mrvl_crtc_save(xf86CrtcPtr         crtc)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_save\n");
}

static void mrvl_crtc_restore(xf86CrtcPtr      crtc)
{
    ScrnInfoPtr pScrn = crtc->scrn;
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "entering into mrvl_crtc_restore\n");
}

static xf86CrtcFuncsRec mrvl_crtc_funcs = {
    .dpms = mrvl_crtc_dpms,
    .save = mrvl_crtc_save, /* XXX */
    .restore = mrvl_crtc_restore, /* XXX */
    .mode_fixup = mrvl_crtc_mode_fixup,
    .prepare = mrvl_crtc_mode_prepare,
    .mode_set = mrvl_crtc_mode_set,
    .commit = mrvl_crtc_mode_commit,
    .gamma_set = mrvl_crtc_gamma_set,
    .lock = mrvl_crtc_lock,
    .unlock = mrvl_crtc_unlock,
#if MRVL_CRTC_SUPPORT_ROTATION == 0
    .shadow_create = NULL,
    .shadow_allocate = NULL,
    .shadow_destroy = NULL,
#else
    .shadow_create = mrvl_crtc_shadow_create,
    .shadow_allocate = mrvl_crtc_shadow_allocate,
    .shadow_destroy = mrvl_crtc_shadow_destroy,
#endif
    .set_cursor_colors = mrvl_crtc_set_cursor_colors,
    .set_cursor_position = mrvl_crtc_set_cursor_position,
    .show_cursor = mrvl_crtc_show_cursor,
    .hide_cursor = mrvl_crtc_hide_cursor,
    .load_cursor_argb = mrvl_crtc_load_cursor_argb,
    .destroy = NULL, /* XXX */
#if XF86_CRTC_VERSION >= 2
    .set_origin = mrvl_crtc_set_origin,
#endif
};




Bool mrvlCrtcInit(ScrnInfoPtr pScrn)
{
    FBDevRec* pDev = FBDEVPTR(pScrn);
    int i, fd, err;
    struct fb_var_screeninfo var;
    int crtc_num = 0;
#if MRVL_RANDR_EDID_MODES
    struct _sEdidInfo edid_info;
#endif

    crtc_num = 0;
    for( i = 0; i < MRVL_MAX_CRTC_COUNT; i++) {
#if MRVL_PLATFORM_INFO==MRVL_DOVE
        pDev->gfx_fd[i] = open( (i ? GFX_DEV1:GFX_DEV0), O_RDWR);
#elif MRVL_PLATFORM_INFO==MRVL_MMP2
        pDev->gfx_fd[i] = open( (i ? MMP2_TV_GFX:MMP2_GFX), O_RDWR);
#endif
        pDev->pCrtc[i] = 0;
    
        if(pDev->gfx_fd[i] >= 0) 
            crtc_num++;
    }

    pDev->crtc_num = crtc_num;

    if (crtc_num == 0) {
        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvlCrtcInit: Can not find any fb device.\n");
        return FALSE;
    } else {
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvlCrtcInit: Found %d fb devices\n", crtc_num);
    }

    for( i=0; i < crtc_num; i++) {

        fd = pDev->gfx_fd[i];
    
        if(fd < 0) {
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvlCrtcInit: fd is not valid.\n" );
            break;
        }

        /*get "var" screeninfo*/
        err = ioctl(fd, FBIOGET_VSCREENINFO, &var);
        if (err) {
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvlCrtcInit: Can not get fb vscreen info\n");
            return FALSE;
        }
#if MRVL_PLATFORM_INFO==MRVL_DOVE
        var.xres_virtual = pScrn->virtualX;
#if MRVL_CRTC_SUPPORT_ROTATION == 0
        var.yres_virtual = pScrn->virtualY;
#else
        var.yres_virtual = pScrn->virtualY * (crtc_num + 1);
#endif
        var.bits_per_pixel = pScrn->bitsPerPixel;
#endif

//        var.xres_virtual = 3200;

        var.activate = FB_ACTIVATE_NOW;

        if (ioctl(fd, FBIOPUT_VSCREENINFO, &var) == -1) {
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvlCrtcInit: Can not put fb vscreen info\n");
            return FALSE;
        }

        /*get "var" screeninfo*/
        err = ioctl(fd, FBIOGET_VSCREENINFO, &var);
        if (err) {
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "mrvlCrtcInit: Can not get fb vscreen info\n");
            return FALSE;
        }

        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvlCrtcInit: crtc [%d], lcd virtual x %d, y %d\n",
            i, var.xres_virtual, var.yres_virtual);

        // Reserve initial virtual X and Y for randr shadow buffer.
        pDev->controller[i].configVirtualX = pScrn->virtualX;
        pDev->controller[i].configVirtualY = pScrn->virtualY;

#if MRVL_RANDR_EDID_MODES
       //xniu ioctl(fd, DOVEFB_IOCTL_GET_EDID_INFO, &edid_info);
       //xniu pDev->controller[i].output_connected = edid_info.connect;
	pDev->controller[i].output_connected = 1;//xniu 
#endif

        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvlCrtcInit: create crtc%d.\n", i);
	/* Create crtc resource. */
        pDev->pCrtc[i] = xf86CrtcCreate(pScrn, &mrvl_crtc_funcs);

        if (!pDev->pCrtc[i])
        {
            xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Can not create crtc LCD%d\n", i);
            return FALSE;    
        }
    
        pDev->pCrtc[i]->driver_private    = (void *)&pDev->controller[i];
        pDev->controller[i].crtc_id       = i;
        pDev->controller[i].crtc_num      = crtc_num;
        xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvlCrtcInit: crtc%d has been initialized.\n", i);

    }
//weizhang add
    FBDevPtr fPtr;
    fPtr = FBDEVPTR(pScrn);
    fPtr->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "mrvlCrtcInit: completed.\n");

    return TRUE;
}