No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / ic / bt485.c
blob43e3aa01c9a60f496a48c3f168cbc0672e08c84e
1 /* $NetBSD: bt485.c,v 1.13 2005/12/11 12:21:26 christos Exp $ */
3 /*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
7 * Author: Chris G. Demetriou
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.
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.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
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
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: bt485.c,v 1.13 2005/12/11 12:21:26 christos Exp $");
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>
44 #include <uvm/uvm_extern.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/ic/bt485reg.h>
48 #include <dev/ic/bt485var.h>
49 #include <dev/ic/ramdac.h>
51 #include <dev/wscons/wsconsio.h>
54 * Functions exported via the RAMDAC configuration table.
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 *);
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 */
84 * Private data.
86 struct bt485data {
87 void *cookie; /* This is what is passed
88 * around, and is probably
89 * struct tga_devconfig *
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);
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];
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
117 #define CURSOR_MAX_SIZE 64
120 * Internal functions.
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 *);
127 /*****************************************************************************/
130 * Functions exported via the RAMDAC configuration table.
133 struct ramdac_funcs *
134 bt485_funcs(void)
136 return &bt485_funcsstruct;
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);
146 struct bt485data *data;
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.
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;
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.
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);
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);
182 void
183 bt485_init(struct ramdac_cookie *rc)
185 u_int8_t regval;
186 struct bt485data *data = (struct bt485data *)rc;
187 int i;
190 * Init the BT485 for normal operation.
194 * Allow indirect register access. (Actually, this is
195 * already enabled. In fact, if it is _disabled_, for
196 * some reason the monitor appears to lose sync!!! (?!?!)
198 regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_0);
199 regval |= 0x80;
201 * Set the RAMDAC to 8 bit resolution, rather than 6 bit
202 * resolution.
204 regval |= 0x02;
205 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_0, regval);
207 /* Set the RAMDAC to 8BPP (no interestion options). */
208 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_1, 0x40);
210 /* Disable the cursor (for now) */
211 regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_2);
212 regval &= ~0x03;
213 regval |= 0x24;
214 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_2, regval);
216 /* Use a 64x64x2 cursor */
217 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
218 regval |= 0x04;
219 regval |= 0x08;
220 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
222 /* Set the Pixel Mask to something useful */
223 data->ramdac_wr(data->cookie, BT485_REG_PIXMASK, 0xff);
226 * Initialize the RAMDAC info struct to hold all of our
227 * data, and fill it in.
229 data->changed = DATA_ALL_CHANGED;
231 data->curenb = 0; /* cursor disabled */
232 data->curpos.x = data->curpos.y = 0; /* right now at 0,0 */
233 data->curhot.x = data->curhot.y = 0; /* hot spot at 0,0 */
235 /* initial cursor colormap: 0 is black, 1 is white */
236 data->curcmap_r[0] = data->curcmap_g[0] = data->curcmap_b[0] = 0;
237 data->curcmap_r[1] = data->curcmap_g[1] = data->curcmap_b[1] = 0xff;
239 /* initial cursor data: 64x64 block of white. */
240 data->cursize.x = data->cursize.y = 64;
241 for (i = 0; i < 512; i++)
242 data->curimage[i] = data->curmask[i] = 0xff;
244 /* Initial colormap: 0 is black, everything else is white */
245 data->cmap_r[0] = data->cmap_g[0] = data->cmap_b[0] = 0;
246 for (i = 1; i < 256; i++)
247 data->cmap_r[i] = data->cmap_g[i] = data->cmap_b[i] = 255;
249 bt485_update((void *)data);
253 bt485_set_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp)
255 struct bt485data *data = (struct bt485data *)rc;
256 u_int count, index;
257 uint8_t r[256], g[256], b[256];
258 int s, error;
260 if (cmapp->index >= 256 || cmapp->count > 256 - cmapp->index)
261 return (EINVAL);
263 index = cmapp->index;
264 count = cmapp->count;
265 error = copyin(cmapp->red, &r[index], count);
266 if (error)
267 return error;
268 error = copyin(cmapp->green, &g[index], count);
269 if (error)
270 return error;
271 error = copyin(cmapp->blue, &b[index], count);
272 if (error)
273 return error;
274 s = spltty();
275 memcpy(&data->cmap_r[index], &r[index], count);
276 memcpy(&data->cmap_g[index], &g[index], count);
277 memcpy(&data->cmap_b[index], &b[index], count);
278 data->changed |= DATA_CMAP_CHANGED;
279 data->ramdac_sched_update(data->cookie, bt485_update);
280 splx(s);
281 return (0);
285 bt485_get_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp)
287 struct bt485data *data = (struct bt485data *)rc;
288 u_int count, index;
289 int error;
291 if (cmapp->index >= 256 || cmapp->count > 256 - cmapp->index )
292 return (EINVAL);
294 count = cmapp->count;
295 index = cmapp->index;
296 error = copyout(&data->cmap_r[index], cmapp->red, count);
297 if (error)
298 return (error);
299 error = copyout(&data->cmap_g[index], cmapp->green, count);
300 if (error)
301 return (error);
302 error = copyout(&data->cmap_b[index], cmapp->blue, count);
303 return (error);
307 bt485_set_cursor(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp)
309 struct bt485data *data = (struct bt485data *)rc;
310 u_int count = 0, icount = 0, index = 0, v;
311 char r[2], g[2], b[2], image[512], mask[512];
312 int s, error;
314 v = cursorp->which;
317 * For DOCMAP and DOSHAPE, copy in the new data
318 * before we do anything that we can't recover from.
320 if (v & WSDISPLAY_CURSOR_DOCMAP) {
321 if (cursorp->cmap.index > 2 ||
322 (cursorp->cmap.index + cursorp->cmap.count) > 2)
323 return (EINVAL);
324 count = cursorp->cmap.count;
325 index = cursorp->cmap.index;
326 error = copyin(cursorp->cmap.red, &r[index], count);
327 if (error)
328 return error;
329 error = copyin(cursorp->cmap.green, &g[index], count);
330 if (error)
331 return error;
332 error = copyin(cursorp->cmap.blue, &b[index], count);
333 if (error)
334 return error;
336 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
337 if (cursorp->size.x > CURSOR_MAX_SIZE ||
338 cursorp->size.y > CURSOR_MAX_SIZE)
339 return (EINVAL);
340 icount = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
341 error = copyin(cursorp->image, image, icount);
342 if (error)
343 return error;
344 error = copyin(cursorp->mask, mask, icount);
345 if (error)
346 return error;
349 if (v & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOCUR)) {
350 if (v & WSDISPLAY_CURSOR_DOPOS)
351 data->curpos = cursorp->pos;
352 if (v & WSDISPLAY_CURSOR_DOCUR)
353 data->curhot = cursorp->hot;
354 bt485_update_curpos(data);
357 s = spltty();
359 /* Data is all available; perform the requested operations. */
360 if (v & WSDISPLAY_CURSOR_DOCUR) {
361 data->curenb = cursorp->enable;
362 data->changed |= DATA_ENB_CHANGED;
364 if (v & WSDISPLAY_CURSOR_DOCMAP) {
365 memcpy(&data->curcmap_r[index], &r[index], count);
366 memcpy(&data->curcmap_g[index], &g[index], count);
367 memcpy(&data->curcmap_b[index], &b[index], count);
368 data->changed |= DATA_CURCMAP_CHANGED;
370 if (v & WSDISPLAY_CURSOR_DOSHAPE) {
371 data->cursize = cursorp->size;
372 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
373 memset(data->curimage, 0, sizeof data->curimage);
374 memset(data->curmask, 0, sizeof data->curmask);
375 memcpy(data->curimage, image, icount);
376 memcpy(data->curmask, mask, icount);
377 data->changed |= DATA_CURSHAPE_CHANGED;
380 if (data->changed)
381 data->ramdac_sched_update(data->cookie, bt485_update);
382 splx(s);
384 return (0);
388 bt485_get_cursor(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp)
390 struct bt485data *data = (struct bt485data *)rc;
391 int error, count;
393 /* we return everything they want */
394 cursorp->which = WSDISPLAY_CURSOR_DOALL;
396 cursorp->enable = data->curenb; /* DOCUR */
397 cursorp->pos = data->curpos; /* DOPOS */
398 cursorp->hot = data->curhot; /* DOHOT */
400 cursorp->cmap.index = 0; /* DOCMAP */
401 cursorp->cmap.count = 2;
402 if (cursorp->cmap.red != NULL) {
403 error = copyout(data->curcmap_r, cursorp->cmap.red, 2);
404 if (error)
405 return (error);
407 if (cursorp->cmap.green != NULL) {
408 error = copyout(data->curcmap_g, cursorp->cmap.green, 2);
409 if (error)
410 return (error);
412 if (cursorp->cmap.blue != NULL) {
413 error = copyout(data->curcmap_b, cursorp->cmap.blue, 2);
414 if (error)
415 return (error);
418 cursorp->size = data->cursize; /* DOSHAPE */
419 if (cursorp->image != NULL) {
420 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
421 error = copyout(data->curimage, cursorp->image, count);
422 if (error)
423 return (error);
424 error = copyout(data->curmask, cursorp->mask, count);
425 if (error)
426 return (error);
429 return (0);
433 bt485_set_curpos(struct ramdac_cookie *rc, struct wsdisplay_curpos *curposp)
435 struct bt485data *data = (struct bt485data *)rc;
437 data->curpos = *curposp;
438 bt485_update_curpos(data);
440 return (0);
444 bt485_get_curpos(struct ramdac_cookie *rc, struct wsdisplay_curpos *curposp)
446 struct bt485data *data = (struct bt485data *)rc;
448 *curposp = data->curpos;
449 return (0);
453 bt485_get_curmax(struct ramdac_cookie *rc, struct wsdisplay_curpos *curposp)
456 curposp->x = curposp->y = CURSOR_MAX_SIZE;
457 return (0);
460 /*****************************************************************************/
463 * Internal functions.
466 inline void
467 bt485_wr_i(struct bt485data *data, u_int8_t ireg, u_int8_t val)
469 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, ireg);
470 data->ramdac_wr(data->cookie, BT485_REG_EXTENDED, val);
473 inline u_int8_t
474 bt485_rd_i(struct bt485data *data, u_int8_t ireg)
476 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, ireg);
477 return (data->ramdac_rd(data->cookie, BT485_REG_EXTENDED));
480 void
481 bt485_update(void *vp)
483 struct bt485data *data = vp;
484 u_int8_t regval;
485 int count, i, v;
487 v = data->changed;
488 data->changed = 0;
490 if (v & DATA_ENB_CHANGED) {
491 regval = data->ramdac_rd(data->cookie, BT485_REG_COMMAND_2);
492 if (data->curenb)
493 regval |= 0x01;
494 else
495 regval &= ~0x03;
496 data->ramdac_wr(data->cookie, BT485_REG_COMMAND_2, regval);
499 if (v & DATA_CURCMAP_CHANGED) {
500 /* addr[9:0] assumed to be 0 */
501 /* set addr[7:0] to 1 */
502 data->ramdac_wr(data->cookie, BT485_REG_COC_WRADDR, 0x01);
504 /* spit out the cursor data */
505 for (i = 0; i < 2; i++) {
506 data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
507 data->curcmap_r[i]);
508 data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
509 data->curcmap_g[i]);
510 data->ramdac_wr(data->cookie, BT485_REG_COCDATA,
511 data->curcmap_b[i]);
515 if (v & DATA_CURSHAPE_CHANGED) {
516 count = (CURSOR_MAX_SIZE / NBBY) * data->cursize.y;
519 * Write the cursor image data:
520 * set addr[9:8] to 0,
521 * set addr[7:0] to 0,
522 * spit it all out.
524 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
525 regval &= ~0x03;
526 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
527 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0);
528 for (i = 0; i < count; i++)
529 data->ramdac_wr(data->cookie, BT485_REG_CURSOR_RAM,
530 data->curimage[i]);
533 * Write the cursor mask data:
534 * set addr[9:8] to 2,
535 * set addr[7:0] to 0,
536 * spit it all out.
538 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
539 regval &= ~0x03; regval |= 0x02;
540 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
541 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0);
542 for (i = 0; i < count; i++)
543 data->ramdac_wr(data->cookie, BT485_REG_CURSOR_RAM,
544 data->curmask[i]);
546 /* set addr[9:0] back to 0 */
547 regval = bt485_rd_i(data, BT485_IREG_COMMAND_3);
548 regval &= ~0x03;
549 bt485_wr_i(data, BT485_IREG_COMMAND_3, regval);
552 if (v & DATA_CMAP_CHANGED) {
553 /* addr[9:0] assumed to be 0 */
554 /* set addr[7:0] to 0 */
555 data->ramdac_wr(data->cookie, BT485_REG_PCRAM_WRADDR, 0x00);
557 /* spit out the cursor data */
558 for (i = 0; i < 256; i++) {
559 data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
560 data->cmap_r[i]);
561 data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
562 data->cmap_g[i]);
563 data->ramdac_wr(data->cookie, BT485_REG_PALETTE,
564 data->cmap_b[i]);
569 void
570 bt485_update_curpos(struct bt485data *data)
572 void *cookie = data->cookie;
573 int s, x, y;
575 s = spltty();
577 x = data->curpos.x + CURSOR_MAX_SIZE - data->curhot.x;
578 y = data->curpos.y + CURSOR_MAX_SIZE - data->curhot.y;
579 data->ramdac_wr(cookie, BT485_REG_CURSOR_X_LOW, x & 0xff);
580 data->ramdac_wr(cookie, BT485_REG_CURSOR_X_HIGH, (x >> 8) & 0x0f);
581 data->ramdac_wr(cookie, BT485_REG_CURSOR_Y_LOW, y & 0xff);
582 data->ramdac_wr(cookie, BT485_REG_CURSOR_Y_HIGH, (y >> 8) & 0x0f);
584 splx(s);