aboutsummaryrefslogtreecommitdiff
path: root/drivers/video/geode/lxfb_ops.c
blob: a52c180062c8ab474d2b0130d188ba2dcaf4627b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* Geode LX framebuffer driver
 *
 * Copyright (C) 2006-2007, Advanced Micro Devices,Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/fb.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <asm/geode.h>

#include "lxfb.h"

/* TODO
 * Support panel scaling
 * Add acceleration
 * Add support for interlacing (TV out)
 * Support compression
 */

/* This is the complete list of PLL frequencies that we can set -
 * we will choose the closest match to the incoming clock.
 * freq is the frequency of the dotclock * 1000 (for example,
 * 24823 = 24.983 Mhz).
 * pllval is the corresponding PLL value
*/

static const struct {
  unsigned int pllval;
  unsigned int freq;
} pll_table[] = {
  { 0x000031AC, 24923 },
  { 0x0000215D, 25175 },
  { 0x00001087, 27000 },
  { 0x0000216C, 28322 },
  { 0x0000218D, 28560 },
  { 0x000010C9, 31200 },
  { 0x00003147, 31500 },
  { 0x000010A7, 33032 },
  { 0x00002159, 35112 },
  { 0x00004249, 35500 },
  { 0x00000057, 36000 },
  { 0x0000219A, 37889 },
  { 0x00002158, 39168 },
  { 0x00000045, 40000 },
  { 0x00000089, 43163 },
  { 0x000010E7, 44900 },
  { 0x00002136, 45720 },
  { 0x00003207, 49500 },
  { 0x00002187, 50000 },
  { 0x00004286, 56250 },
  { 0x000010E5, 60065 },
  { 0x00004214, 65000 },
  { 0x00001105, 68179 },
  { 0x000031E4, 74250 },
  { 0x00003183, 75000 },
  { 0x00004284, 78750 },
  { 0x00001104, 81600 },
  { 0x00006363, 94500 },
  { 0x00005303, 97520 },
  { 0x00002183, 100187 },
  { 0x00002122, 101420 },
  { 0x00001081, 108000 },
  { 0x00006201, 113310 },
  { 0x00000041, 119650 },
  { 0x000041A1, 129600 },
  { 0x00002182, 133500 },
  { 0x000041B1, 135000 },
  { 0x00000051, 144000 },
  { 0x000041E1, 148500 },
  { 0x000062D1, 157500 },
  { 0x000031A1, 162000 },
  { 0x00000061, 169203 },
  { 0x00004231, 172800 },
  { 0x00002151, 175500 },
  { 0x000052E1, 189000 },
  { 0x00000071, 192000 },
  { 0x00003201, 198000 },
  { 0x00004291, 202500 },
  { 0x00001101, 204750 },
  { 0x00007481, 218250 },
  { 0x00004170, 229500 },
  { 0x00006210, 234000 },
  { 0x00003140, 251182 },
  { 0x00006250, 261000 },
  { 0x000041C0, 278400 },
  { 0x00005220, 280640 },
  { 0x00000050, 288000 },
  { 0x000041E0, 297000 },
  { 0x00002130, 320207 }
};


static void lx_set_dotpll(u32 pllval)
{
	u32 dotpll_lo, dotpll_hi;
	int i;

	rdmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);

	if ((dotpll_lo & GLCP_DOTPLL_LOCK) && (dotpll_hi == pllval))
		return;

	dotpll_hi = pllval;
	dotpll_lo &= ~(GLCP_DOTPLL_BYPASS | GLCP_DOTPLL_HALFPIX);
	dotpll_lo |= GLCP_DOTPLL_RESET;

	wrmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);

	/* Wait 100us for the PLL to lock */

	udelay(100);

	/* Now, loop for the lock bit */

	for (i = 0; i < 1000; i++) {
		rdmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
		if (dotpll_lo & GLCP_DOTPLL_LOCK)
			break;
	}

	/* Clear the reset bit */

	dotpll_lo &= ~GLCP_DOTPLL_RESET;
	wrmsr(MSR_GLCP_DOTPLL, dotpll_lo, dotpll_hi);
}

