b0631c55b36c446428aeb88de69286e771f62cc8
[coreboot.git] / payloads / libpayload / drivers / video / geode.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <libpayload.h>
31 #include <pci.h>
32 #include <video_console.h>
33 #include <arch/msr.h>
34 #include "font8x16.h"
35
36 /* This is the video mode that we're going to use for our VGA screen */
37
38 static const struct mode {
39         unsigned int pll;
40         unsigned int hactive, hblankstart, hsyncstart;
41         unsigned int hsyncend, hblankend, htotal;
42         unsigned int vactive, vblankstart, vsyncstart;
43         unsigned int vsyncend, vblankend, vtotal;
44         unsigned int synccfg;
45 } vga_mode = {
46         .pll = 0x215D,
47         .hactive = 640,
48         .vactive = 400,
49         .hblankstart = 640,
50         .hsyncstart =  640 + 40,
51         .hsyncend =    640 + 40 + 96,
52         .hblankend =   640 + 40 + 96 + 24,
53         .htotal =      640 + 40 + 96 + 24,
54         .vblankstart = 400,
55         .vsyncstart = 400 + 39,
56         .vsyncend =   400 + 39 + 2,
57         .vblankend =  400 + 39 + 2 + 9,
58         .vtotal =     400 + 39 + 2 + 9,
59         .synccfg = 0x300,
60 };
61
62 /* These are the color definitions for the 16 standard colors */
63
64 static const unsigned int vga_colors[] = {
65         (0x00 << 16) | (0x00 << 8) | 0x00,
66         (0xAA << 16) | (0x00 << 8) | 0x00,
67         (0x00 << 16) | (0xAA << 8) | 0x00,
68         (0xAA << 16) | (0x55 << 8) | 0x00,
69         (0x00 << 16) | (0x00 << 8) | 0xAA,
70         (0xAA << 16) | (0x00 << 8) | 0xAA,
71         (0x00 << 16) | (0xAA << 8) | 0xAA,
72         (0xAA << 16) | (0xAA << 8) | 0xAA,
73         (0x55 << 16) | (0x55 << 8) | 0x55,
74         (0xFF << 16) | (0x55 << 8) | 0x55,
75         (0x55 << 16) | (0xFF << 8) | 0x55,
76         (0xFF << 16) | (0xFF << 8) | 0x55,
77         (0x55 << 16) | (0x55 << 8) | 0xFF,
78         (0xFF << 16) | (0x55 << 8) | 0xFF,
79         (0x55 << 16) | (0xFF << 8) | 0xFF,
80         (0xFF << 16) | (0xFF << 8) | 0xFF,
81 };
82
83 /* Addresses for the various components */
84
85 static unsigned long dcaddr;
86 static unsigned long vgaddr;
87 static unsigned long gpaddr;
88 static unsigned long fbaddr;
89
90 #define FB ((unsigned char *) fbaddr)
91
92 static void init_video_mode(void)
93 {
94         unsigned int lo, hi, val;
95         int i;
96
97         /* Set the PLL */
98
99         rdmsr(0x4c000015, lo, hi);
100
101         hi = vga_mode.pll;
102
103         lo &= ~0x1008000;
104         lo |= 0x01;
105
106         wrmsr(0x4c000015, lo, hi);
107         udelay(100);
108
109         for(i = 0; i < 1000; i++) {
110                 rdmsr(0x4c000015, lo, hi);
111                 if (lo & 0x2000000)
112                         break;
113         }
114
115         lo &= ~0x01;
116         wrmsr(0x4c000015, lo, hi);
117
118         rdmsr(0x48002001, lo, hi);
119         lo &= ~0x38;
120         wrmsr(0x48002001, lo, hi);
121
122         writel(0x4758, dcaddr + 0x00);
123
124         val = readl(dcaddr + 0x00);
125
126         writel(0, dcaddr + 0x10);
127         writel(0, dcaddr + 0x14);
128         writel(0, dcaddr + 0x18);
129
130         /* Set up the default scaling */
131
132         val = readl(dcaddr + 0xD4);
133
134         writel((0x4000 << 16) | 0x4000, dcaddr + 0x90);
135         writel(0, dcaddr + 0x94);
136         writel(val & ~0xf3040000, dcaddr + 0xD4);
137
138         /* Set up the compression (or lack thereof) */
139         writel(vga_mode.hactive * vga_mode.vactive | 0x01, dcaddr + 0x2C);
140
141         val = readl(dcaddr + 0x88);
142         writel(val & ~0xC00, dcaddr + 0x88);
143         writel(0, dcaddr + 0x8C);
144
145         /* Set the pitch */
146         writel(vga_mode.hactive >> 3, dcaddr + 0x34);
147         writel((vga_mode.hactive + 7) >> 3, dcaddr + 0x30);
148
149         /* Set up default watermarks */
150
151         lo = 0xC0;
152         wrmsr(0x80000011, lo, hi);
153
154         /* Write the timings */
155
156         writel((vga_mode.hactive - 1) | ((vga_mode.htotal - 1) << 16),
157                dcaddr + 0x40);
158
159         writel((vga_mode.hblankstart - 1) | ((vga_mode.hblankend - 1) << 16),
160                dcaddr + 0x44);
161
162         writel((vga_mode.hsyncstart - 1) | ((vga_mode.hsyncend - 1) << 16),
163                dcaddr + 0x48);
164
165         writel((vga_mode.vactive - 1) | ((vga_mode.vtotal - 1) << 16),
166                dcaddr + 0x50);
167
168         writel((vga_mode.vblankstart - 1) | ((vga_mode.vblankend - 1) << 16),
169                dcaddr + 0x54);
170
171         writel((vga_mode.vsyncstart - 1) | ((vga_mode.vsyncend - 1) << 16),
172                dcaddr + 0x58);
173
174         writel(((vga_mode.hactive - 1) << 16) | (vga_mode.vactive - 1),
175                dcaddr + 0x5C);
176
177
178         /* Write the VG configuration */
179
180         writel(0x290000F | vga_mode.synccfg, vgaddr + 0x08);
181
182         /* Turn on the dacs */
183
184         val = readl(vgaddr + 0x50);
185         writel((val & ~0xC00) | 0x01, vgaddr + 0x50);
186
187         /* Set the framebuffer base */
188         writel(fbaddr, dcaddr + 0x84);
189
190         /* Write the final configuration */
191
192         writel(0xB000059, dcaddr + 0x08);
193         writel(0, dcaddr + 0x0C);
194         writel(0x2B601, dcaddr + 0x04);
195 }
196
197 static void geode_set_palette(int entry, unsigned int color)
198 {
199         writel(entry, dcaddr + 0x70);
200         writel(color, dcaddr + 0x74);
201 }
202
203 static void geode_scroll_up(void)
204 {
205         unsigned char *dst = FB;
206         unsigned char *src = FB + vga_mode.hactive;
207         int y;
208
209         for(y = 0; y < vga_mode.vactive - FONT_HEIGHT; y++) {
210                 memcpy(dst, src, vga_mode.hactive);
211
212                 dst += vga_mode.hactive;
213                 src += vga_mode.hactive;
214         }
215
216         dst = FB + (vga_mode.vactive - FONT_HEIGHT) * vga_mode.hactive;
217
218         for(; y < vga_mode.vactive; y++) {
219                 memset(dst, 0, vga_mode.hactive);
220                 dst += vga_mode.hactive;
221         }
222 }
223
224 static void geode_clear(void)
225 {
226         int row;
227         unsigned char *ptr = FB;
228
229         for(row = 0; row < vga_mode.vactive; row++) {
230                 memset(ptr, 0, vga_mode.hactive);
231                 ptr += vga_mode.hactive;
232         }
233 }
234
235 static void geode_putc(u8 row, u8 col, unsigned int ch)
236 {
237         unsigned char *dst;
238         unsigned char *glyph = font8x16 + ((ch & 0xFF) * FONT_HEIGHT);
239
240         unsigned char bg = (ch >> 12) & 0xF;
241         unsigned char fg = (ch >> 8) & 0xF;
242
243         int x, y;
244
245         dst = FB + ((row * FONT_HEIGHT) * vga_mode.hactive);
246         dst += (col * FONT_WIDTH);
247
248         for(y = 0; y < FONT_HEIGHT; y++) {
249
250                 for(x = FONT_WIDTH - 1; x >= 0; x--)
251                         dst[FONT_WIDTH - x] = (*glyph & (1 << x)) ?
252                                 fg : bg;
253
254                 dst += vga_mode.hactive;
255                 glyph++;
256         }
257 }
258
259 static int geode_init(void)
260 {
261         pcidev_t dev;
262         int i;
263
264         if (!pci_find_device(0x1022, 0x2081, &dev))
265                 return -1;
266
267         fbaddr = pci_read_resource(dev, 0);
268         gpaddr = pci_read_resource(dev, 1);
269         dcaddr = pci_read_resource(dev, 2);
270         vgaddr = pci_read_resource(dev, 3);
271
272         init_video_mode();
273
274         /* Set up the palette */
275
276         for(i = 0; i < ARRAY_SIZE(vga_colors); i++) {
277                 geode_set_palette(i, vga_colors[i]);
278         }
279
280         return 0;
281 }
282
283 struct video_console geode_video_console = {
284         .init = geode_init,
285         .putc = geode_putc,
286         .clear = geode_clear,
287         .scroll_up = geode_scroll_up
288 };