xref: /netbsd-src/sys/dev/ic/bt485.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* $NetBSD: bt485.c,v 1.13 2005/12/11 12:21:26 christos Exp $ */
2 
3 /*
4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5  * All rights reserved.
6  *
7  * Author: Chris G. Demetriou
8  *
9  * Permission to use, copy, modify and distribute this software and
10  * its documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie the
27  * rights to redistribute these changes.
28  */
29 
30  /* This code was derived from and originally located in sys/dev/pci/
31   *	 NetBSD: tga_bt485.c,v 1.4 1999/03/24 05:51:21 mrg Exp
32   */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: bt485.c,v 1.13 2005/12/11 12:21:26 christos Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/buf.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 #include <dev/pci/pcivar.h>
47 #include <dev/ic/bt485reg.h>
48 #include <dev/ic/bt485var.h>
49 #include <dev/ic/ramdac.h>
50 
51 #include <dev/wscons/wsconsio.h>
52 
53 /*
54  * Functions exported via the RAMDAC configuration table.
55  */
56 void	bt485_init(struct ramdac_cookie *);
57 int	bt485_set_cmap(struct ramdac_cookie *, struct wsdisplay_cmap *);
58 int	bt485_get_cmap(struct ramdac_cookie *, struct wsdisplay_cmap *);
59 int	bt485_set_cursor(struct ramdac_cookie *, struct wsdisplay_cursor *);
60 int	bt485_get_cursor(struct ramdac_cookie *, struct wsdisplay_cursor *);
61 int	bt485_set_curpos(struct ramdac_cookie *, struct wsdisplay_curpos *);
62 int	bt485_get_curpos(struct ramdac_cookie *, struct wsdisplay_curpos *);
63 int	bt485_get_curmax(struct ramdac_cookie *, struct wsdisplay_curpos *);
64 
65 /* XXX const */
66 struct ramdac_funcs bt485_funcsstruct = {
67 	"Bt485",
68 	bt485_register,
69 	bt485_init,
70 	bt485_set_cmap,
71 	bt485_get_cmap,
72 	bt485_set_cursor,
73 	bt485_get_cursor,
74 	bt485_set_curpos,
75 	bt485_get_curpos,
76 	bt485_get_curmax,
77 	NULL,			/* check_curcmap; not needed */
78 	NULL,			/* set_curcmap; not needed */
79 	NULL,			/* get_curcmap; not needed */
80 	NULL,			/* no dot clock to set */
81 };
82 
83 /*
84  * Private data.
85  */
86 struct bt485data {
87 	void            *cookie;        /* This is what is passed
88 					 * around, and is probably
89 					 * struct tga_devconfig *
90 					 */
91 
92 	int             (*ramdac_sched_update)(void *, void (*)(void *));
93 	void            (*ramdac_wr)(void *, u_int, u_int8_t);
94 	u_int8_t        (*ramdac_rd)(void *, u_int);
95 
96 	int	changed;			/* what changed; see below */
97 	int	curenb;				/* cursor enabled */
98 	struct wsdisplay_curpos curpos;		/* current cursor position */
99 	struct wsdisplay_curpos curhot;		/* cursor hotspot */
100 	char curcmap_r[2];			/* cursor colormap */
101 	char curcmap_g[2];
102 	char curcmap_b[2];
103 	struct wsdisplay_curpos cursize;	/* current cursor size */
104 	char curimage[512];			/* cursor image data */
105 	char curmask[512];			/* cursor mask data */
106 	char cmap_r[256];				/* colormap */
107 	char cmap_g[256];
108 	char cmap_b[256];
109 };
110 
111 #define	DATA_ENB_CHANGED	0x01	/* cursor enable changed */
112 #define	DATA_CURCMAP_CHANGED	0x02	/* cursor colormap changed */
113 #define	DATA_CURSHAPE_CHANGED	0x04	/* cursor size, image, mask changed */
114 #define	DATA_CMAP_CHANGED	0x08	/* colormap changed */
115 #define	DATA_ALL_CHANGED	0x0f
116 
117 #define	CURSOR_MAX_SIZE		64
118 
119 /*
120  * Internal functions.
121  */
122 inline void	bt485_wr_i (struct bt485data *, u_int8_t, u_int8_t);
123 inline u_int8_t bt485_rd_i(struct bt485data *, u_int8_t);
124 void	bt485_update(void *);
125 void	bt485_update_curpos(struct bt485data *);
126 
127 /*****************************************************************************/
128 
129 /*
130  * Functions exported via the RAMDAC configuration table.
131  */
132 
133 struct ramdac_funcs *
134 bt485_funcs(void)
135 {
136 	return &bt485_funcsstruct;
137 }
138 
139 struct ramdac_cookie *
140 bt485_register(v, sched_update, wr, rd)
141 	void *v;
142 	int (*sched_update)(void *, void (*)(void *));
143 	void (*wr)(void *, u_int, u_int8_t);
144 	u_int8_t (*rd)(void *, u_int);
145 {
146 	struct bt485data *data;
147 	/*
148 	 * XXX -- comment out of date.  rcd.
149 	 * If we should allocate a new private info struct, do so.
150 	 * Otherwise, use the one we have (if it's there), or
151 	 * use the temporary one on the stack.
152 	 */
153 	data = malloc(sizeof *data, M_DEVBUF, M_WAITOK);
154 	/* XXX -- if !data */
155 	data->cookie = v;
156 	data->ramdac_sched_update = sched_update;
157 	data->ramdac_wr = wr;
158 	data->ramdac_rd = rd;
159 	return (struct ramdac_cookie *)data;
160 }
161 
162 /*
163  * This function exists solely to provide a means to init
164  * the RAMDAC without first registering.  It is useful for
165  * initializing the console early on.
166  */
167 void
168 bt485_cninit(v, sched_update, wr, rd)
169 	void *v;
170 	int (*sched_update)(void *, void (*)(void *));
171 	void (*wr)(void *, u_int, u_int8_t);
172 	u_int8_t (*rd)(void *, u_int);
173 {
174 	struct bt485data tmp, *data = &tmp;
175 	data->cookie = v;
176 	data->ramdac_sched_update = sched_update;
177 	data->ramdac_wr = wr;
178 	data->ramdac_rd = rd;
179 	bt485_init((struct ramdac_cookie *)data);
180 }
181 
182 void
183 bt485_init(rc)
184 	struct ramdac_cookie *rc;
185 {
186 	u_int8_t regval;
187 	struct bt485data *data = (struct bt485data *)rc;
188 	int i;
189 
190 	/*
191 	 * Init the BT485 for normal operation.
192 	 */
193 
194 	/*
195 	 * Allow indirect register access.  (Actually, this is
196 	 * already enabled.  In fact, if it is _disabled_, for
197 	 * some reason the monitor appears to lose sync!!! (?!?!)
198 	 */
199 	regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_0);
200 	regval |= 0x80;
201 	/*
202 	 * Set the RAMDAC to 8 bit resolution, rather than 6 bit
203 	 * resolution.
204 	 */
205 	regval |= 0x02;
206 	data->ramdac_wr(data->cookie, BT485_REG_COMMAND_0, regval);
207 
208 	/* Set the RAMDAC to 8BPP (no interestion options). */
209 	data->ramdac_wr(data->cookie, BT485_REG_COMMAND_1, 0x40);
210 
211 	/* Disable the cursor (for now) */
212 	regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_2);
213 	regval &= ~0x03;
214 	regval |= 0x24;
215 	data->ramdac_wr(data->cookie, BT485_REG_COMMAND_2, regval);
216 
217 	/* Use a 64x64x2 cursor */
218 	regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
219 	regval |= 0x04;
220 	regval |= 0x08;
221 	bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
222 
223 	/* Set the Pixel Mask to something useful */
224 	data->ramdac_wr(data->cookie, BT485_REG_PIXMASK, 0xff);
225 
226 	/*
227 	 * Initialize the RAMDAC info struct to hold all of our
228 	 * data, and fill it in.
229 	 */
230 	data->changed = DATA_ALL_CHANGED;
231 
232 	data->curenb = 0;				/* cursor disabled */
233 	data->curpos.x = data->curpos.y = 0;		/* right now at 0,0 */
234 	data->curhot.x = data->curhot.y = 0;		/* hot spot at 0,0 */
235 
236 	/* initial cursor colormap: 0 is black, 1 is white */
237 	data->curcmap_r[0] = data->curcmap_g[0] = data->curcmap_b[0] = 0;
238 	data->curcmap_r[1] = data->curcmap_g[1] = data->curcmap_b[1] = 0xff;
239 
240 	/* initial cursor data: 64x64 block of white. */
241 	data->cursize.x = data->cursize.y = 64;
242 	for (i = 0; i < 512; i++)
243 		data->curimage[i] = data->curmask[i] = 0xff;
244 
245 	/* Initial colormap: 0 is black, everything else is white */
246 	data->cmap_r[0] = data->cmap_g[0] = data->cmap_b[0] = 0;
247 	for (i = 1; i < 256; i++)
248 		data->cmap_r[i] = data->cmap_g[i] = data->cmap_b[i] = 255;
249 
250 	bt485_update((void *)data);
251 }
252 
253 int
254 bt485_set_cmap(rc, cmapp)
255 	struct ramdac_cookie *rc;
256 	struct wsdisplay_cmap *cmapp;
257 {
258 	struct bt485data *data = (struct bt485data *)rc;
259 	u_int count, index;
260 	uint8_t r[256], g[256], b[256];
261 	int s, error;
262 
263 	if (cmapp->index >= 256 || cmapp->count > 256 - cmapp->index)
264 		return (EINVAL);
265 
266 	index = cmapp->index;
267 	count = cmapp->count;
268 	error = copyin(cmapp->red, &r[index], count);
269 	if (error)
270 		return error;
271 	error = copyin(cmapp->green, &g[index], count);
272 	if (error)
273 		return error;
274 	error = copyin(cmapp->blue, &b[index], count);
275 	if (error)
276 		return error;
277 	s = spltty();
278 	memcpy(&data->cmap_r[index], &r[index], count);
279 	memcpy(&data->cmap_g[index], &g[index], count);
280 	memcpy(&data->cmap_b[index], &b[index], count);
281 	data->changed |= DATA_CMAP_CHANGED;
282 	data->ramdac_sched_update(data->cookie, bt485_update);
283 	splx(s);
284 	return (0);
285 }
286 
287 int
288 bt485_get_cmap(rc, cmapp)
289 	struct ramdac_cookie *rc;
290 	struct wsdisplay_cmap *cmapp;
291 {
292 	struct bt485data *data = (struct bt485data *)rc;
293 	u_int count, index;
294 	int error;
295 
296 	if (cmapp->index >= 256 || cmapp->count > 256 - cmapp->index )
297 		return (EINVAL);
298 
299 	count = cmapp->count;
300 	index = cmapp->index;
301 	error = copyout(&data->cmap_r[index], cmapp->red, count);
302 	if (error)
303 		return (error);
304 	error = copyout(&data->cmap_g[index], cmapp->green, count);
305 	if (error)
306 		return (error);
307 	error = copyout(&data->cmap_b[index], cmapp->blue, count);
308 	return (error);
309 }
310 
311 int
312 bt485_set_cursor(rc, cursorp)
313 	struct ramdac_cookie *rc;
314 	struct wsdisplay_cursor *cursorp;
315 {
316 	struct bt485data *data = (struct bt485data *)rc;
317 	u_int count = 0, icount = 0, index = 0, v;
318 	char r[2], g[2], b[2], image[512], mask[512];
319 	int s, error;
320 
321 	v = cursorp->which;
322 
323 	/*
324 	 * For DOCMAP and DOSHAPE, copy in the new data
325 	 * before we do anything that we can't recover from.
326 	 */
327 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
328 		if (cursorp->cmap.index > 2 ||
329 		    (cursorp->cmap.index + cursorp->cmap.count) > 2)
330 			return (EINVAL);
331 		count = cursorp->cmap.count;
332 		index = cursorp->cmap.index;
333 		error = copyin(cursorp->cmap.red, &r[index], count);
334 		if (error)
335 			return error;
336 		error = copyin(cursorp->cmap.green, &g[index], count);
337 		if (error)
338 			return error;
339 		error = copyin(cursorp->cmap.blue, &b[index], count);
340 		if (error)
341 			return error;
342 	}
343 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
344 		if (cursorp->size.x > CURSOR_MAX_SIZE ||
345 		    cursorp->size.y > CURSOR_MAX_SIZE)
346 			return (EINVAL);
347 		icount = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
348 		error = copyin(cursorp->image, image, icount);
349 		if (error)
350 			return error;
351 		error = copyin(cursorp->mask, mask, icount);
352 		if (error)
353 			return error;
354 	}
355 
356 	if (v & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOCUR)) {
357 		if (v & WSDISPLAY_CURSOR_DOPOS)
358 			data->curpos = cursorp->pos;
359 		if (v & WSDISPLAY_CURSOR_DOCUR)
360 			data->curhot = cursorp->hot;
361 		bt485_update_curpos(data);
362 	}
363 
364 	s = spltty();
365 
366 	/* Data is all available; perform the requested operations. */
367 	if (v & WSDISPLAY_CURSOR_DOCUR) {
368 		data->curenb = cursorp->enable;
369 		data->changed |= DATA_ENB_CHANGED;
370 	}
371 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
372 		memcpy(&data->curcmap_r[index], &r[index], count);
373 		memcpy(&data->curcmap_g[index], &g[index], count);
374 		memcpy(&data->curcmap_b[index], &b[index], count);
375 		data->changed |= DATA_CURCMAP_CHANGED;
376 	}
377 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
378 		data->cursize = cursorp->size;
379 		count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
380 		memset(data->curimage, 0, sizeof data->curimage);
381 		memset(data->curmask, 0, sizeof data->curmask);
382 		memcpy(data->curimage, image, icount);
383 		memcpy(data->curmask, mask, icount);
384 		data->changed |= DATA_CURSHAPE_CHANGED;
385 	}
386 
387 	if (data->changed)
388 		data->ramdac_sched_update(data->cookie, bt485_update);
389 	splx(s);
390 
391 	return (0);
392 }
393 
394 int
395 bt485_get_cursor(rc, cursorp)
396 	struct ramdac_cookie *rc;
397 	struct wsdisplay_cursor *cursorp;
398 {
399 	struct bt485data *data = (struct bt485data *)rc;
400 	int error, count;
401 
402 	/* we return everything they want */
403 	cursorp->which = WSDISPLAY_CURSOR_DOALL;
404 
405 	cursorp->enable = data->curenb;	/* DOCUR */
406 	cursorp->pos = data->curpos;	/* DOPOS */
407 	cursorp->hot = data->curhot;	/* DOHOT */
408 
409 	cursorp->cmap.index = 0;	/* DOCMAP */
410 	cursorp->cmap.count = 2;
411 	if (cursorp->cmap.red != NULL) {
412 		error = copyout(data->curcmap_r, cursorp->cmap.red, 2);
413 		if (error)
414 			return (error);
415 	}
416 	if (cursorp->cmap.green != NULL) {
417 		error = copyout(data->curcmap_g, cursorp->cmap.green, 2);
418 		if (error)
419 			return (error);
420 	}
421 	if (cursorp->cmap.blue != NULL) {
422 		error = copyout(data->curcmap_b, cursorp->cmap.blue, 2);
423 		if (error)
424 			return (error);
425 	}
426 
427 	cursorp->size = data->cursize;	/* DOSHAPE */
428 	if (cursorp->image != NULL) {
429 		count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
430 		error = copyout(data->curimage, cursorp->image, count);
431 		if (error)
432 			return (error);
433 		error = copyout(data->curmask, cursorp->mask, count);
434 		if (error)
435 			return (error);
436 	}
437 
438 	return (0);
439 }
440 
441 int
442 bt485_set_curpos(rc, curposp)
443 	struct ramdac_cookie *rc;
444 	struct wsdisplay_curpos *curposp;
445 {
446 	struct bt485data *data = (struct bt485data *)rc;
447 
448 	data->curpos = *curposp;
449 	bt485_update_curpos(data);
450 
451 	return (0);
452 }
453 
454 int
455 bt485_get_curpos(rc, curposp)
456 	struct ramdac_cookie *rc;
457 	struct wsdisplay_curpos *curposp;
458 {
459 	struct bt485data *data = (struct bt485data *)rc;
460 
461 	*curposp = data->curpos;
462 	return (0);
463 }
464 
465 int
466 bt485_get_curmax(rc, curposp)
467 	struct ramdac_cookie *rc;
468 	struct wsdisplay_curpos *curposp;
469 {
470 
471 	curposp->x = curposp->y = CURSOR_MAX_SIZE;
472 	return (0);
473 }
474 
475 /*****************************************************************************/
476 
477 /*
478  * Internal functions.
479  */
480 
481 inline void
482 bt485_wr_i(data, ireg, val)
483 	struct bt485data *data;
484 	u_int8_t ireg;
485 	u_int8_t val;
486 {
487 	data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, ireg);
488 	data->ramdac_wr(data->cookie, BT485_REG_EXTENDED, val);
489 }
490 
491 inline u_int8_t
492 bt485_rd_i(data, ireg)
493 	struct bt485data *data;
494 	u_int8_t ireg;
495 {
496 	data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, ireg);
497 	return (data->ramdac_rd(data->cookie, BT485_REG_EXTENDED));
498 }
499 
500 void
501 bt485_update(vp)
502 	void *vp;
503 {
504 	struct bt485data *data = vp;
505 	u_int8_t regval;
506 	int count, i, v;
507 
508 	v = data->changed;
509 	data->changed = 0;
510 
511 	if (v & DATA_ENB_CHANGED) {
512 		regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_2);
513 		if (data->curenb)
514 			regval |= 0x01;
515 		else
516 			regval &= ~0x03;
517                 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_2, regval);
518 	}
519 
520 	if (v & DATA_CURCMAP_CHANGED) {
521 		/* addr[9:0] assumed to be 0 */
522 		/* set addr[7:0] to 1 */
523                 data->ramdac_wr(data->cookie, BT485_REG_COC_WRADDR, 0x01);
524 
525 		/* spit out the cursor data */
526 		for (i = 0; i < 2; i++) {
527                 	data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
528 			    data->curcmap_r[i]);
529                 	data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
530 			    data->curcmap_g[i]);
531                 	data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
532 			    data->curcmap_b[i]);
533 		}
534 	}
535 
536 	if (v & DATA_CURSHAPE_CHANGED) {
537 		count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
538 
539 		/*
540 		 * Write the cursor image data:
541 		 *	set addr[9:8] to 0,
542 		 *	set addr[7:0] to 0,
543 		 *	spit it all out.
544 		 */
545 		regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
546 		regval &= ~0x03;
547 		bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
548                 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0);
549 		for (i = 0; i < count; i++)
550 			data->ramdac_wr(data->cookie, BT485_REG_CURSOR_RAM,
551 			    data->curimage[i]);
552 
553 		/*
554 		 * Write the cursor mask data:
555 		 *	set addr[9:8] to 2,
556 		 *	set addr[7:0] to 0,
557 		 *	spit it all out.
558 		 */
559 		regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
560 		regval &= ~0x03; regval |= 0x02;
561 		bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
562                 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0);
563 		for (i = 0; i < count; i++)
564 			data->ramdac_wr(data->cookie, BT485_REG_CURSOR_RAM,
565 			    data->curmask[i]);
566 
567 		/* set addr[9:0] back to 0 */
568 		regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
569 		regval &= ~0x03;
570 		bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
571 	}
572 
573 	if (v & DATA_CMAP_CHANGED) {
574 		/* addr[9:0] assumed to be 0 */
575 		/* set addr[7:0] to 0 */
576                 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0x00);
577 
578 		/* spit out the cursor data */
579 		for (i = 0; i < 256; i++) {
580                 	data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
581 			    data->cmap_r[i]);
582                 	data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
583 			    data->cmap_g[i]);
584                 	data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
585 			    data->cmap_b[i]);
586 		}
587 	}
588 }
589 
590 void
591 bt485_update_curpos(data)
592 	struct bt485data *data;
593 {
594 	void *cookie = data->cookie;
595 	int s, x, y;
596 
597 	s = spltty();
598 
599 	x = data->curpos.x + CURSOR_MAX_SIZE - data->curhot.x;
600 	y = data->curpos.y + CURSOR_MAX_SIZE - data->curhot.y;
601 	data->ramdac_wr(cookie, BT485_REG_CURSOR_X_LOW, x & 0xff);
602 	data->ramdac_wr(cookie, BT485_REG_CURSOR_X_HIGH, (x >> 8) & 0x0f);
603 	data->ramdac_wr(cookie, BT485_REG_CURSOR_Y_LOW, y & 0xff);
604 	data->ramdac_wr(cookie, BT485_REG_CURSOR_Y_HIGH, (y >> 8) & 0x0f);
605 
606 	splx(s);
607 }
608