x86: geode: MSR cleanup
[safe/jmp/linux-2.6] / drivers / video / geode / lxfb_ops.c
1 /* Geode LX framebuffer driver
2  *
3  * Copyright (C) 2006-2007, Advanced Micro Devices,Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/fb.h>
14 #include <linux/uaccess.h>
15 #include <linux/delay.h>
16 #include <asm/geode.h>
17
18 #include "lxfb.h"
19
20 /* TODO
21  * Support panel scaling
22  * Add acceleration
23  * Add support for interlacing (TV out)
24  * Support compression
25  */
26
27 /* This is the complete list of PLL frequencies that we can set -
28  * we will choose the closest match to the incoming clock.
29  * freq is the frequency of the dotclock * 1000 (for example,
30  * 24823 = 24.983 Mhz).
31  * pllval is the corresponding PLL value
32 */
33
34 static const struct {
35   unsigned int pllval;
36   unsigned int freq;
37 } pll_table[] = {
38   { 0x000031AC, 24923 },
39   { 0x0000215D, 25175 },
40   { 0x00001087, 27000 },
41   { 0x0000216C, 28322 },
42   { 0x0000218D, 28560 },
43   { 0x000010C9, 31200 },
44   { 0x00003147, 31500 },
45   { 0x000010A7, 33032 },
46   { 0x00002159, 35112 },
47   { 0x00004249, 35500 },
48   { 0x00000057, 36000 },
49   { 0x0000219A, 37889 },
50   { 0x00002158, 39168 },
51   { 0x00000045, 40000 },
52   { 0x00000089, 43163 },
53   { 0x000010E7, 44900 },
54   { 0x00002136, 45720 },
55   { 0x00003207, 49500 },
56   { 0x00002187, 50000 },
57   { 0x00004286, 56250 },
58   { 0x000010E5, 60065 },
59   { 0x00004214, 65000 },
60   { 0x00001105, 68179 },
61   { 0x000031E4, 74250 },
62   { 0x00003183, 75000 },
63   { 0x00004284, 78750 },
64   { 0x00001104, 81600 },
65   { 0x00006363, 94500 },
66   { 0x00005303, 97520 },
67   { 0x00002183, 100187 },
68   { 0x00002122, 101420 },
69   { 0x00001081, 108000 },
70   { 0x00006201, 113310 },
71   { 0x00000041, 119650 },
72   { 0x000041A1, 129600 },
73   { 0x00002182, 133500 },
74   { 0x000041B1, 135000 },
75   { 0x00000051, 144000 },
76   { 0x000041E1, 148500 },
77   { 0x000062D1, 157500 },
78   { 0x000031A1, 162000 },
79   { 0x00000061, 169203 },
80   { 0x00004231, 172800 },
81   { 0x00002151, 175500 },
82   { 0x000052E1, 189000 },
83   { 0x00000071, 192000 },
84   { 0x00003201, 198000 },
85   { 0x00004291, 202500 },
86   { 0x00001101, 204750 },
87   { 0x00007481, 218250 },
88   { 0x00004170, 229500 },
89   { 0x00006210, 234000 },
90   { 0x00003140, 251182 },
91   { 0x00006250, 261000 },
92   { 0x000041C0, 278400 },
93   { 0x00005220, 280640 },
94   { 0x00000050, 288000 },
95   { 0x000041E0, 297000 },
96   { 0x00002130, 320207 }
97 };
98
99
100 static void lx_set_dotpll(u32 pllval)
101 {
102         u32 dotpll_lo, dotpll_hi;
103         int i;
104
105         rdmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
106
107         if ((dotpll_lo & GLCP_DOTPLL_LOCK) && (dotpll_hi == pllval))
108                 return;
109
110         dotpll_hi = pllval;
111         dotpll_lo &= ~(GLCP_DOTPLL_BYPASS | GLCP_DOTPLL_HALFPIX);
112         dotpll_lo |= GLCP_DOTPLL_RESET;
113
114         wrmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
115
116         /* Wait 100us for the PLL to lock */
117
118         udelay(100);
119
120         /* Now, loop for the lock bit */
121
122         for (i = 0; i < 1000; i++) {
123                 rdmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
124                 if (dotpll_lo & GLCP_DOTPLL_LOCK)
125                         break;
126         }
127
128         /* Clear the reset bit */
129
130         dotpll_lo &= ~GLCP_DOTPLL_RESET;
131         wrmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
132 }
133
134 /* Set the clock based on the frequency specified by the current mode */
135
136 static void lx_set_clock(struct fb_info *info)
137 {
138         unsigned int diff, min, best = 0;
139         unsigned int freq, i;
140
141         freq = (unsigned int) (0x3b9aca00 / info->var.pixclock);
142
143         min = abs(pll_table[0].freq - freq);
144
145         for (i = 0; i < ARRAY_SIZE(pll_table); i++) {
146                 diff = abs(pll_table[i].freq - freq);
147                 if (diff < min) {
148                         min = diff;
149                         best = i;
150                 }
151         }
152
153         lx_set_dotpll(pll_table[best].pllval & 0x7FFF);
154 }
155
156 static void lx_graphics_disable(struct fb_info *info)
157 {
158         struct lxfb_par *par = info->par;
159         unsigned int val, gcfg;
160
161         /* Note:  This assumes that the video is in a quitet state */
162
163         writel(0, par->df_regs + DF_ALPHA_CONTROL_1);
164         writel(0, par->df_regs + DF_ALPHA_CONTROL_1 + 32);
165         writel(0, par->df_regs + DF_ALPHA_CONTROL_1 + 64);
166
167         /* Turn off the VGA and video enable */
168         val = readl (par->dc_regs + DC_GENERAL_CFG) &
169                 ~(DC_GCFG_VGAE | DC_GCFG_VIDE);
170
171         writel(val, par->dc_regs + DC_GENERAL_CFG);
172
173         val = readl(par->df_regs + DF_VIDEO_CFG) & ~DF_VCFG_VID_EN;
174         writel(val, par->df_regs + DF_VIDEO_CFG);
175
176         writel( DC_IRQ_MASK | DC_VSYNC_IRQ_MASK |
177                 DC_IRQ_STATUS | DC_VSYNC_IRQ_STATUS,
178                 par->dc_regs + DC_IRQ);
179
180         val = readl(par->dc_regs + DC_GENLCK_CTRL) & ~DC_GENLCK_ENABLE;
181         writel(val, par->dc_regs + DC_GENLCK_CTRL);
182
183         val = readl(par->dc_regs + DC_COLOR_KEY) & ~DC_CLR_KEY_ENABLE;
184         writel(val & ~DC_CLR_KEY_ENABLE, par->dc_regs + DC_COLOR_KEY);
185
186         /* We don't actually blank the panel, due to the long latency
187            involved with bringing it back */
188
189         val = readl(par->df_regs + DF_MISC) | DF_MISC_DAC_PWRDN;
190         writel(val, par->df_regs + DF_MISC);
191
192         /* Turn off the display */
193
194         val = readl(par->df_regs + DF_DISPLAY_CFG);
195         writel(val & ~(DF_DCFG_CRT_EN | DF_DCFG_HSYNC_EN | DF_DCFG_VSYNC_EN |
196                        DF_DCFG_DAC_BL_EN), par->df_regs + DF_DISPLAY_CFG);
197
198         gcfg = readl(par->dc_regs + DC_GENERAL_CFG);
199         gcfg &= ~(DC_GCFG_CMPE | DC_GCFG_DECE);
200         writel(gcfg, par->dc_regs + DC_GENERAL_CFG);
201
202         /* Turn off the TGEN */
203         val = readl(par->dc_regs + DC_DISPLAY_CFG);
204         val &= ~DC_DCFG_TGEN;
205         writel(val, par->dc_regs + DC_DISPLAY_CFG);
206
207         /* Wait 1000 usecs to ensure that the TGEN is clear */
208         udelay(1000);
209
210         /* Turn off the FIFO loader */
211
212         gcfg &= ~DC_GCFG_DFLE;
213         writel(gcfg, par->dc_regs + DC_GENERAL_CFG);
214
215         /* Lastly, wait for the GP to go idle */
216
217         do {
218                 val = readl(par->gp_regs + GP_BLT_STATUS);
219         } while ((val & GP_BS_BLT_BUSY) || !(val & GP_BS_CB_EMPTY));
220 }
221
222 static void lx_graphics_enable(struct fb_info *info)
223 {
224         struct lxfb_par *par = info->par;
225         u32 temp, config;
226
227         /* Set the video request register */
228         writel(0, par->df_regs + DF_VIDEO_REQUEST);
229
230         /* Set up the polarities */
231
232         config = readl(par->df_regs + DF_DISPLAY_CFG);
233
234         config &= ~(DF_DCFG_CRT_SYNC_SKW_MASK | DF_DCFG_PWR_SEQ_DLY_MASK |
235                   DF_DCFG_CRT_HSYNC_POL     | DF_DCFG_CRT_VSYNC_POL);
236
237         config |= (DF_DCFG_CRT_SYNC_SKW_INIT | DF_DCFG_PWR_SEQ_DLY_INIT  |
238                    DF_DCFG_GV_PAL_BYP);
239
240         if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
241                 config |= DF_DCFG_CRT_HSYNC_POL;
242
243         if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
244                 config |= DF_DCFG_CRT_VSYNC_POL;
245
246         if (par->output & OUTPUT_PANEL) {
247                 u32 msrlo, msrhi;
248
249                 writel(DF_DEFAULT_TFT_PMTIM1,
250                        par->df_regs + DF_PANEL_TIM1);
251                 writel(DF_DEFAULT_TFT_PMTIM2,
252                        par->df_regs + DF_PANEL_TIM2);
253                 writel(DF_DEFAULT_TFT_DITHCTL,
254                        par->df_regs + DF_DITHER_CONTROL);
255
256                 msrlo = DF_DEFAULT_TFT_PAD_SEL_LOW;
257                 msrhi = DF_DEFAULT_TFT_PAD_SEL_HIGH;
258
259                 wrmsr(MSR_LX_MSR_PADSEL, msrlo, msrhi);
260         }
261
262         if (par->output & OUTPUT_CRT) {
263                 config |= DF_DCFG_CRT_EN   | DF_DCFG_HSYNC_EN |
264                         DF_DCFG_VSYNC_EN | DF_DCFG_DAC_BL_EN;
265         }
266
267         writel(config, par->df_regs + DF_DISPLAY_CFG);
268
269         /* Turn the CRT dacs back on */
270
271         if (par->output & OUTPUT_CRT) {
272                 temp = readl(par->df_regs + DF_MISC);
273                 temp &= ~(DF_MISC_DAC_PWRDN  | DF_MISC_A_PWRDN);
274                 writel(temp, par->df_regs + DF_MISC);
275         }
276
277         /* Turn the panel on (if it isn't already) */
278
279         if (par->output & OUTPUT_PANEL) {
280                 temp = readl(par->df_regs + DF_FP_PM);
281
282                 if (!(temp & 0x09))
283                         writel(temp | DF_FP_PM_P, par->df_regs + DF_FP_PM);
284         }
285
286         temp = readl(par->df_regs + DF_MISC);
287         temp = readl(par->df_regs + DF_DISPLAY_CFG);
288 }
289
290 unsigned int lx_framebuffer_size(void)
291 {
292         unsigned int val;
293
294         /* The frame buffer size is reported by a VSM in VSA II */
295         /* Virtual Register Class    = 0x02                     */
296         /* VG_MEM_SIZE (1MB units)   = 0x00                     */
297
298         outw(0xFC53, 0xAC1C);
299         outw(0x0200, 0xAC1C);
300
301         val = (unsigned int)(inw(0xAC1E)) & 0xFE;
302         return (val << 20);
303 }
304
305 void lx_set_mode(struct fb_info *info)
306 {
307         struct lxfb_par *par = info->par;
308         u64 msrval;
309
310         unsigned int max, dv, val, size;
311
312         unsigned int gcfg, dcfg;
313         int hactive, hblankstart, hsyncstart, hsyncend, hblankend, htotal;
314         int vactive, vblankstart, vsyncstart, vsyncend, vblankend, vtotal;
315
316         /* Unlock the DC registers */
317         writel(DC_UNLOCK_CODE, par->dc_regs + DC_UNLOCK);
318
319         lx_graphics_disable(info);
320
321         lx_set_clock(info);
322
323         /* Set output mode */
324
325         rdmsrl(MSR_LX_GLD_MSR_CONFIG, msrval);
326         msrval &= ~DF_CONFIG_OUTPUT_MASK;
327
328         if (par->output & OUTPUT_PANEL) {
329                 msrval |= DF_OUTPUT_PANEL;
330
331                 if (par->output & OUTPUT_CRT)
332                         msrval |= DF_SIMULTANEOUS_CRT_AND_FP;
333                 else
334                         msrval &= ~DF_SIMULTANEOUS_CRT_AND_FP;
335         } else {
336                 msrval |= DF_OUTPUT_CRT;
337         }
338
339         wrmsrl(MSR_LX_GLD_MSR_CONFIG, msrval);
340
341         /* Clear the various buffers */
342         /* FIXME:  Adjust for panning here */
343
344         writel(0, par->dc_regs + DC_FB_START);
345         writel(0, par->dc_regs + DC_CB_START);
346         writel(0, par->dc_regs + DC_CURSOR_START);
347
348         /* FIXME: Add support for interlacing */
349         /* FIXME: Add support for scaling */
350
351         val = readl(par->dc_regs + DC_GENLCK_CTRL);
352         val &= ~(DC_GC_ALPHA_FLICK_ENABLE |
353                  DC_GC_FLICKER_FILTER_ENABLE | DC_GC_FLICKER_FILTER_MASK);
354
355         /* Default scaling params */
356
357         writel((0x4000 << 16) | 0x4000, par->dc_regs + DC_GFX_SCALE);
358         writel(0, par->dc_regs + DC_IRQ_FILT_CTL);
359         writel(val, par->dc_regs + DC_GENLCK_CTRL);
360
361         /* FIXME:  Support compression */
362
363         if (info->fix.line_length > 4096)
364                 dv = DC_DV_LINE_SIZE_8192;
365         else if (info->fix.line_length > 2048)
366                 dv = DC_DV_LINE_SIZE_4096;
367         else if (info->fix.line_length > 1024)
368                 dv = DC_DV_LINE_SIZE_2048;
369         else
370                 dv = DC_DV_LINE_SIZE_1024;
371
372         max = info->fix.line_length * info->var.yres;
373         max = (max + 0x3FF) & 0xFFFFFC00;
374
375         writel(max | DC_DV_TOP_ENABLE, par->dc_regs + DC_DV_TOP);
376
377         val = readl(par->dc_regs + DC_DV_CTL) & ~DC_DV_LINE_SIZE_MASK;
378         writel(val | dv, par->dc_regs + DC_DV_CTL);
379
380         size = info->var.xres * (info->var.bits_per_pixel >> 3);
381
382         writel(info->fix.line_length >> 3, par->dc_regs + DC_GRAPHICS_PITCH);
383         writel((size + 7) >> 3, par->dc_regs + DC_LINE_SIZE);
384
385         /* Set default watermark values */
386
387         rdmsrl(MSR_LX_SPARE_MSR, msrval);
388
389         msrval &= ~(DC_SPARE_DISABLE_CFIFO_HGO | DC_SPARE_VFIFO_ARB_SELECT |
390                     DC_SPARE_LOAD_WM_LPEN_MASK | DC_SPARE_WM_LPEN_OVRD |
391                     DC_SPARE_DISABLE_INIT_VID_PRI | DC_SPARE_DISABLE_VFIFO_WM);
392         msrval |= DC_SPARE_DISABLE_VFIFO_WM | DC_SPARE_DISABLE_INIT_VID_PRI;
393         wrmsrl(MSR_LX_SPARE_MSR, msrval);
394
395         gcfg = DC_GCFG_DFLE;   /* Display fifo enable */
396         gcfg |= 0xB600;         /* Set default priority */
397         gcfg |= DC_GCFG_FDTY;  /* Set the frame dirty mode */
398
399         dcfg  = DC_DCFG_VDEN;  /* Enable video data */
400         dcfg |= DC_DCFG_GDEN;  /* Enable graphics */
401         dcfg |= DC_DCFG_TGEN;  /* Turn on the timing generator */
402         dcfg |= DC_DCFG_TRUP;  /* Update timings immediately */
403         dcfg |= DC_DCFG_PALB;  /* Palette bypass in > 8 bpp modes */
404         dcfg |= DC_DCFG_VISL;
405         dcfg |= DC_DCFG_DCEN;  /* Always center the display */
406
407         /* Set the current BPP mode */
408
409         switch (info->var.bits_per_pixel) {
410         case 8:
411                 dcfg |= DC_DCFG_DISP_MODE_8BPP;
412                 break;
413
414         case 16:
415                 dcfg |= DC_DCFG_DISP_MODE_16BPP | DC_DCFG_16BPP;
416                 break;
417
418         case 32:
419         case 24:
420                 dcfg |= DC_DCFG_DISP_MODE_24BPP;
421                 break;
422         }
423
424         /* Now - set up the timings */
425
426         hactive = info->var.xres;
427         hblankstart = hactive;
428         hsyncstart = hblankstart + info->var.right_margin;
429         hsyncend =  hsyncstart + info->var.hsync_len;
430         hblankend = hsyncend + info->var.left_margin;
431         htotal = hblankend;
432
433         vactive = info->var.yres;
434         vblankstart = vactive;
435         vsyncstart = vblankstart + info->var.lower_margin;
436         vsyncend =  vsyncstart + info->var.vsync_len;
437         vblankend = vsyncend + info->var.upper_margin;
438         vtotal = vblankend;
439
440         writel((hactive - 1) | ((htotal - 1) << 16),
441                par->dc_regs + DC_H_ACTIVE_TIMING);
442         writel((hblankstart - 1) | ((hblankend - 1) << 16),
443                par->dc_regs + DC_H_BLANK_TIMING);
444         writel((hsyncstart - 1) | ((hsyncend - 1) << 16),
445                par->dc_regs + DC_H_SYNC_TIMING);
446
447         writel((vactive - 1) | ((vtotal - 1) << 16),
448                par->dc_regs + DC_V_ACTIVE_TIMING);
449
450         writel((vblankstart - 1) | ((vblankend - 1) << 16),
451                par->dc_regs + DC_V_BLANK_TIMING);
452
453         writel((vsyncstart - 1)  | ((vsyncend - 1) << 16),
454                par->dc_regs + DC_V_SYNC_TIMING);
455
456         writel( (info->var.xres - 1) << 16 | (info->var.yres - 1),
457                 par->dc_regs + DC_FB_ACTIVE);
458
459         /* And re-enable the graphics output */
460         lx_graphics_enable(info);
461
462         /* Write the two main configuration registers */
463         writel(dcfg, par->dc_regs + DC_DISPLAY_CFG);
464         writel(0, par->dc_regs + DC_ARB_CFG);
465         writel(gcfg, par->dc_regs + DC_GENERAL_CFG);
466
467         /* Lock the DC registers */
468         writel(0, par->dc_regs + DC_UNLOCK);
469 }
470
471 void lx_set_palette_reg(struct fb_info *info, unsigned regno,
472                         unsigned red, unsigned green, unsigned blue)
473 {
474         struct lxfb_par *par = info->par;
475         int val;
476
477         /* Hardware palette is in RGB 8-8-8 format. */
478
479         val  = (red   << 8) & 0xff0000;
480         val |= (green)      & 0x00ff00;
481         val |= (blue  >> 8) & 0x0000ff;
482
483         writel(regno, par->dc_regs + DC_PAL_ADDRESS);
484         writel(val, par->dc_regs + DC_PAL_DATA);
485 }
486
487 int lx_blank_display(struct fb_info *info, int blank_mode)
488 {
489         struct lxfb_par *par = info->par;
490         u32 dcfg, fp_pm;
491         int blank, hsync, vsync;
492
493         /* CRT power saving modes. */
494         switch (blank_mode) {
495         case FB_BLANK_UNBLANK:
496                 blank = 0; hsync = 1; vsync = 1;
497                 break;
498         case FB_BLANK_NORMAL:
499                 blank = 1; hsync = 1; vsync = 1;
500                 break;
501         case FB_BLANK_VSYNC_SUSPEND:
502                 blank = 1; hsync = 1; vsync = 0;
503                 break;
504         case FB_BLANK_HSYNC_SUSPEND:
505                 blank = 1; hsync = 0; vsync = 1;
506                 break;
507         case FB_BLANK_POWERDOWN:
508                 blank = 1; hsync = 0; vsync = 0;
509                 break;
510         default:
511                 return -EINVAL;
512         }
513
514         dcfg = readl(par->df_regs + DF_DISPLAY_CFG);
515         dcfg &= ~(DF_DCFG_DAC_BL_EN
516                   | DF_DCFG_HSYNC_EN | DF_DCFG_VSYNC_EN);
517         if (!blank)
518                 dcfg |= DF_DCFG_DAC_BL_EN;
519         if (hsync)
520                 dcfg |= DF_DCFG_HSYNC_EN;
521         if (vsync)
522                 dcfg |= DF_DCFG_VSYNC_EN;
523         writel(dcfg, par->df_regs + DF_DISPLAY_CFG);
524
525         /* Power on/off flat panel */
526
527         if (par->output & OUTPUT_PANEL) {
528                 fp_pm = readl(par->df_regs + DF_FP_PM);
529                 if (blank_mode == FB_BLANK_POWERDOWN)
530                         fp_pm &= ~DF_FP_PM_P;
531                 else
532                         fp_pm |= DF_FP_PM_P;
533                 writel(fp_pm, par->df_regs + DF_FP_PM);
534         }
535
536         return 0;
537 }