xref: /netbsd-src/sys/arch/ews4800mips/dev/ga.c (revision 9480c51b042e0def8b42fb02329183ed5bbec874)
1 /*	$NetBSD: ga.c,v 1.5 2009/11/07 07:27:43 cegger Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
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.
18  *
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.
30  */
31 
32 /* Graphic Adaptor  (350, 360) */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ga.c,v 1.5 2009/11/07 07:27:43 cegger Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 
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>
51 
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
65 
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)))
70 
71 bool
ga_init(struct ga * ga)72 ga_init(struct ga *ga)
73 {
74 	int i;
75 
76 	/* Map GA register and buffers */
77 	if (ga->reg_addr == 0 && ga_map(ga) != 0)
78 		return false;
79 
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);
94 	}
95 
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);
102 	}
103 
104 	ga_vblank_start(ga);
105 
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);
112 	}
113 
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);
120 	}
121 
122 	/* Blink mask P0-P7 */
123 	ga_bt463_reg(ga, 0x209);
124 	for (i = 0; i < 4; i++)
125 		ga_bt463_data(ga, 0x00);
126 
127 	ga_clut_init(ga);
128 
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);
135 	}
136 
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);
154 	}
155 
156 	return true;
157 }
158 
159 bool
ga_map(struct ga * ga)160 ga_map(struct ga *ga)
161 {
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;
169 
170 	pa = (paddr_t)GA_REG_ADDR;
171 	epa = pa + GA_REG_SIZE;
172 
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;
176 	}
177 
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);
180 
181 	pmap_update(pmap_kernel());
182 
183 	ga->reg_addr = (uint32_t)va;
184 #endif
185 
186 	return true;
187 }
188 
189 void
ga_vblank_start(const struct ga * ga)190 ga_vblank_start(const struct ga *ga)
191 {
192 
193 	while ((ga_reg_read(ga, 0xe00) & 0x1) == 0)	/* V-blank */
194 		;
195 	while ((ga_reg_read(ga, 0xe00) & 0x1) == 1)
196 		;
197 	/* V-blank start */
198 }
199 
200 /* Bt463 utils */
201 void
ga_bt463_reg(const struct ga * ga,int r)202 ga_bt463_reg(const struct ga *ga, int r)
203 {
204 
205 	ga_reg_write(ga, 0xc80, r & 0xff);
206 	ga_reg_write(ga, 0xc84, (r >> 8) & 0xff);
207 }
208 
209 void
ga_bt463_data(const struct ga * ga,int v)210 ga_bt463_data(const struct ga *ga, int v)
211 {
212 
213 	ga_reg_write(ga, 0xc88, v & 0xff);
214 }
215 
216 void
ga_bt463_reg_data(const struct ga * ga,int r,int v)217 ga_bt463_reg_data(const struct ga *ga, int r, int v)
218 {
219 
220 	ga_bt463_reg(ga, r);
221 	ga_bt463_data(ga, v);
222 }
223 
224 /* CLUT */
225 void
ga_clut_init(struct ga * ga)226 ga_clut_init(struct ga *ga)
227 {
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 },
246 	};
247 	int i, j, r, g, b;
248 
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]);
255 	}
256 
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);
263 	}
264 
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]);
275 			}
276 		}
277 	}
278 
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);
284 	}
285 
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);
291 	}
292 }
293 
294 void
ga_clut_get(struct ga * ga)295 ga_clut_get(struct ga *ga)
296 {
297 	int i;
298 
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);
304 	}
305 }
306 
307 void
ga_clut_set(const struct ga * ga)308 ga_clut_set(const struct ga *ga)
309 {
310 	int i;
311 
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]);
317 	}
318 }
319 
320 /* Not yet analyzed. */
321 #ifdef _STANDALONE
322 void
ga_dda_busy(const struct ga * ga)323 ga_dda_busy(const struct ga *ga)
324 {
325 
326 	while ((ga_reg_read(ga, 0xf00) & 0x8000) == 0)
327 		;
328 }
329 
330 void
ga_ovl_init(const struct ga * ga)331 ga_ovl_init(const struct ga *ga)
332 {
333 	uint32_t *p0, *p1;
334 
335 	ga_reg_write(ga, 0x400, 0xffffffff);
336 	p0 = (uint32_t *)0xf2000000;
337 	p1 = (uint32_t *)0xf2200000;
338 	while (p0 < p1)
339 		*p0++ = 0;
340 }
341 
342 void
ga_id_init(const struct ga * ga)343 ga_id_init(const struct ga *ga)
344 {
345 	uint32_t *p0, *p1;
346 
347 	p0 = (uint32_t *)0xf3000000;
348 	p1 = (uint32_t *)0xf3040000;
349 	while (p0 < p1)
350 		*p0++ = 0;
351 }
352 
353 void
ga_block_clear(const struct ga * ga)354 ga_block_clear(const struct ga *ga)
355 {
356 	uint32_t *p0, *p1;
357 
358 	ga_reg_write(ga, 0xe80, 0);
359 	ga_reg_write(ga, 0x400, 0xffffff);
360 
361 	p0 = (uint32_t *)0xf0c00000;
362 	p1 = (uint32_t *)0xf0c80000;
363 	while (p0 < p1)
364 		*p0++ = 0xffffffff;
365 }
366 
367 void
ga_plane_mask_test(const struct ga * ga)368 ga_plane_mask_test(const struct ga *ga)
369 {
370 	int i;
371 
372 	ga_reg_write(ga, 0x400, 0xffffff);
373 	*(volatile uint32_t *)0xf1000000 = 0;
374 
375 	ga_reg_write(ga, 0x400, 0xaaaaaa);
376 	*(volatile uint32_t *)0xf1000000 = 0xffffff;
377 
378 	if ((*(volatile uint32_t *)0xf1000000 & 0xffffff) != 0xaaaaaa)
379 		goto err;
380 	ga_reg_write(ga, 0x400, 0xffffff);
381 	*(volatile uint32_t *)0xf1000000 = 0;
382 
383 
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;
391 
392 	*(volatile uint32_t *)0xf1100000 = 0;
393 	*(volatile uint32_t *)0xf1100000 = 0xffffff;
394 	if ((*(volatile uint32_t *)0xf1100000 & 0xffffff) != 0xffffff)
395 		goto err;
396 
397 	ga_reg_write(ga, 0x400, 0xaaaaaa);
398 	*(volatile uint32_t *)0xf1100000 = 0;
399 	if ((*(volatile uint32_t *)0xf1100000 & 0xffffff) != 0x555555)
400 		goto err;
401 
402 	ga_reg_write(ga, 0x400, 0);
403 	*(volatile uint32_t *)0xf1100000 = 0xffffff;
404 	if ((*(volatile uint32_t *)0xf1100000 & 0xffffff) != 0x555555)
405 		goto err;
406 
407 	ga_reg_write(ga, 0x400, 0xffffff);
408 	*(volatile uint32_t *)0xf1100000 = 0;
409 
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;
417 
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;
431 
432 	printf("SUCCESS\n");
433 	return;
434  err:
435 	printf("ERROR\n");
436 }
437 #endif /* _STANDALONE */
438