Merge branch 'master' of daeinki@10.89.8.158:/pub/git/u-boot-s5pc1xx
[kernel/u-boot.git] / drivers / video / s5p-fimd.c
1 /*
2  * S5PC100 and S5PC110 LCD Controller Specific driver.
3  *
4  * Author: InKi Dae <inki.dae@samsung.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21
22 #include <config.h>
23 #include <common.h>
24 #include <stdarg.h>
25 #include <linux/types.h>
26 #include <asm/io.h>
27 #include <lcd.h>
28
29 #include <asm/arch/cpu.h>
30 #include <asm/arch/regs-fb.h>
31 #include <asm/arch/hardware.h>
32 #include "s5p-fb.h"
33
34 #define MPLL 1
35
36 /* LCD Panel definitions */
37 #define PANEL_WIDTH             480
38 #define PANEL_HEIGHT            800
39 #define S5P_LCD_BPP             32      
40
41 #define S5PCFB_VBPE             1
42
43 #define S5PCFB_VFPE             1
44
45 #define S5PCFB_HRES             480
46 #define S5PCFB_VRES             800
47
48 #define S5PCFB_HRES_VIRTUAL     480
49 #define S5PCFB_VRES_VIRTUAL     800
50
51 #define S5PCFB_HRES_OSD         480
52 #define S5PCFB_VRES_OSD         800
53
54 #define S5P_VFRAME_FREQ         60
55
56 static unsigned int ctrl_base;
57 static unsigned long *lcd_base_addr;
58
59 extern unsigned long get_pll_clk(int pllreg);
60
61 void s5pc_fimd_lcd_init_mem(u_long screen_base, u_long fb_size, u_long palette_size)
62 {
63         lcd_base_addr = (unsigned long *)screen_base;
64
65         udebug("lcd_base_addr(framebuffer memory) = %x\n", lcd_base_addr);
66
67         return;
68 }
69
70 void s5pc_c100_gpio_setup(void)
71 {
72         /* set GPF0[0:7] for RGB Interface and Data lines */
73         writel(0x22222222, 0xE03000E0);
74
75         /* set Data lines */
76         writel(0x22222222, 0xE0300100);
77         writel(0x22222222, 0xE0300120);
78         writel(0x2222, 0xE0300140);
79
80         /* set gpio configuration pin for MLCD_RST */
81         writel(0x10000000, 0xE0300C20);
82
83         /* set gpio configuration pin for MLCD_ON */
84         writel(0x1000, 0xE0300220);
85         writel(readl(0xE0300224) & 0xf7, 0xE0300224);
86
87         /* set gpio configuration pin for DISPLAY_CS, DISPLAY_CLK and DISPLSY_SI */
88         writel(0x11100000, 0xE0300300);
89 }
90
91 void s5pc_c110_gpio_setup(void)
92 {
93         /* set GPF0[0:7] for RGB Interface and Data lines */
94         writel(0x22222222, 0xE0200120);
95         /* pull-up/down disable */
96         writel(0x0, 0xE0200128);
97         /* drive strength to max */
98         writel(0xffffffff, 0xE020012C);
99
100         /* set Data lines */
101         writel(0x22222222, 0xE0200140);
102         writel(0x22222222, 0xE0200160);
103         writel(readl(0xE0200180) & 0xFF0000, 0xE0200180);
104         writel(readl(0xE0200180) | 0x002222, 0xE0200180);
105
106         /* drive strength to max */
107         writel(0xffffffff, 0xE020014C);
108         writel(0xffffffff, 0xE020016C);
109         writel(readl(0xE020018C) & 0xFF00, 0xE020018C);
110         writel(readl(0xE020018C) | 0x00FF, 0xE020018C);
111
112         /* pull-up/down disable */
113         writel(0x0, 0xE0200148);
114         writel(0x0, 0xE0200168);
115         writel(0x0, 0xE0200188);
116
117         /* display output path selection */
118         writel(0x2, 0xE0107008);
119
120         /* set gpio configuration pin for MLCD_RST */
121         writel(readl(0xE0200C20) & 0x0fffffff, 0xE0200C20);
122         writel(readl(0xE0200C20) | 0x10000000, 0xE0200C20);
123
124         /* set gpio configuration pin for MLCD_ON */
125         writel(readl(0xE0200260) & 0xFFFF0FFF, 0xE0200260);
126         writel(readl(0xE0200260) | 0x00001000, 0xE0200260);
127         writel(readl(0xE0200264) & 0xf7, 0xE0200264);
128
129         /* set gpio configuration pin for DISPLAY_CS, DISPLAY_CLK, DISPLSY_SI and LCD_ID */
130         writel(readl(0xE02002E0) & 0xFFFFFF0F, 0xE02002E0);
131         writel(readl(0xE02002E0) | 0x00000010, 0xE02002E0);
132         writel(readl(0xE0200340) & 0xFFFF000F, 0xE0200340);
133         writel(readl(0xE0200340) | 0x00001110, 0xE0200340);
134
135         return;
136 }
137
138 static void s5pc_fimd_set_par(unsigned int win_id)
139 {
140         unsigned int cfg = 0;
141
142         /* set window control */
143         cfg = readl(ctrl_base + S5P_WINCON(win_id));
144
145         cfg &= ~(S5P_WINCON_BITSWP_ENABLE | S5P_WINCON_BYTESWP_ENABLE | \
146                 S5P_WINCON_HAWSWP_ENABLE | S5P_WINCON_WSWP_ENABLE | \
147                 S5P_WINCON_BURSTLEN_MASK | S5P_WINCON_BPPMODE_MASK | \
148                 S5P_WINCON_INRGB_MASK | S5P_WINCON_DATAPATH_MASK);
149
150         /* DATAPATH is DMA */
151         cfg |= S5P_WINCON_DATAPATH_DMA;
152
153         /* bpp is 32 */
154         cfg |= S5P_WINCON_WSWP_ENABLE;
155
156         /* dma burst is 16 */
157         cfg |= S5P_WINCON_BURSTLEN_16WORD;
158
159         /* pixel format is unpacked RGB888 */
160         cfg |= S5P_WINCON_BPPMODE_24BPP_888;
161
162         writel(cfg, ctrl_base + S5P_WINCON(win_id));
163         udebug("wincon%d = %x\n", win_id, cfg);
164
165         /* set window position to x=0, y=0*/
166         cfg = S5P_VIDOSD_LEFT_X(0) | S5P_VIDOSD_TOP_Y(0);
167         writel(cfg, ctrl_base + S5P_VIDOSD_A(win_id));
168         udebug("window postion left,top = %x\n", cfg);
169
170         cfg = S5P_VIDOSD_RIGHT_X(PANEL_WIDTH - 1) |
171                 S5P_VIDOSD_BOTTOM_Y(PANEL_HEIGHT - 1);
172         writel(cfg, ctrl_base + S5P_VIDOSD_B(win_id));
173         udebug("window postion right,bottom= %x\n", cfg);
174
175         /* set window size for window0*/
176         cfg = S5P_VIDOSD_SIZE(PANEL_WIDTH * PANEL_HEIGHT);
177         writel(cfg, ctrl_base + S5P_VIDOSD_C(win_id));
178         udebug("vidosd_c%d= %x\n", win_id, cfg);
179
180         return;
181 }
182
183 static void s5pc_fimd_set_buffer_address(unsigned int win_id)
184 {
185         unsigned long start_addr, end_addr;
186
187         start_addr = (unsigned long)lcd_base_addr;
188         end_addr = start_addr + ((PANEL_WIDTH * (S5P_LCD_BPP / 8))
189                 * PANEL_HEIGHT);
190
191         writel(start_addr, ctrl_base + S5P_VIDADDR_START0(win_id));
192         writel(end_addr, ctrl_base + S5P_VIDADDR_END0(win_id));
193
194         udebug("start addr = %x, end addr = %x\n", start_addr, end_addr);
195
196         return;
197 }
198
199 static void s5pc_fimd_set_clock(vidinfo_t *vid)
200 {
201         unsigned int cfg = 0, div = 0, mpll_ratio = 0;
202         unsigned long pixel_clock, src_clock, max_clock;
203
204         max_clock = 66 * 1000000;
205
206         pixel_clock = S5P_VFRAME_FREQ * (vid->vl_hpw + vid->vl_blw + vid->vl_elw + vid->vl_width) *
207                 (vid->vl_vpw + vid->vl_bfw + vid->vl_efw + vid->vl_height);
208
209         src_clock = get_pll_clk(MPLL);
210
211         cfg = readl(ctrl_base + S5P_VIDCON0);
212         cfg &= ~(S5P_VIDCON0_CLKSEL_MASK | S5P_VIDCON0_CLKVALUP_MASK | \
213                 S5P_VIDCON0_VCLKEN_MASK | S5P_VIDCON0_CLKDIR_MASK);
214         cfg |= (S5P_VIDCON0_CLKSEL_HCLK | S5P_VIDCON0_CLKVALUP_ALWAYS | \
215                 S5P_VIDCON0_VCLKEN_NORMAL | S5P_VIDCON0_CLKDIR_DIVIDED);
216
217         if (pixel_clock > max_clock)
218                 pixel_clock = max_clock;
219
220         /* get mpll ratio */
221         if (cpu_is_s5pc110())
222                 mpll_ratio = (readl(0xE0100300) & 0xf0000) >> 16;
223         else
224                 mpll_ratio = (readl(0xE0100304) & 0xf0) >> 4;
225
226         /* 
227          * It can get sorce clock speed as (mpll / mpll_ratio) 
228          * because lcd controller uses hclk_dsys.
229          * mpll is a parent of hclk_dsys.
230          */
231         div = (unsigned int)((src_clock / (mpll_ratio + 1)) / pixel_clock);
232         cfg |= S5P_VIDCON0_CLKVAL_F(div - 1);
233         writel(cfg, ctrl_base + S5P_VIDCON0);
234
235         udebug("mpll_ratio = %d, src_clock = %d, pixel_clock = %d, div = %d\n",
236                 mpll_ratio, src_clock, pixel_clock, div);
237
238         return;
239 }
240
241 void s5pc_fimd_lcd_init(vidinfo_t *vid)
242 {
243         unsigned int cfg = 0, rgb_mode, win_id = 0;
244
245         /* select register base according to cpu type */
246         if (cpu_is_s5pc110())
247                 ctrl_base = 0xF8000000;
248         else
249                 ctrl_base = 0xEE000000;
250
251         /* set output to RGB */
252         rgb_mode = MODE_RGB_P;
253         cfg = readl(ctrl_base + S5P_VIDCON0);   
254         cfg &= ~S5P_VIDCON0_VIDOUT_MASK;
255
256         /* clock source is HCLK */
257         cfg |= 0 << 2;
258
259         cfg |= S5P_VIDCON0_VIDOUT_RGB;
260         writel(cfg, ctrl_base + S5P_VIDCON0);
261
262         /* set display mode */
263         cfg = readl(ctrl_base + S5P_VIDCON0);
264         cfg &= ~S5P_VIDCON0_PNRMODE_MASK;
265         cfg |= (rgb_mode << S5P_VIDCON0_PNRMODE_SHIFT);
266         writel(cfg, ctrl_base + S5P_VIDCON0);
267
268         /* set polarity */
269         cfg = 0;
270         cfg |= S5P_VIDCON1_IVDEN_INVERT | S5P_VIDCON1_IVCLK_RISING_EDGE;
271         writel(cfg, ctrl_base + S5P_VIDCON1);
272
273
274         /* set timing */
275         cfg = 0;
276         //cfg |= S5P_VIDTCON0_VBPDE(S5PCFB_VBPE - 1);
277         cfg |= S5P_VIDTCON0_VBPD(vid->vl_bfw - 1);
278         cfg |= S5P_VIDTCON0_VFPD(vid->vl_efw - 1);
279         cfg |= S5P_VIDTCON0_VSPW(vid->vl_vpw - 1);
280         writel(cfg, ctrl_base + S5P_VIDTCON0);
281         udebug("vidtcon0 = %x\n", cfg);
282
283         cfg = 0;
284         //cfg |= S5P_VIDTCON1_VFPDE(S5PCFB_VFPE - 1);
285         cfg |= S5P_VIDTCON1_HBPD(vid->vl_blw - 1);
286         cfg |= S5P_VIDTCON1_HFPD(vid->vl_elw - 1);
287         cfg |= S5P_VIDTCON1_HSPW(vid->vl_hpw - 1);
288
289         writel(cfg, ctrl_base + S5P_VIDTCON1);
290         udebug("vidtcon1 = %x\n", cfg);
291
292         /* set lcd size */
293         cfg = 0;
294         cfg |= S5P_VIDTCON2_HOZVAL(PANEL_WIDTH - 1);
295         cfg |= S5P_VIDTCON2_LINEVAL(PANEL_HEIGHT - 1);
296         
297         writel(cfg, ctrl_base + S5P_VIDTCON2);
298         udebug("vidtcon2 = %x\n", cfg);
299
300         /* set par */
301         s5pc_fimd_set_par(win_id);
302
303         /* set memory address */
304         s5pc_fimd_set_buffer_address(win_id);
305
306         /* set buffer size */
307         cfg = S5P_VIDADDR_PAGEWIDTH(PANEL_WIDTH * S5P_LCD_BPP / 8);
308         writel(cfg, ctrl_base + S5P_VIDADDR_SIZE(win_id));
309         udebug("vidaddr_pagewidth = %d\n", cfg);
310
311         /* set clock */
312         s5pc_fimd_set_clock(vid);
313
314         /* display on */
315         cfg = readl(ctrl_base + S5P_VIDCON0);
316         cfg |= (S5P_VIDCON0_ENVID_ENABLE | S5P_VIDCON0_ENVID_F_ENABLE);
317         writel(cfg, ctrl_base + S5P_VIDCON0);
318         udebug("vidcon0 = %x\n", cfg);
319
320         /* enable window */
321         cfg = readl(ctrl_base + S5P_WINCON(win_id));
322         cfg |= S5P_WINCON_ENWIN_ENABLE;
323         writel(cfg, ctrl_base + S5P_WINCON(win_id));
324         udebug("wincon%d=%x\n", win_id, cfg);
325
326         udebug("lcd controller init completed.\n");
327
328         return;
329 }
330
331 ulong s5pc_fimd_calc_fbsize(void)
332 {
333         return (PANEL_WIDTH * PANEL_HEIGHT * (S5P_LCD_BPP / 8));
334 }