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