Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / ews4800mips / dev / ga.c
blob5a675f26a76acb7209e1bfef25e3465dc3f0705e
1 /* $NetBSD: ga.c,v 1.4 2008/04/28 20:23:18 martin Exp $ */
3 /*-
4 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 /* Graphic Adaptor (350, 360) */
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ga.c,v 1.4 2008/04/28 20:23:18 martin Exp $");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #ifdef _STANDALONE
41 #include <lib/libsa/stand.h>
42 #include <lib/libkern/libkern.h>
43 #include "local.h"
44 #endif
45 #ifdef _KERNEL
46 #include <uvm/uvm_extern.h>
47 #include <machine/vmparam.h>
48 #endif
49 #include <machine/gareg.h>
50 #include <machine/gavar.h>
52 bool ga_map(struct ga *);
53 void ga_clut_init(struct ga *);
54 void ga_vblank_start(const struct ga *);
55 void ga_bt463_reg(const struct ga *, int);
56 void ga_bt463_data(const struct ga *, int);
57 void ga_bt463_reg_data(const struct ga *, int, int);
58 #ifdef _STANDALONE
59 void ga_dda_busy(const struct ga *);
60 void ga_ovl_init(const struct ga *);
61 void ga_id_init(const struct ga *);
62 void ga_block_clear(const struct ga *);
63 void ga_plane_mask_test(const struct ga *);
64 #endif
66 #define ga_reg_write(ga, ofs, val) \
67 (*(volatile uint32_t *)((ga)->reg_addr + (ofs)) = (val))
68 #define ga_reg_read(ga, ofs) \
69 (*(volatile uint32_t *)((ga)->reg_addr + (ofs)))
71 bool
72 ga_init(struct ga *ga)
74 int i;
76 /* Map GA register and buffers */
77 if (ga->reg_addr == 0 && ga_map(ga) != 0)
78 return false;
80 /* This is 350 GA-ROM initialization sequence. */
81 if (ga->flags == 0x0000) {
82 ga_bt463_reg_data(ga, 0x201, 0x40);
83 ga_bt463_reg_data(ga, 0x202, 0x40);
84 ga_bt463_reg_data(ga, 0x203,
85 ((ga_reg_read(ga, 0xe00) & 2) << 6) | 0x40);
86 } else if (ga->flags == 0x0001) {
87 ga_bt463_reg_data(ga, 0x201, 0x40);
88 ga_bt463_reg_data(ga, 0x202, 0);
89 ga_bt463_reg_data(ga, 0x203,
90 ((ga_reg_read(ga, 0xe00) & 2) << 6) | 0x40);
91 ga_bt463_reg_data(ga, 0x204, 0xff); /* Display ON/OFF ? */
92 ga_bt463_reg_data(ga, 0x206, 0);
93 ga_bt463_reg_data(ga, 0x20a, 0);
96 /* Window type table */
97 ga_bt463_reg(ga, 0x300);
98 for (i = 0; i < 16; i++) {
99 ga_bt463_data(ga, 0x00);
100 ga_bt463_data(ga, 0xe1);
101 ga_bt463_data(ga, 0x01);
104 ga_vblank_start(ga);
106 /* ??? */
107 ga_bt463_reg(ga, 0x302);
108 for (i = 0; i < 2; i++) {
109 ga_bt463_data(ga, 0x00);
110 ga_bt463_data(ga, 0xe3);
111 ga_bt463_data(ga, 0x21);
114 /* Read mask P0-P7 */
115 if (ga->flags != 0x0001) {
116 /* TR2A display blinks if this was done.. */
117 ga_bt463_reg(ga, 0x205);
118 for (i = 0; i < 4; i++)
119 ga_bt463_data(ga, 0xff);
122 /* Blink mask P0-P7 */
123 ga_bt463_reg(ga, 0x209);
124 for (i = 0; i < 4; i++)
125 ga_bt463_data(ga, 0x00);
127 ga_clut_init(ga);
129 /* ??? */
130 ga_bt463_reg(ga, 0x200);
131 for (i = 0; i < 0xff; i++) {
132 ga_reg_write(ga, 0xc8c, 0);
133 ga_reg_write(ga, 0xc8c, 0);
134 ga_reg_write(ga, 0xc8c, 0);
137 if (ga_reg_read(ga, 0xe00) & 2)
138 ga_reg_write(ga, 0xe08, 0x790); /* 71Hz */
139 else
140 ga_reg_write(ga, 0xe08, 0x670); /* 60Hz */
141 #ifdef _STANDALONE
142 ga_block_clear(ga);
143 ga_ovl_init(ga);
144 ga_id_init(ga);
145 #endif
146 /* Cursor RAM clear */
147 ga_reg_write(ga, 0xc90, 0);
148 ga_reg_write(ga, 0xc94, 0);
149 ga_reg_write(ga, 0xca0, 0);
150 ga_reg_write(ga, 0xca4, 0);
151 for (i = 0; i < 512; i++) {
152 ga_reg_write(ga, 0xc98, 0);
153 ga_reg_write(ga, 0xca8, 0);
156 return true;
159 bool
160 ga_map(struct ga *ga)
162 #ifdef _STANDALONE
163 /* IPL maps register region using 16Mpage */
164 ga->reg_addr = GA_REG_ADDR;
165 #endif
166 #ifdef _KERNEL
167 paddr_t pa, epa;
168 vaddr_t va, tva;
170 pa = (paddr_t)GA_REG_ADDR;
171 epa = pa + GA_REG_SIZE;
173 if (!(va = uvm_km_alloc(kernel_map, epa - pa, 0, UVM_KMF_VAONLY))) {
174 printf("can't map GA register.\n");
175 return false;
178 for (tva = va; pa < epa; pa += PAGE_SIZE, tva += PAGE_SIZE)
179 pmap_kenter_pa(tva, pa, VM_PROT_READ | VM_PROT_WRITE, 0);
181 pmap_update(pmap_kernel());
183 ga->reg_addr = (uint32_t)va;
184 #endif
186 return true;
189 void
190 ga_vblank_start(const struct ga *ga)
193 while ((ga_reg_read(ga, 0xe00) & 0x1) == 0) /* V-blank */
195 while ((ga_reg_read(ga, 0xe00) & 0x1) == 1)
197 /* V-blank start */
200 /* Bt463 utils */
201 void
202 ga_bt463_reg(const struct ga *ga, int r)
205 ga_reg_write(ga, 0xc80, r & 0xff);
206 ga_reg_write(ga, 0xc84, (r >> 8) & 0xff);
209 void
210 ga_bt463_data(const struct ga *ga, int v)
213 ga_reg_write(ga, 0xc88, v & 0xff);
216 void
217 ga_bt463_reg_data(const struct ga *ga, int r, int v)
220 ga_bt463_reg(ga, r);
221 ga_bt463_data(ga, v);
224 /* CLUT */
225 void
226 ga_clut_init(struct ga *ga)
228 const uint8_t compo6[6] = { 0, 51, 102, 153, 204, 255 };
229 const uint8_t ansi_color[16][3] = {
230 { 0x00, 0x00, 0x00 },
231 { 0xff, 0x00, 0x00 },
232 { 0x00, 0xff, 0x00 },
233 { 0xff, 0xff, 0x00 },
234 { 0x00, 0x00, 0xff },
235 { 0xff, 0x00, 0xff },
236 { 0x00, 0xff, 0xff },
237 { 0xff, 0xff, 0xff },
238 { 0x00, 0x00, 0x00 },
239 { 0x80, 0x00, 0x00 },
240 { 0x00, 0x80, 0x00 },
241 { 0x80, 0x80, 0x00 },
242 { 0x00, 0x00, 0x80 },
243 { 0x80, 0x00, 0x80 },
244 { 0x00, 0x80, 0x80 },
245 { 0x80, 0x80, 0x80 },
247 int i, j, r, g, b;
249 ga_bt463_reg(ga, 0);
250 /* ANSI escape sequence */
251 for (i = 0; i < 16; i++) {
252 ga_reg_write(ga, 0xc8c, ga->clut[i][0] = ansi_color[i][0]);
253 ga_reg_write(ga, 0xc8c, ga->clut[i][1] = ansi_color[i][1]);
254 ga_reg_write(ga, 0xc8c, ga->clut[i][2] = ansi_color[i][2]);
257 /* 16 - 31, gray scale */
258 for ( ; i < 32; i++) {
259 j = (i - 16) * 17;
260 ga_reg_write(ga, 0xc8c, ga->clut[i][0] = j);
261 ga_reg_write(ga, 0xc8c, ga->clut[i][1] = j);
262 ga_reg_write(ga, 0xc8c, ga->clut[i][2] = j);
265 /* 32 - 247, RGB color */
266 for (r = 0; r < 6; r++) {
267 for (g = 0; g < 6; g++) {
268 for (b = 0; b < 6; b++, i++) {
269 ga_reg_write(ga, 0xc8c,
270 ga->clut[i][0] = compo6[r]);
271 ga_reg_write(ga, 0xc8c,
272 ga->clut[i][1] = compo6[g]);
273 ga_reg_write(ga, 0xc8c,
274 ga->clut[i][2] = compo6[b]);
279 /* 248 - 256, white */
280 for ( ; i < 256; i++) {
281 ga_reg_write(ga, 0xc8c, ga->clut[i][0] = 0xff);
282 ga_reg_write(ga, 0xc8c, ga->clut[i][1] = 0xff);
283 ga_reg_write(ga, 0xc8c, ga->clut[i][2] = 0xff);
286 /* 257 - 528, black */
287 for ( ; i < 528; i++) {
288 ga_reg_write(ga, 0xc8c, 0);
289 ga_reg_write(ga, 0xc8c, 0);
290 ga_reg_write(ga, 0xc8c, 0);
294 void
295 ga_clut_get(struct ga *ga)
297 int i;
299 ga_bt463_reg(ga, 0);
300 for (i = 0; i < 256; i++) {
301 ga->clut[i][0] = ga_reg_read(ga, 0xc8c);
302 ga->clut[i][1] = ga_reg_read(ga, 0xc8c);
303 ga->clut[i][2] = ga_reg_read(ga, 0xc8c);
307 void
308 ga_clut_set(const struct ga *ga)
310 int i;
312 ga_bt463_reg(ga, 0);
313 for (i = 0; i < 256; i++) {
314 ga_reg_write(ga, 0xc8c, ga->clut[i][0]);
315 ga_reg_write(ga, 0xc8c, ga->clut[i][1]);
316 ga_reg_write(ga, 0xc8c, ga->clut[i][2]);
320 /* Not yet analyzed. */
321 #ifdef _STANDALONE
322 void
323 ga_dda_busy(const struct ga *ga)
326 while ((ga_reg_read(ga, 0xf00) & 0x8000) == 0)
330 void
331 ga_ovl_init(const struct ga *ga)
333 uint32_t *p0, *p1;
335 ga_reg_write(ga, 0x400, 0xffffffff);
336 p0 = (uint32_t *)0xf2000000;
337 p1 = (uint32_t *)0xf2200000;
338 while (p0 < p1)
339 *p0++ = 0;
342 void
343 ga_id_init(const struct ga *ga)
345 uint32_t *p0, *p1;
347 p0 = (uint32_t *)0xf3000000;
348 p1 = (uint32_t *)0xf3040000;
349 while (p0 < p1)
350 *p0++ = 0;
353 void
354 ga_block_clear(const struct ga *ga)
356 uint32_t *p0, *p1;
358 ga_reg_write(ga, 0xe80, 0);
359 ga_reg_write(ga, 0x400, 0xffffff);
361 p0 = (uint32_t *)0xf0c00000;
362 p1 = (uint32_t *)0xf0c80000;
363 while (p0 < p1)
364 *p0++ = 0xffffffff;
367 void
368 ga_plane_mask_test(const struct ga *ga)
370 int i;
372 ga_reg_write(ga, 0x400, 0xffffff);
373 *(volatile uint32_t *)0xf1000000 = 0;
375 ga_reg_write(ga, 0x400, 0xaaaaaa);
376 *(volatile uint32_t *)0xf1000000 = 0xffffff;
378 if ((*(volatile uint32_t *)0xf1000000 & 0xffffff) != 0xaaaaaa)
379 goto err;
380 ga_reg_write(ga, 0x400, 0xffffff);
381 *(volatile uint32_t *)0xf1000000 = 0;
384 *(volatile uint32_t *)0xf1080008 = 0;
385 ga_reg_write(ga, 0x400, 0x555555);
386 *(volatile uint32_t *)0xf1080008 = 0xffffff;
387 if ((*(volatile uint32_t *)0xf1080008 & 0xffffff) != 0x555555)
388 goto err;
389 ga_reg_write(ga, 0x400, 0xffffff);
390 *(volatile uint32_t *)0xf1080008 = 0;
392 *(volatile uint32_t *)0xf1100000 = 0;
393 *(volatile uint32_t *)0xf1100000 = 0xffffff;
394 if ((*(volatile uint32_t *)0xf1100000 & 0xffffff) != 0xffffff)
395 goto err;
397 ga_reg_write(ga, 0x400, 0xaaaaaa);
398 *(volatile uint32_t *)0xf1100000 = 0;
399 if ((*(volatile uint32_t *)0xf1100000 & 0xffffff) != 0x555555)
400 goto err;
402 ga_reg_write(ga, 0x400, 0);
403 *(volatile uint32_t *)0xf1100000 = 0xffffff;
404 if ((*(volatile uint32_t *)0xf1100000 & 0xffffff) != 0x555555)
405 goto err;
407 ga_reg_write(ga, 0x400, 0xffffff);
408 *(volatile uint32_t *)0xf1100000 = 0;
410 ga_reg_write(ga, 0xe80, 0xffffff);
411 ga_reg_write(ga, 0x400, 0xffffff);
412 *(volatile uint32_t *)0xf0c00000 = 0xffffffff;
413 for (i = 0; i < 32; i++)
414 if ((*(volatile uint32_t *)(0xf1000000 + i * 4) & 0xffffff) !=
415 0xffffff)
416 goto err;
418 ga_reg_write(ga, 0xe80, 0);
419 ga_reg_write(ga, 0x400, 0xaaaaaa);
420 *(volatile uint32_t *)0xf0c00000 = 0xffffffff;
421 for (i = 0; i < 32; i++)
422 if ((*(volatile uint32_t *)(0xf1000000 + i * 4) & 0xffffff) !=
423 0x555555)
424 goto err;
425 ga_reg_write(ga, 0x400, 0x555555);
426 *(volatile uint32_t *)0xf0c00000 = 0xffffffff;
427 for (i = 0; i < 32; i++)
428 if ((*(volatile uint32_t *)(0xf1000000 + i * 4) & 0xffffff) !=
429 0x0)
430 goto err;
432 printf("SUCCESS\n");
433 return;
434 err:
435 printf("ERROR\n");
437 #endif /* _STANDALONE */