/* Set the clock based on the frequency specified by the current mode */

static void lx_set_clock(struct fb_info *info)
{
	unsigned int diff, min, best = 0;
	unsigned int freq, i;

	freq = (unsigned int) (0x3b9aca00 / info->var.pixclock);

	min = abs(pll_table[0].freq - freq);

	for (i = 0; i < ARRAY_SIZE(pll_table); i++) {
		diff = abs(pll_table[i].freq - freq);
		if (diff < min) {
			min = diff;
			best = i;
		}
	}

	lx_set_dotpll(pll_table[best].pllval & 0x7FFF);
}

static void lx_graphics_disable(struct fb_info *info)
{
	struct lxfb_par *par = info->par;
	unsigned int val, gcfg;

	/* Note:  This assumes that the video is in a quitet state */

	writel(0, par->df_regs + DF_ALPHA_CONTROL_1);
	writel(0, par->df_regs + DF_ALPHA_CONTROL_1 + 32);
	writel(0, par->df_regs + DF_ALPHA_CONTROL_1 + 64);

	/* Turn off the VGA and video enable */
	val = readl (par->dc_regs + DC_GENERAL_CFG) &
		~(DC_GCFG_VGAE | DC_GCFG_VIDE);

	writel(val, par->dc_regs + DC_GENERAL_CFG);

	val = readl(par->df_regs + DF_VIDEO_CFG) & ~DF_VCFG_VID_EN;
	writel(val, par->df_regs + DF_VIDEO_CFG);

	writel( DC_IRQ_MASK | DC_VSYNC_IRQ_MASK |
		DC_IRQ_STATUS | DC_VSYNC_IRQ_STATUS,
		par->dc_regs + DC_IRQ);

	val = readl(par->dc_regs + DC_GENLCK_CTRL) & ~DC_GENLCK_ENABLE;
	writel(val, par->dc_regs + DC_GENLCK_CTRL);

	val = readl(par->dc_regs + DC_COLOR_KEY) & ~DC_CLR_KEY_ENABLE;
	writel(val & ~DC_CLR_KEY_ENABLE, par->dc_regs + DC_COLOR_KEY);

	/* We don't actually blank the panel, due to the long latency
	   involved with bringing it back */

	val = readl(par->df_regs + DF_MISC) | DF_MISC_DAC_PWRDN;
	writel(val, par->df_regs + DF_MISC);

	/* Turn off the display */

	val = readl(par->df_regs + DF_DISPLAY_CFG);
	writel(val & ~(DF_DCFG_CRT_EN | DF_DCFG_HSYNC_EN | DF_DCFG_VSYNC_EN |
		       DF_DCFG_DAC_BL_EN), par->df_regs + DF_DISPLAY_CFG);

	gcfg = readl(par->dc_regs + DC_GENERAL_CFG);
	gcfg &= ~(DC_GCFG_CMPE | DC_GCFG_DECE);
	writel(gcfg, par->dc_regs + DC_GENERAL_CFG);

	/* Turn off the TGEN */
	val = readl(par->dc_regs + DC_DISPLAY_CFG);
	val &= ~DC_DCFG_TGEN;
	writel(val, par->dc_regs + DC_DISPLAY_CFG);

	/* Wait 1000 usecs to ensure that the TGEN is clear */
	udelay(1000);

	/* Turn off the FIFO loader */

	gcfg &= ~DC_GCFG_DFLE;
	writel(gcfg, par->dc_regs + DC_GENERAL_CFG);

	/* Lastly, wait for the GP to go idle */

	do {
		val = readl(par->gp_regs + GP_BLT_STATUS);
	} while ((val & GP_BS_BLT_BUSY) || !(val & GP_BS_CB_EMPTY));
}

