display_controller.c 7.3 KB
Newer Older
M
Ming, Bai 已提交
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
M
Ming, Bai 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
M
Ming, Bai 已提交
5 6 7 8 9 10 11 12 13 14 15
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-08-09     lgnq         first version for LS1B DC
 */

#include <rtthread.h>
#include "display_controller.h"

struct vga_struct vga_mode[] =
{
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    {/*"640x480_70.00"*/    28560,  640,    664,    728,    816,    480,    481,    484,    500,    },
    {/*"640x640_60.00"*/    33100,  640,    672,    736,    832,    640,    641,    644,    663,    },
    {/*"640x768_60.00"*/    39690,  640,    672,    736,    832,    768,    769,    772,    795,    },
    {/*"640x800_60.00"*/    42130,  640,    680,    744,    848,    800,    801,    804,    828,    },
    {/*"800x480_70.00"*/    35840,  800,    832,    912,    1024,   480,    481,    484,    500,    },
    {/*"800x600_60.00"*/    38220,  800,    832,    912,    1024,   600,    601,    604,    622,    },
    {/*"800x640_60.00"*/    40730,  800,    832,    912,    1024,   640,    641,    644,    663,    },
    {/*"832x600_60.00"*/    40010,  832,    864,    952,    1072,   600,    601,    604,    622,    },
    {/*"832x608_60.00"*/    40520,  832,    864,    952,    1072,   608,    609,    612,    630,    },
    {/*"1024x480_60.00"*/   38170,  1024,   1048,   1152,   1280,   480,    481,    484,    497,    },
    {/*"1024x600_60.00"*/   48960,  1024,   1064,   1168,   1312,   600,    601,    604,    622,    },
    {/*"1024x640_60.00"*/   52830,  1024,   1072,   1176,   1328,   640,    641,    644,    663,    },
    {/*"1024x768_60.00"*/   64110,  1024,   1080,   1184,   1344,   768,    769,    772,    795,    },
    {/*"1152x764_60.00"*/   71380,  1152,   1208,   1328,   1504,   764,    765,    768,    791,    },
    {/*"1280x800_60.00"*/   83460,  1280,   1344,   1480,   1680,   800,    801,    804,    828,    },
    {/*"1280x1024_55.00"*/  98600,  1280,   1352,   1488,   1696,   1024,   1025,   1028,   1057,   },
    {/*"1440x800_60.00"*/   93800,  1440,   1512,   1664,   1888,   800,    801,    804,    828,    },
    {/*"1440x900_67.00"*/   120280, 1440,   1528,   1680,   1920,   900,    901,    904,    935,    },
M
Ming, Bai 已提交
34 35 36 37 38 39 40 41 42 43 44
};

ALIGN(16)
volatile rt_uint16_t _rt_framebuffer[FB_YSIZE][FB_XSIZE];
static struct rt_device_graphic_info _dc_info;

#define abs(x) ((x<0)?(-x):x)
#define min(a,b) ((a<b)?a:b)

int caclulate_freq(long long XIN, long long PCLK)
{
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
    int i;
    long long clk, clk1;
    int start, end;
    int mi;
    int pll,ctrl,div,div1,frac;

    pll = PLL_FREQ;
    ctrl = PLL_DIV_PARAM;
    rt_kprintf("pll=0x%x, ctrl=0x%x\n", pll, ctrl);
//  rt_kprintf("cpu freq is %d\n", tgt_pipefreq());
    start = -1;
    end = 1;

    for (i=start; i<=end; i++)
    {
        clk = (12+i+(pll&0x3f))*33333333/2;
        div = clk/(long)PCLK/1000;
        clk1 = (12+i+1+(pll&0x3f))*33333333/2;
        div1 = clk1/(long)PCLK/1000;
        if (div!=div1)
            break;
    }

    if (div!=div1)
    {
        frac = ((PCLK*1000*div1)*2*1024/33333333 - (12+i+(pll&0x3f))*1024)&0x3ff;
        pll = (pll & ~0x3ff3f)|(frac<<8)|((pll&0x3f)+i);
        ctrl = ctrl&~(0x1f<<26)|(div1<<26)|(1<<31);
    }
    else
    {
        clk = (12+start+(pll&0x3f))*33333333/2;
        clk1 = (12+end+(pll&0x3f))*33333333/2;
        if (abs((long)clk/div/1000-PCLK)<abs((long)clk1/(div+1)/1000-PCLK))
        {
            pll = (pll & ~0x3ff3f)|((pll&0x3f)+start);
            ctrl = ctrl&~(0x1f<<26)|(div<<26)|(1<<31);
        }
        else
        {
            pll = (pll & ~0x3ff3f)|((pll&0x3f)+end);
            ctrl = ctrl&~(0x1f<<26)|((div+1)<<26)|(1<<31);
        }
    }

    rt_kprintf("new pll=0x%x, ctrl=0x%x\n", pll, ctrl);
    ctrl |= 0x2a00;
    PLL_DIV_PARAM = ctrl;
    PLL_FREQ = pll;
    rt_thread_delay(10);
//  initserial(0);
//  _probe_frequencies();
//  rt_kprintf("cpu freq is %d\n",tgt_pipefreq());
    return 0;
M
Ming, Bai 已提交
99 100 101 102
}

