Merge branch 'master' of git://git.denx.de/u-boot
[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/clk.h>
30 #include <asm/arch/cpu.h>
31 #include <asm/arch/regs-fb.h>
32 #include <asm/arch/gpio.h>
33 #include "s5p-fb.h"
34
35 /* DISPLAY CONTROL REGISTER */
36 #define DCR             0xE0107008
37
38 /* CLOCK DIVIDER 0 */
39 #define CLK_DIV0        0xE0100300
40 #define CLK_DIV1        0xE0100304
41
42 /* LCD CONTROLLER REGISTER BASE */
43 #define S5PC100_LCRB            0xEE000000
44 #define S5PC110_LCRB            0xF8000000
45
46 #define MPLL 1
47
48 #define S5P_VFRAME_FREQ         60
49
50 static unsigned int ctrl_base;
51 static unsigned long *lcd_base_addr;
52 static vidinfo_t *pvid = NULL;
53
54 void s5pc_fimd_lcd_init_mem(u_long screen_base, u_long fb_size, u_long palette_size)
55 {
56         lcd_base_addr = (unsigned long *)screen_base;
57
58         udebug("lcd_base_addr(framebuffer memory) = %x\n", lcd_base_addr);
59
60         return;
61 }
62
63 void s5pc_c100_gpio_setup(void)
64 {
65 }
66
67 void s5pc_c110_gpio_setup(void)
68 {
69         unsigned int i;
70         struct s5pc110_gpio *gpio = (struct s5pc110_gpio *) S5PC110_GPIO_BASE;
71
72         for (i = 0; i < 8; i++) {
73                 /* set GPF0,1,2[0:7] for RGB Interface and Data lines (32bit) */
74                 gpio_cfg_pin(&gpio->gpio_f0, i, GPIO_FUNC(2));
75                 gpio_cfg_pin(&gpio->gpio_f1, i, GPIO_FUNC(2));
76                 gpio_cfg_pin(&gpio->gpio_f2, i, GPIO_FUNC(2));
77                 /* pull-up/down disable */
78                 gpio_set_pull(&gpio->gpio_f0, i, GPIO_PULL_NONE);
79                 gpio_set_pull(&gpio->gpio_f1, i, GPIO_PULL_NONE);
80                 gpio_set_pull(&gpio->gpio_f2, i, GPIO_PULL_NONE);
81
82                 /* drive strength to max (24bit) */
83                 gpio_set_drv(&gpio->gpio_f0, i, GPIO_DRV_4x);
84                 gpio_set_rate(&gpio->gpio_f0, i, GPIO_DRV_SLOW);
85                 gpio_set_drv(&gpio->gpio_f1, i, GPIO_DRV_4x);
86                 gpio_set_rate(&gpio->gpio_f1, i, GPIO_DRV_SLOW);
87                 gpio_set_drv(&gpio->gpio_f2, i, GPIO_DRV_4x);
88                 gpio_set_rate(&gpio->gpio_f2, i, GPIO_DRV_SLOW);
89         }
90
91         for (i =0; i < 4; i++) {
92                 /* set GPF3[0:3] for RGB Interface and Data lines (32bit) */
93                 gpio_cfg_pin(&gpio->gpio_f3, i, GPIO_PULL_UP);
94                 /* pull-up/down disable */
95                 gpio_set_pull(&gpio->gpio_f3, i, GPIO_PULL_NONE);
96                 /* drive strength to max (24bit) */
97                 gpio_set_drv(&gpio->gpio_f3, i, GPIO_DRV_4x);
98                 gpio_set_rate(&gpio->gpio_f3, i, GPIO_DRV_SLOW);
99         }
100         /* display output path selection (only [1:0] valid) */
101         writel(0x2, DCR);
102
103         /* gpio pad configuration for LCD reset. */
104         gpio_cfg_pin(&gpio->gpio_mp0_5, 5, GPIO_OUTPUT);
105
106         /* gpio pad configuration for LCD ON. */
107         gpio_cfg_pin(&gpio->gpio_j1, 3, GPIO_OUTPUT);
108
109         /* gpio pad configuration for DISPLAY_CS, DISPLAY_CLK, DISPLAY_SO, DISPLAY_SI. */
110         gpio_cfg_pin(&gpio->gpio_mp0_1, 1, GPIO_OUTPUT);
111         gpio_cfg_pin(&gpio->gpio_mp0_4, 1, GPIO_OUTPUT);
112         gpio_cfg_pin(&gpio->gpio_mp0_4, 2, GPIO_INPUT);
113         gpio_cfg_pin(&gpio->gpio_mp0_4, 3, GPIO_OUTPUT);
114
115         s5pc1xx_clock_init();
116
117         return;
118 }
119
120 static void s5pc_fimd_set_par(unsigned int win_id)
121 {
122         unsigned int cfg = 0;
123
124         /* set window control */
125         cfg = readl(ctrl_base + S5P_WINCON(win_id));
126
127         cfg &= ~(S5P_WINCON_BITSWP_ENABLE | S5P_WINCON_BYTESWP_ENABLE | \
128                 S5P_WINCON_HAWSWP_ENABLE | S5P_WINCON_WSWP_ENABLE | \
129                 S5P_WINCON_BURSTLEN_MASK | S5P_WINCON_BPPMODE_MASK | \
130                 S5P_WINCON_INRGB_MASK | S5P_WINCON_DATAPATH_MASK);
131
132         /* DATAPATH is DMA */
133         cfg |= S5P_WINCON_DATAPATH_DMA;
134
135         /* bpp is 32 */
136         cfg |= S5P_WINCON_WSWP_ENABLE;
137
138         /* dma burst is 16 */
139         cfg |= S5P_WINCON_BURSTLEN_16WORD;
140
141         /* pixel format is unpacked RGB888 */
142         cfg |= S5P_WINCON_BPPMODE_24BPP_888;
143
144         writel(cfg, ctrl_base + S5P_WINCON(win_id));
145         udebug("wincon%d = %x\n", win_id, cfg);
146
147         /* set window position to x=0, y=0*/
148         cfg = S5P_VIDOSD_LEFT_X(0) | S5P_VIDOSD_TOP_Y(0);
149         writel(cfg, ctrl_base + S5P_VIDOSD_A(win_id));
150         udebug("window postion left,top = %x\n", cfg);
151
152         cfg = S5P_VIDOSD_RIGHT_X(pvid->vl_col - 1) |
153                 S5P_VIDOSD_BOTTOM_Y(pvid->vl_row - 1);
154         writel(cfg, ctrl_base + S5P_VIDOSD_B(win_id));
155         udebug("window postion right,bottom= %x\n", cfg);
156
157         /* set window size for window0*/
158         cfg = S5P_VIDOSD_SIZE(pvid->vl_col * pvid->vl_row);
159         writel(cfg, ctrl_base + S5P_VIDOSD_C(win_id));
160         udebug("vidosd_c%d= %x\n", win_id, cfg);
161
162         return;
163 }
164
165 static void s5pc_fimd_set_buffer_address(unsigned int win_id)
166 {
167         unsigned long start_addr, end_addr;
168
169         start_addr = (unsigned long)lcd_base_addr;
170         end_addr = start_addr + ((pvid->vl_col * (pvid->vl_bpix / 8))
171                 * pvid->vl_row);
172
173         writel(start_addr, ctrl_base + S5P_VIDADDR_START0(win_id));
174         writel(end_addr, ctrl_base + S5P_VIDADDR_END0(win_id));
175
176         udebug("start addr = %x, end addr = %x\n", start_addr, end_addr);
177
178         return;
179 }
180
181 static void s5pc_fimd_set_clock(void)
182 {
183         unsigned int cfg = 0, div = 0, mpll_ratio = 0;
184         unsigned long pixel_clock, src_clock, max_clock;
185
186         max_clock = 66 * 1000000;
187
188         pixel_clock = S5P_VFRAME_FREQ * (pvid->vl_hpw + pvid->vl_blw +
189                 pvid->vl_elw + pvid->vl_width) * (pvid->vl_vpw +
190                     pvid->vl_bfw + pvid->vl_efw + pvid->vl_height);
191
192         if (get_pll_clk == NULL) {
193                 printf("get_pll_clk is null.\n");
194                 return;
195         }
196         src_clock = get_pll_clk(MPLL);
197
198         cfg = readl(ctrl_base + S5P_VIDCON0);
199         cfg &= ~(S5P_VIDCON0_CLKSEL_MASK | S5P_VIDCON0_CLKVALUP_MASK | \
200                 S5P_VIDCON0_VCLKEN_MASK | S5P_VIDCON0_CLKDIR_MASK);
201         cfg |= (S5P_VIDCON0_CLKSEL_HCLK | S5P_VIDCON0_CLKVALUP_ALWAYS | \
202                 S5P_VIDCON0_VCLKEN_NORMAL | S5P_VIDCON0_CLKDIR_DIVIDED);
203
204         if (pixel_clock > max_clock)
205                 pixel_clock = max_clock;
206
207         /* get mpll ratio */
208         if (cpu_is_s5pc110())
209                 mpll_ratio = (readl(CLK_DIV0) & 0xf0000) >> 16;
210         else
211                 mpll_ratio = (readl(CLK_DIV1) & 0xf0) >> 4;
212
213         /* 
214          * It can get source clock speed as (mpll / mpll_ratio) 
215          * because lcd controller uses hclk_dsys.
216          * mpll is a parent of hclk_dsys.
217          */
218         div = (unsigned int)((src_clock / (mpll_ratio + 1)) / pixel_clock);
219         cfg |= S5P_VIDCON0_CLKVAL_F(div - 1);
220         writel(cfg, ctrl_base + S5P_VIDCON0);
221
222         udebug("mpll_ratio = %d, src_clock = %d, pixel_clock = %d, div = %d\n",
223                 mpll_ratio, src_clock, pixel_clock, div);
224
225         return;
226 }
227
228 static void s5pc_fimd_lcd_on(unsigned int win_id)
229 {
230         unsigned int cfg = 0;
231
232         /* display on */
233         cfg = readl(ctrl_base + S5P_VIDCON0);
234         cfg |= (S5P_VIDCON0_ENVID_ENABLE | S5P_VIDCON0_ENVID_F_ENABLE);
235         writel(cfg, ctrl_base + S5P_VIDCON0);
236         udebug("vidcon0 = %x\n", cfg);
237
238         /* enable window */
239         cfg = readl(ctrl_base + S5P_WINCON(win_id));
240         cfg |= S5P_WINCON_ENWIN_ENABLE;
241         writel(cfg, ctrl_base + S5P_WINCON(win_id));
242         udebug("wincon%d=%x\n", win_id, cfg);
243 }
244
245 void s5pc_fimc_lcd_off(unsigned int win_id)
246 {
247         unsigned int cfg = 0;
248
249         cfg = readl(ctrl_base + S5P_VIDCON0);
250         cfg &= (S5P_VIDCON0_ENVID_DISABLE | S5P_VIDCON0_ENVID_F_DISABLE);
251         writel(cfg, ctrl_base + S5P_VIDCON0);
252
253         cfg = readl(ctrl_base + S5P_WINCON(win_id));
254         cfg &= S5P_WINCON_ENWIN_DISABLE;
255         writel(cfg, ctrl_base + S5P_WINCON(win_id));
256 }
257
258
259 void s5pc_fimd_lcd_init(vidinfo_t *vid)
260 {
261         unsigned int cfg = 0, rgb_mode, win_id = 1;
262
263         /* store panel info to global variable */
264         pvid = vid;
265
266         /* select register base according to cpu type */
267         if (cpu_is_s5pc110())
268                 ctrl_base = S5PC110_LCRB;
269         else
270                 ctrl_base = S5PC100_LCRB;
271
272         /* set output to RGB */
273         rgb_mode = MODE_RGB_P;
274         cfg = readl(ctrl_base + S5P_VIDCON0);
275         cfg &= ~S5P_VIDCON0_VIDOUT_MASK;
276
277         /* clock source is HCLK */
278         cfg |= 0 << 2;
279
280         cfg |= S5P_VIDCON0_VIDOUT_RGB;
281         writel(cfg, ctrl_base + S5P_VIDCON0);
282
283         /* set display mode */
284         cfg = readl(ctrl_base + S5P_VIDCON0);
285         cfg &= ~S5P_VIDCON0_PNRMODE_MASK;
286         cfg |= (rgb_mode << S5P_VIDCON0_PNRMODE_SHIFT);
287         writel(cfg, ctrl_base + S5P_VIDCON0);
288
289         /* set polarity */
290         cfg = 0;
291         if (!pvid->vl_clkp)
292                 cfg |= S5P_VIDCON1_IVCLK_RISING_EDGE;
293         if (!pvid->vl_hsp)
294                 cfg |= S5P_VIDCON1_IHSYNC_INVERT;
295         if (!pvid->vl_vsp)
296                 cfg |= S5P_VIDCON1_IVSYNC_INVERT;
297         if (!pvid->vl_dp)
298                 cfg |= S5P_VIDCON1_IVDEN_INVERT;
299
300         writel(cfg, ctrl_base + S5P_VIDCON1);
301
302         /* set timing */
303         cfg = 0;
304         cfg |= S5P_VIDTCON0_VBPD(pvid->vl_bfw - 1);
305         cfg |= S5P_VIDTCON0_VFPD(pvid->vl_efw - 1);
306         cfg |= S5P_VIDTCON0_VSPW(pvid->vl_vpw - 1);
307         writel(cfg, ctrl_base + S5P_VIDTCON0);
308         udebug("vidtcon0 = %x\n", cfg);
309
310         cfg = 0;
311         cfg |= S5P_VIDTCON1_HBPD(pvid->vl_blw - 1);
312         cfg |= S5P_VIDTCON1_HFPD(pvid->vl_elw - 1);
313         cfg |= S5P_VIDTCON1_HSPW(pvid->vl_hpw - 1);
314
315         writel(cfg, ctrl_base + S5P_VIDTCON1);
316         udebug("vidtcon1 = %x\n", cfg);
317
318         /* set lcd size */
319         cfg = 0;
320         cfg |= S5P_VIDTCON2_HOZVAL(pvid->vl_col - 1);
321         cfg |= S5P_VIDTCON2_LINEVAL(pvid->vl_row - 1);
322
323         writel(cfg, ctrl_base + S5P_VIDTCON2);
324         udebug("vidtcon2 = %x\n", cfg);
325
326         /* set par */
327         s5pc_fimd_set_par(win_id);
328
329         /* set memory address */
330         s5pc_fimd_set_buffer_address(win_id);
331
332         /* set buffer size */
333         cfg = S5P_VIDADDR_PAGEWIDTH(pvid->vl_col * pvid->vl_bpix / 8);
334         writel(cfg, ctrl_base + S5P_VIDADDR_SIZE(win_id));
335         udebug("vidaddr_pagewidth = %d\n", cfg);
336
337         /* set clock */
338         s5pc_fimd_set_clock();
339
340         /* display on */
341         s5pc_fimd_lcd_on(win_id);
342
343         udebug("lcd controller init completed.\n");
344
345         return;
346 }
347
348 ulong s5pc_fimd_calc_fbsize(void)
349 {
350         return (pvid->vl_col * pvid->vl_row * (pvid->vl_bpix / 8));
351 }