static void lx_graphics_enable(struct fb_info *info)
{
	struct lxfb_par *par = info->par;
	u32 temp, config;

	/* Set the video request register */
	writel(0, par->df_regs + DF_VIDEO_REQUEST);

	/* Set up the polarities */

	config = readl(par->df_regs + DF_DISPLAY_CFG);

	config &= ~(DF_DCFG_CRT_SYNC_SKW_MASK | DF_DCFG_PWR_SEQ_DLY_MASK |
		  DF_DCFG_CRT_HSYNC_POL     | DF_DCFG_CRT_VSYNC_POL);

	config |= (DF_DCFG_CRT_SYNC_SKW_INIT | DF_DCFG_PWR_SEQ_DLY_INIT  |
		   DF_DCFG_GV_PAL_BYP);

	if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
		config |= DF_DCFG_CRT_HSYNC_POL;

	if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
		config |= DF_DCFG_CRT_VSYNC_POL;

	if (par->output & OUTPUT_PANEL) {
		u32 msrlo, msrhi;

		writel(DF_DEFAULT_TFT_PMTIM1,
		       par->df_regs + DF_PANEL_TIM1);
		writel(DF_DEFAULT_TFT_PMTIM2,
		       par->df_regs + DF_PANEL_TIM2);
		writel(DF_DEFAULT_TFT_DITHCTL,
		       par->df_regs + DF_DITHER_CONTROL);

		msrlo = DF_DEFAULT_TFT_PAD_SEL_LOW;
		msrhi = DF_DEFAULT_TFT_PAD_SEL_HIGH;

		wrmsr(MSR_LX_MSR_PADSEL, msrlo, msrhi);
	}

	if (par->output & OUTPUT_CRT) {
		config |= DF_DCFG_CRT_EN   | DF_DCFG_HSYNC_EN |
			DF_DCFG_VSYNC_EN | DF_DCFG_DAC_BL_EN;
	}

	writel(config, par->df_regs + DF_DISPLAY_CFG);

	/* Turn the CRT dacs back on */

	if (par->output & OUTPUT_CRT) {
		temp = readl(par->df_regs + DF_MISC);
		temp &= ~(DF_MISC_DAC_PWRDN  | DF_MISC_A_PWRDN);
		writel(temp, par->df_regs + DF_MISC);
	}

	/* Turn the panel on (if it isn't already) */

	if (par->output & OUTPUT_PANEL) {
		temp = readl(par->df_regs + DF_FP_PM);

		if (!(temp & 0x09))
			writel(temp | DF_FP_PM_P, par->df_regs + DF_FP_PM);
	}

	temp = readl(par->df_regs + DF_MISC);
	temp = readl(par->df_regs + DF_DISPLAY_CFG);
}

unsigned int lx_framebuffer_size(void)
{
	unsigned int val;

	/* The frame buffer size is reported by a VSM in VSA II */
	/* Virtual Register Class    = 0x02                     */
	/* VG_MEM_SIZE (1MB units)   = 0x00                     */

	outw(0xFC53, 0xAC1C);
	outw(0x0200, 0xAC1C);

	val = (unsigned int)(inw(0xAC1E)) & 0xFE;
	return (val << 20);
}