static rt_err_t rt_dc_init(rt_device_t dev)
{
103 104 105 106 107 108 109 110
    int i, out, mode=-1;
    int val;

    for (i=0; i<sizeof(vga_mode)/sizeof(struct vga_struct); i++)
    {
        if (vga_mode[i].hr == FB_XSIZE && vga_mode[i].vr == FB_YSIZE)
        {
            mode=i;
M
Ming, Bai 已提交
111
#ifdef LS1FSOC
112 113 114 115 116 117 118 119
//          out = caclulatefreq(APB_CLK/1000,vga_mode[i].pclk);
//          rt_kprintf("out=%x\n",out);
            /*inner gpu dc logic fifo pll ctrl,must large then outclk*/
//          *(volatile int *)0xbfd00414 = out+1;
            /*output pix1 clock  pll ctrl*/
//          *(volatile int *)0xbfd00410 = out;
            /*output pix2 clock pll ctrl */
//          *(volatile int *)0xbfd00424 = out;
M
Ming, Bai 已提交
120
#else
121
            caclulate_freq(APB_CLK/1000, vga_mode[i].pclk);
M
Ming, Bai 已提交
122
#endif
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
            break;
        }
    }

    if (mode<0)
    {
        rt_kprintf("\n\n\nunsupported framebuffer resolution\n\n\n");
        return;
    }

    DC_FB_CONFIG = 0x0;
    DC_FB_CONFIG = 0x3; //  // framebuffer configuration RGB565
    DC_FB_BUFFER_ADDR0 = (rt_uint32_t)_rt_framebuffer - 0x80000000;
    DC_FB_BUFFER_ADDR1 = (rt_uint32_t)_rt_framebuffer - 0x80000000;
    DC_DITHER_CONFIG = 0x0;
    DC_DITHER_TABLE_LOW = 0x0;
    DC_DITHER_TABLE_HIGH = 0x0;
    DC_PANEL_CONFIG = 0x80001311;
    DC_PANEL_TIMING = 0x0;

    DC_HDISPLAY = (vga_mode[mode].hfl<<16) | vga_mode[mode].hr;
    DC_HSYNC = 0x40000000 | (vga_mode[mode].hse<<16) | vga_mode[mode].hss;
    DC_VDISPLAY = (vga_mode[mode].vfl<<16) | vga_mode[mode].vr;
    DC_VSYNC = 0x40000000 | (vga_mode[mode].vse<<16) | vga_mode[mode].vss;
M
Ming, Bai 已提交
147 148

#if defined(CONFIG_VIDEO_32BPP)
149 150
    DC_FB_CONFIG = 0x00100104;
    DC_FB_BUFFER_STRIDE = FB_XSIZE*4;
M
Ming, Bai 已提交
151
#elif defined(CONFIG_VIDEO_16BPP)
152 153
    DC_FB_CONFIG = 0x00100103;
    DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255);
M
Ming, Bai 已提交
154
#elif defined(CONFIG_VIDEO_15BPP)
155 156
    DC_FB_CONFIG = 0x00100102;
    DC_FB_BUFFER_STRIDE = FB_XSIZE*2;
M
Ming, Bai 已提交
157
#elif defined(CONFIG_VIDEO_12BPP)
158 159
    DC_FB_CONFIG = 0x00100101;
    DC_FB_BUFFER_STRIDE = FB_XSIZE*2;
M
Ming, Bai 已提交
160
#else  //640x480-32Bits
161 162
    DC_FB_CONFIG = 0x00100104;
    DC_FB_BUFFER_STRIDE = FB_XSIZE*4;
M
Ming, Bai 已提交
163 164 165
#endif //32Bits

#ifdef LS1GSOC
166 167 168 169 170 171 172 173 174 175 176 177 178 179
    /*fix ls1g dc
     *first switch to tile mode
     *change origin register to 0
     *goback nomal mode
     */
    {
        val = DC_FB_CONFIG;
        DC_FB_CONFIG = val | 0x10;
        DC_FB_BUFFER_ORIGIN = 0;
        DC_FB_BUFFER_ORIGIN;
        rt_thread_delay(10);
        DC_FB_CONFIG;
        DC_FB_CONFIG = val;
    }
M
Ming, Bai 已提交
180 181
#endif

182
    return RT_EOK;
M
Ming, Bai 已提交
183 184
}

B
bernard 已提交
185
static rt_err_t rt_dc_control(rt_device_t dev, int cmd, void *args)
M
Ming, Bai 已提交
186
{
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    switch (cmd)
    {
    case RTGRAPHIC_CTRL_RECT_UPDATE:
        break;
    case RTGRAPHIC_CTRL_POWERON:
        break;
    case RTGRAPHIC_CTRL_POWEROFF:
        break;
    case RTGRAPHIC_CTRL_GET_INFO:
        rt_memcpy(args, &_dc_info, sizeof(_dc_info));
        break;
    case RTGRAPHIC_CTRL_SET_MODE:
        break;
    }

    return RT_EOK;
M
Ming, Bai 已提交
203 204 205 206
}

void rt_hw_dc_init(void)
{
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    rt_device_t dc = rt_malloc(sizeof(struct rt_device));
    if (dc == RT_NULL)
    {
        rt_kprintf("dc == RT_NULL\n");
        return; /* no memory yet */
    }

    _dc_info.bits_per_pixel = 16;
    _dc_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
    _dc_info.framebuffer = (rt_uint8_t*)HW_FB_ADDR;
    _dc_info.width = FB_XSIZE;
    _dc_info.height = FB_YSIZE;

    /* init device structure */
    dc->type = RT_Device_Class_Graphic;
    dc->init = rt_dc_init;
    dc->open = RT_NULL;
    dc->close = RT_NULL;
    dc->control = rt_dc_control;
    dc->user_data = (void*)&_dc_info;

    /* register Display Controller device to RT-Thread */
    rt_device_register(dc, "dc", RT_DEVICE_FLAG_RDWR);
M
Ming, Bai 已提交
230
}