230926c99a0401a95bc6217f0f82fdbaabdf093d
[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/io.h>
34 #include <arch/msr.h>
35
36 #include "font8x16.h"
37
38 /* This is the video mode that we're going to use for our VGA screen */
39
40 static const struct mode {
41         unsigned int pll;
42         unsigned int hactive, hblankstart, hsyncstart;
43         unsigned int hsyncend, hblankend, htotal;
44         unsigned int vactive, vblankstart, vsyncstart;
45         unsigned int vsyncend, vblankend, vtotal;
46         unsigned int synccfg;
47 } vga_mode = {
48         .pll = 0x215D,
49         .hactive = 640,
50         .vactive = 400,
51         .hblankstart = 640,
52         .hsyncstart =  640 + 40,
53         .hsyncend =    640 + 40 + 96,
54         .hblankend =   640 + 40 + 96 + 24,
55         .htotal =      640 + 40 + 96 + 24,
56         .vblankstart = 400,
57         .vsyncstart = 400 + 39,
58         .vsyncend =   400 + 39 + 2,
59         .vblankend =  400 + 39 + 2 + 9,
60         .vtotal =     400 + 39 + 2 + 9,
61         .synccfg = 0x300,
62 };
63
64 /* These are the color definitions for the 16 standard colors */
65
66 static const unsigned int vga_colors[] = {
67         (0x00 << 16) | (0x00 << 8) | 0x00,
68         (0xAA << 16) | (0x00 << 8) | 0x00,
69         (0x00 << 16) | (0xAA << 8) | 0x00,
70         (0xAA << 16) | (0x55 << 8) | 0x00,
71         (0x00 << 16) | (0x00 << 8) | 0xAA,
72         (0xAA << 16) | (0x00 << 8) | 0xAA,
73         (0x00 << 16) | (0xAA << 8) | 0xAA,
74         (0xAA << 16) | (0xAA << 8) | 0xAA,
75         (0x55 << 16) | (0x55 << 8) | 0x55,
76         (0xFF << 16) | (0x55 << 8) | 0x55,
77         (0x55 << 16) | (0xFF << 8) | 0x55,
78         (0xFF << 16) | (0xFF << 8) | 0x55,
79         (0x55 << 16) | (0x55 << 8) | 0xFF,
80         (0xFF << 16) | (0x55 << 8) | 0xFF,
81         (0x55 << 16) | (0xFF << 8) | 0xFF,
82         (0xFF << 16) | (0xFF << 8) | 0xFF,
83 };
84
85 /* Addresses for the various components */
86
87 static unsigned long dcaddr;
88 static unsigned long vgaddr;
89 static unsigned long gpaddr;
90 static unsigned long fbaddr;
91
92 #define FB ((unsigned char *) fbaddr)
93
94 static void init_video_mode(void)
95 {
96         unsigned int lo, hi, val;
97         int i;
98
99         /* Set the PLL */
100
101         rdmsr(0x4c000015, lo, hi);
102
103         hi = vga_mode.pll;
104
105         lo &= ~0x1008000;
106         lo |= 0x01;
107
108         wrmsr(0x4c000015, lo, hi);
109         udelay(100);
110
111         for(i = 0; i < 1000; i++) {
112                 rdmsr(0x4c000015, lo, hi);
113                 if (lo & 0x2000000)
114                         break;
115         }
116
117         lo &= ~0x01;
118         wrmsr(0x4c000015, lo, hi);
119
120         rdmsr(0x48002001, lo, hi);
121         lo &= ~0x38;
122         wrmsr(0x48002001, lo, hi);
123
124         writel(0x4758, dcaddr + 0x00);
125
126         val = readl(dcaddr + 0x00);
127
128         writel(0, dcaddr + 0x10);
129         writel(0, dcaddr + 0x14);
130         writel(0, dcaddr + 0x18);
131
132         /* Set up the default scaling */
133
134         val = readl(dcaddr + 0xD4);
135
136         writel((0x4000 << 16) | 0x4000, dcaddr + 0x90);
137         writel(0, dcaddr + 0x94);
138         writel(val & ~0xf3040000, dcaddr + 0xD4);
139
140         /* Set up the compression (or lack thereof) */
141         writel(vga_mode.hactive * vga_mode.vactive | 0x01, dcaddr + 0x2C);
142
143         val = readl(dcaddr + 0x88);
144         writel(val & ~0xC00, dcaddr + 0x88);
145         writel(0, dcaddr + 0x8C);
146
147         /* Set the pitch */
148         writel(vga_mode.hactive >> 3, dcaddr + 0x34);
149         writel((vga_mode.hactive + 7) >> 3, dcaddr + 0x30);
150
151         /* Set up default watermarks */
152
153         lo = 0xC0;
154         wrmsr(0x80000011, lo, hi);
155
156         /* Write the timings */
157
158         writel((vga_mode.hactive - 1) | ((vga_mode.htotal - 1) << 16),
159                dcaddr + 0x40);
160
161         writel((vga_mode.hblankstart - 1) | ((vga_mode.hblankend - 1) << 16),
162                dcaddr + 0x44);
163
164         writel((vga_mode.hsyncstart - 1) | ((vga_mode.hsyncend - 1) << 16),
165                dcaddr + 0x48);
166
167         writel((vga_mode.vactive - 1) | ((vga_mode.vtotal - 1) << 16),
168                dcaddr + 0x50);
169
170         writel((vga_mode.vblankstart - 1) | ((vga_mode.vblankend - 1) << 16),
171                dcaddr + 0x54);
172
173         writel((vga_mode.vsyncstart - 1) | ((vga_mode.vsyncend - 1) << 16),
174                dcaddr + 0x58);
175
176         writel(((vga_mode.hactive - 1) << 16) | (vga_mode.vactive - 1),
177                dcaddr + 0x5C);
178
179
180         /* Write the VG configuration */
181
182         writel(0x290000F | vga_mode.synccfg, vgaddr + 0x08);
183
184         /* Turn on the dacs */
185
186         val = readl(vgaddr + 0x50);
187         writel((val & ~0xC00) | 0x01, vgaddr + 0x50);
188
189         /* Set the framebuffer base */
190         writel(fbaddr, dcaddr + 0x84);
191
192         /* Write the final configuration */
193
194         writel(0xB000059, dcaddr + 0x08);
195         writel(0, dcaddr + 0x0C);
196         writel(0x2B601, dcaddr + 0x04);
197 }
198
199 static void geode_set_palette(int entry, unsigned int color)
200 {
201         writel(entry, dcaddr + 0x70);
202         writel(color, dcaddr + 0x74);
203 }
204
205 static void geode_scroll_up(void)
206 {
207         unsigned char *dst = FB;
208         unsigned char *src = FB + vga_mode.hactive;
209         int y;
210
211         for(y = 0; y < vga_mode.vactive - FONT_HEIGHT; y++) {
212                 memcpy(dst, src, vga_mode.hactive);
213
214                 dst += vga_mode.hactive;
215                 src += vga_mode.hactive;
216         }
217
218         dst = FB + (vga_mode.vactive - FONT_HEIGHT) * vga_mode.hactive;
219
220         for(; y < vga_mode.vactive; y++) {
221                 memset(dst, 0, vga_mode.hactive);
222                 dst += vga_mode.hactive;
223         }
224 }
225
226 static void geode_clear(void)
227 {
228         int row;
229         unsigned char *ptr = FB;
230
231         for(row = 0; row < vga_mode.vactive; row++) {
232                 memset(ptr, 0, vga_mode.hactive);
233                 ptr += vga_mode.hactive;
234         }
235 }
236
237 static void geode_putc(u8 row, u8 col, unsigned int ch)
238 {
239         unsigned char *dst;
240         unsigned char *glyph = font8x16 + ((ch & 0xFF) * FONT_HEIGHT);
241
242         unsigned char bg = (ch >> 12) & 0xF;
243         unsigned char fg = (ch >> 8) & 0xF;
244
245         int x, y;
246
247         dst = FB + ((row * FONT_HEIGHT) * vga_mode.hactive);
248         dst += (col * FONT_WIDTH);
249
250         for(y = 0; y < FONT_HEIGHT; y++) {
251
252                 for(x = FONT_WIDTH - 1; x >= 0; x--)
253                         dst[FONT_WIDTH - x] = (*glyph & (1 << x)) ?
254                                 fg : bg;
255
256                 dst += vga_mode.hactive;
257                 glyph++;
258         }
259 }
260
261 static int geode_init(void)
262 {
263         pcidev_t dev;
264         int i;
265
266         if (!pci_find_device(0x1022, 0x2081, &dev))
267                 return -1;
268
269         fbaddr = pci_read_resource(dev, 0);
270         gpaddr = pci_read_resource(dev, 1);
271         dcaddr = pci_read_resource(dev, 2);
272         vgaddr = pci_read_resource(dev, 3);
273
274         init_video_mode();
275
276         /* Set up the palette */
277
278         for(i = 0; i < ARRAY_SIZE(vga_colors); i++) {
279                 geode_set_palette(i, vga_colors[i]);
280         }
281
282         return 0;
283 }
284
285 struct video_console geode_video_console = {
286         .init = geode_init,
287         .putc = geode_putc,
288         .clear = geode_clear,
289         .scroll_up = geode_scroll_up
290 };