void lx_set_mode(struct fb_info *info)
{
	struct lxfb_par *par = info->par;
	u64 msrval;

	unsigned int max, dv, val, size;

	unsigned int gcfg, dcfg;
	int hactive, hblankstart, hsyncstart, hsyncend, hblankend, htotal;
	int vactive, vblankstart, vsyncstart, vsyncend, vblankend, vtotal;

	/* Unlock the DC registers */
	writel(DC_UNLOCK_CODE, par->dc_regs + DC_UNLOCK);

	lx_graphics_disable(info);

	lx_set_clock(info);

	/* Set output mode */

	rdmsrl(MSR_LX_GLD_MSR_CONFIG, msrval);
	msrval &= ~DF_CONFIG_OUTPUT_MASK;

	if (par->output & OUTPUT_PANEL) {
		msrval |= DF_OUTPUT_PANEL;

		if (par->output & OUTPUT_CRT)
			msrval |= DF_SIMULTANEOUS_CRT_AND_FP;
		else
			msrval &= ~DF_SIMULTANEOUS_CRT_AND_FP;
	} else {
		msrval |= DF_OUTPUT_CRT;
	}

	wrmsrl(MSR_LX_GLD_MSR_CONFIG, msrval);

	/* Clear the various buffers */
	/* FIXME:  Adjust for panning here */

	writel(0, par->dc_regs + DC_FB_START);
	writel(0, par->dc_regs + DC_CB_START);
	writel(0, par->dc_regs + DC_CURSOR_START);

	/* FIXME: Add support for interlacing */
	/* FIXME: Add support for scaling */

	val = readl(par->dc_regs + DC_GENLCK_CTRL);
	val &= ~(DC_GC_ALPHA_FLICK_ENABLE |
		 DC_GC_FLICKER_FILTER_ENABLE | DC_GC_FLICKER_FILTER_MASK);

	/* Default scaling params */

	writel((0x4000 << 16) | 0x4000, par->dc_regs + DC_GFX_SCALE);
	writel(0, par->dc_regs + DC_IRQ_FILT_CTL);
	writel(val, par->dc_regs + DC_GENLCK_CTRL);

	/* FIXME:  Support compression */

	if (info->fix.line_length > 4096)
		dv = DC_DV_LINE_SIZE_8192;
	else if (info->fix.line_length > 2048)
		dv = DC_DV_LINE_SIZE_4096;
	else if (info->fix.line_length > 1024)
		dv = DC_DV_LINE_SIZE_2048;
	else
		dv = DC_DV_LINE_SIZE_1024;

	max = info->fix.line_length * info->var.yres;
	max = (max + 0x3FF) & 0xFFFFFC00;

	writel(max | DC_DV_TOP_ENABLE, par->dc_regs + DC_DV_TOP);

	val = readl(par->dc_regs + DC_DV_CTL) & ~DC_DV_LINE_SIZE_MASK;
	writel(val | dv, par->dc_regs + DC_DV_CTL);

	size = info->var.xres * (info->var.bits_per_pixel >> 3);

	writel(info->fix.line_length >> 3, par->dc_regs + DC_GRAPHICS_PITCH);
	writel((size + 7) >> 3, par->dc_regs + DC_LINE_SIZE);

	/* Set default watermark values */

	rdmsrl(MSR_LX_SPARE_MSR, msrval);

	msrval &= ~(DC_SPARE_DISABLE_CFIFO_HGO | DC_SPARE_VFIFO_ARB_SELECT |
		    DC_SPARE_LOAD_WM_LPEN_MASK | DC_SPARE_WM_LPEN_OVRD |
		    DC_SPARE_DISABLE_INIT_VID_PRI | DC_SPARE_DISABLE_VFIFO_WM);
	msrval |= DC_SPARE_DISABLE_VFIFO_WM | DC_SPARE_DISABLE_INIT_VID_PRI;
	wrmsrl(MSR_LX_SPARE_MSR, msrval);

	gcfg = DC_GCFG_DFLE;   /* Display fifo enable */
	gcfg |= 0xB600;         /* Set default priority */
	gcfg |= DC_GCFG_FDTY;  /* Set the frame dirty mode */

	dcfg  = DC_DCFG_VDEN;  /* Enable video data */
	dcfg |= DC_DCFG_GDEN;  /* Enable graphics */
	dcfg |= DC_DCFG_TGEN;  /* Turn on the timing generator */
	dcfg |= DC_DCFG_TRUP;  /* Update timings immediately */
	dcfg |= DC_DCFG_PALB;  /* Palette bypass in > 8 bpp modes */
	dcfg |= DC_DCFG_VISL;
	dcfg |= DC_DCFG_DCEN;  /* Always center the display */

	/* Set the current BPP mode */

	switch (info->var.bits_per_pixel) {
	case 8:
		dcfg |= DC_DCFG_DISP_MODE_8BPP;
		break;

	case 16:
		dcfg |= DC_DCFG_DISP_MODE_16BPP | DC_DCFG_16BPP;
		break;

	case 32:
	case 24:
		dcfg |= DC_DCFG_DISP_MODE_24BPP;
		break;
	}

	/* Now - set up the timings */

	hactive = info->var.xres;
	hblankstart = hactive;
	hsyncstart = hblankstart + info->var.right_margin;
	hsyncend =  hsyncstart + info->var.hsync_len;
	hblankend = hsyncend + info->var.left_margin;
	htotal = hblankend;

	vactive = info->var.yres;
	vblankstart = vactive;
	vsyncstart = vblankstart + info->var.lower_margin;
	vsyncend =  vsyncstart + info->var.vsync_len;
	vblankend = vsyncend + info->var.upper_margin;
	vtotal = vblankend;

	writel((hactive - 1) | ((htotal - 1) << 16),
	       par->dc_regs + DC_H_ACTIVE_TIMING);
	writel((hblankstart - 1) | ((hblankend - 1) << 16),
	       par->dc_regs + DC_H_BLANK_TIMING);
	writel((hsyncstart - 1) | ((hsyncend - 1) << 16),
	       par->dc_regs + DC_H_SYNC_TIMING);

	writel((vactive - 1) | ((vtotal - 1) << 16),
	       par->dc_regs + DC_V_ACTIVE_TIMING);

	writel((vblankstart - 1) | ((vblankend - 1) << 16),
	       par->dc_regs + DC_V_BLANK_TIMING);

	writel((vsyncstart - 1)  | ((vsyncend - 1) << 16),
	       par->dc_regs + DC_V_SYNC_TIMING);

	writel( (info->var.xres - 1) << 16 | (info->var.yres - 1),
		par->dc_regs + DC_FB_ACTIVE);

	/* And re-enable the graphics output */
	lx_graphics_enable(info);

	/* Write the two main configuration registers */
	writel(dcfg, par->dc_regs + DC_DISPLAY_CFG);
	writel(0, par->dc_regs + DC_ARB_CFG);
	writel(gcfg, par->dc_regs + DC_GENERAL_CFG);

	/* Lock the DC registers */
	writel(0, par->dc_regs + DC_UNLOCK);
}

void lx_set_palette_reg(struct fb_info *info, unsigned regno,
			unsigned red, unsigned green, unsigned blue)
{
	struct lxfb_par *par = info->par;
	int val;

	/* Hardware palette is in RGB 8-8-8 format. */

	val  = (red   << 8) & 0xff0000;
	val |= (green)      & 0x00ff00;
	val |= (blue  >> 8) & 0x0000ff;

	writel(regno, par->dc_regs + DC_PAL_ADDRESS);
	writel(val, par->dc_regs + DC_PAL_DATA);
}

int lx_blank_display(struct fb_info *info, int blank_mode)
{
	struct lxfb_par *par = info->par;
	u32 dcfg, fp_pm;
	int blank, hsync, vsync;

	/* CRT power saving modes. */
	switch (blank_mode) {
	case FB_BLANK_UNBLANK:
		blank = 0; hsync = 1; vsync = 1;
		break;
	case FB_BLANK_NORMAL:
		blank = 1; hsync = 1; vsync = 1;
		break;
	case FB_BLANK_VSYNC_SUSPEND:
		blank = 1; hsync = 1; vsync = 0;
		break;
	case FB_BLANK_HSYNC_SUSPEND:
		blank = 1; hsync = 0; vsync = 1;
		break;
	case FB_BLANK_POWERDOWN:
		blank = 1; hsync = 0; vsync = 0;
		break;
	default:
		return -EINVAL;
	}

	dcfg = readl(par->df_regs + DF_DISPLAY_CFG);
	dcfg &= ~(DF_DCFG_DAC_BL_EN
		  | DF_DCFG_HSYNC_EN | DF_DCFG_VSYNC_EN);
	if (!blank)
		dcfg |= DF_DCFG_DAC_BL_EN;
	if (hsync)
		dcfg |= DF_DCFG_HSYNC_EN;
	if (vsync)
		dcfg |= DF_DCFG_VSYNC_EN;
	writel(dcfg, par->df_regs + DF_DISPLAY_CFG);

	/* Power on/off flat panel */

	if (par->output & OUTPUT_PANEL) {
		fp_pm = readl(par->df_regs + DF_FP_PM);
		if (blank_mode == FB_BLANK_POWERDOWN)
			fp_pm &= ~DF_FP_PM_P;
		else
			fp_pm |= DF_FP_PM_P;
		writel(fp_pm, par->df_regs + DF_FP_PM);
	}

	return 0;
}