Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / ic / bt463.c
blob5f73af47f91b1df795e097987dbd9fac999925e3
1 /* $NetBSD: bt463.c,v 1.13 2008/04/28 20:23:49 martin Exp $ */
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
35 * All rights reserved.
37 * Author: Chris G. Demetriou
39 * Permission to use, copy, modify and distribute this software and
40 * its documentation is hereby granted, provided that both the copyright
41 * notice and this permission notice appear in all copies of the
42 * software, derivative works or modified versions, and any portions
43 * thereof, and that both notices appear in supporting documentation.
45 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 * Carnegie Mellon requests users of this software to return to
51 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
52 * School of Computer Science
53 * Carnegie Mellon University
54 * Pittsburgh PA 15213-3890
56 * any improvements or extensions that they make and grant Carnegie the
57 * rights to redistribute these changes.
60 /* This code was derived from and originally located in sys/dev/pci/
61 * NetBSD: tga_bt463.c,v 1.5 2000/03/04 10:27:59 elric Exp
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: bt463.c,v 1.13 2008/04/28 20:23:49 martin Exp $");
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/device.h>
70 #include <sys/buf.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
74 #include <uvm/uvm_extern.h>
76 #include <dev/pci/pcivar.h>
77 #include <dev/pci/tgareg.h>
78 #include <dev/pci/tgavar.h>
79 #include <dev/ic/bt463reg.h>
80 #include <dev/ic/bt463var.h>
82 #include <dev/wscons/wsconsio.h>
85 * Functions exported via the RAMDAC configuration table.
87 void bt463_init(struct ramdac_cookie *);
88 int bt463_set_cmap(struct ramdac_cookie *, struct wsdisplay_cmap *);
89 int bt463_get_cmap(struct ramdac_cookie *, struct wsdisplay_cmap *);
90 int bt463_set_cursor(struct ramdac_cookie *, struct wsdisplay_cursor *);
91 int bt463_get_cursor(struct ramdac_cookie *, struct wsdisplay_cursor *);
92 int bt463_set_curpos(struct ramdac_cookie *, struct wsdisplay_curpos *);
93 int bt463_get_curpos(struct ramdac_cookie *, struct wsdisplay_curpos *);
94 int bt463_get_curmax(struct ramdac_cookie *, struct wsdisplay_curpos *);
95 int bt463_check_curcmap(struct ramdac_cookie *,
96 struct wsdisplay_cursor *cursorp);
97 void bt463_set_curcmap(struct ramdac_cookie *,
98 struct wsdisplay_cursor *cursorp);
99 int bt463_get_curcmap(struct ramdac_cookie *,
100 struct wsdisplay_cursor *cursorp);
102 #ifdef BT463_DEBUG
103 int bt463_store(void *);
104 int bt463_debug(void *);
105 int bt463_readback(void *);
106 void bt463_copyback(void *);
107 #endif
109 struct ramdac_funcs bt463_funcsstruct = {
110 "Bt463",
111 bt463_register,
112 bt463_init,
113 bt463_set_cmap,
114 bt463_get_cmap,
115 bt463_set_cursor,
116 bt463_get_cursor,
117 bt463_set_curpos,
118 bt463_get_curpos,
119 bt463_get_curmax,
120 bt463_check_curcmap,
121 bt463_set_curcmap,
122 bt463_get_curcmap,
123 NULL,
127 * Private data.
129 struct bt463data {
130 void *cookie; /* This is what is passed
131 * around, and is probably
132 * struct tga_devconfig *
135 int (*ramdac_sched_update)(void *, void (*)(void *));
136 void (*ramdac_wr)(void *, u_int, u_int8_t);
137 u_int8_t (*ramdac_rd)(void *, u_int);
139 int changed; /* what changed; see below */
140 char curcmap_r[2]; /* cursor colormap */
141 char curcmap_g[2];
142 char curcmap_b[2];
143 char tmpcurcmap_r[2]; /* tmp cursor colormap */
144 char tmpcurcmap_g[2];
145 char tmpcurcmap_b[2];
146 char cmap_r[BT463_NCMAP_ENTRIES]; /* colormap */
147 char cmap_g[BT463_NCMAP_ENTRIES];
148 char cmap_b[BT463_NCMAP_ENTRIES];
149 int window_type[16]; /* 16 24-bit window type table entries */
152 /* When we're doing console initialization, there's no
153 * way to get our cookie back to the video card's softc
154 * before we call sched_update. So we stash it here,
155 * and bt463_update will look for it here first.
157 static struct bt463data *console_data;
160 #define BTWREG(data, addr, val) do { bt463_wraddr((data), (addr)); \
161 (data)->ramdac_wr((data)->cookie, BT463_REG_IREG_DATA, (val)); } while (0)
162 #define BTWNREG(data, val) (data)->ramdac_wr((data)->cookie, \
163 BT463_REG_IREG_DATA, (val))
164 #define BTRREG(data, addr) (bt463_wraddr((data), (addr)), \
165 (data)->ramdac_rd((data)->cookie, BT463_REG_IREG_DATA))
166 #define BTRNREG(data) ((data)->ramdac_rd((data)->cookie, BT463_REG_IREG_DATA))
168 #define DATA_CURCMAP_CHANGED 0x01 /* cursor colormap changed */
169 #define DATA_CMAP_CHANGED 0x02 /* colormap changed */
170 #define DATA_WTYPE_CHANGED 0x04 /* window type table changed */
171 #define DATA_ALL_CHANGED 0x07
174 * Internal functions.
176 inline void bt463_wraddr(struct bt463data *, u_int16_t);
178 void bt463_update(void *);
181 /*****************************************************************************/
184 * Functions exported via the RAMDAC configuration table.
187 struct ramdac_funcs *
188 bt463_funcs(void)
190 return &bt463_funcsstruct;
193 struct ramdac_cookie *
194 bt463_register(v, sched_update, wr, rd)
195 void *v;
196 int (*sched_update)(void *, void (*)(void *));
197 void (*wr)(void *, u_int, u_int8_t);
198 u_int8_t (*rd)(void *, u_int);
200 struct bt463data *data;
202 * XXX -- comment out of date. rcd.
203 * If we should allocate a new private info struct, do so.
204 * Otherwise, use the one we have (if it's there), or
205 * use the temporary one on the stack.
207 data = malloc(sizeof *data, M_DEVBUF, M_WAITOK);
208 /* XXX -- if !data */
209 data->cookie = v;
210 data->ramdac_sched_update = sched_update;
211 data->ramdac_wr = wr;
212 data->ramdac_rd = rd;
213 return (struct ramdac_cookie *)data;
217 * This function exists solely to provide a means to init
218 * the RAMDAC without first registering. It is useful for
219 * initializing the console early on.
221 void
222 bt463_cninit(v, sched_update, wr, rd)
223 void *v;
224 int (*sched_update)(void *, void (*)(void *));
225 void (*wr)(void *, u_int, u_int8_t);
226 u_int8_t (*rd)(void *, u_int);
228 struct bt463data tmp, *data = &tmp;
229 data->cookie = v;
230 data->ramdac_sched_update = sched_update;
231 data->ramdac_wr = wr;
232 data->ramdac_rd = rd;
233 /* Set up console_data so that when bt463_update is called back,
234 * it can use the right information.
236 console_data = data;
237 bt463_init((struct ramdac_cookie *)data);
238 console_data = NULL;
241 void
242 bt463_init(struct ramdac_cookie *rc)
244 struct bt463data *data = (struct bt463data *)rc;
246 int i;
249 * Init the BT463 for normal operation.
254 * Setup:
255 * reg 0: 4:1 multiplexing, 25/75 blink.
256 * reg 1: Overlay mapping: mapped to common palette,
257 * 14 window type entries, 24-plane configuration mode,
258 * 4 overlay planes, underlays disabled, no cursor.
259 * reg 2: sync-on-green enabled, pedestal enabled.
262 BTWREG(data, BT463_IREG_COMMAND_0, 0x40);
263 BTWREG(data, BT463_IREG_COMMAND_1, 0x48);
264 BTWREG(data, BT463_IREG_COMMAND_2, 0x40);
267 * Initialize the read mask.
269 bt463_wraddr(data, BT463_IREG_READ_MASK_P0_P7);
270 for (i = 0; i < 4; i++)
271 BTWNREG(data, 0xff);
274 * Initialize the blink mask.
276 bt463_wraddr(data, BT463_IREG_BLINK_MASK_P0_P7);
277 for (i = 0; i < 4; i++)
278 BTWNREG(data, 0);
282 * Clear test register
284 BTWREG(data, BT463_IREG_TEST, 0);
287 * Initialize the RAMDAC info struct to hold all of our
288 * data, and fill it in.
290 data->changed = DATA_ALL_CHANGED;
292 /* initial cursor colormap: 0 is black, 1 is white */
293 data->curcmap_r[0] = data->curcmap_g[0] = data->curcmap_b[0] = 0;
294 data->curcmap_r[1] = data->curcmap_g[1] = data->curcmap_b[1] = 0xff;
296 /* Initial colormap: 0 is black, everything else is white */
297 data->cmap_r[0] = data->cmap_g[0] = data->cmap_b[0] = 0;
298 for (i = 1; i < 256; i++)
299 data->cmap_r[i] = data->cmap_g[i] = data->cmap_b[i] = 255;
302 /* Initialize the window type table:
304 * Entry 0: 24-plane truecolor, overlays enabled, bypassed.
306 * Lookup table bypass: yes ( 1 << 23 & 0x800000) 800000
307 * Colormap address: 0x000 (0x000 << 17 & 0x7e0000) 0
308 * Overlay mask: 0xf ( 0xf << 13 & 0x01e000) 1e000
309 * Overlay location: P<27:24> ( 0 << 12 & 0x001000) 0
310 * Display mode: Truecolor ( 0 << 9 & 0x000e00) 000
311 * Number of planes: 8 ( 8 << 5 & 0x0001e0) 100
312 * Plane shift: 0 ( 0 << 0 & 0x00001f) 0
313 * --------
314 * 0x81e100
316 data->window_type[0] = 0x81e100;
318 /* Entry 1: 8-plane pseudocolor in the bottom 8 bits,
319 * overlays enabled, colormap starting at 0.
321 * Lookup table bypass: no ( 0 << 23 & 0x800000) 0
322 * Colormap address: 0x000 (0x000 << 17 & 0x7e0000) 0
323 * Overlay mask: 0xf ( 0xf << 13 & 0x01e000) 0x1e000
324 * Overlay location: P<27:24> ( 0 << 12 & 0x001000) 0
325 * Display mode: Pseudocolor ( 1 << 9 & 0x000e00) 0x200
326 * Number of planes: 8 ( 8 << 5 & 0x0001e0) 0x100
327 * Plane shift: 16 ( 0x10 << 0 & 0x00001f) 10
328 * --------
329 * 0x01e310
331 data->window_type[1] = 0x01e310;
333 /* The colormap interface to the world only supports one colormap,
334 * so having an entry for the 'alternate' colormap in the bt463
335 * probably isn't useful.
338 /* Fill the remaining table entries with clones of entry 0 until we
339 * figure out a better use for them.
342 for (i = 2; i < BT463_NWTYPE_ENTRIES; i++) {
343 data->window_type[i] = 0x81e100;
346 data->ramdac_sched_update(data->cookie, bt463_update);
351 bt463_set_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp)
353 struct bt463data *data = (struct bt463data *)rc;
354 u_int count, index;
355 uint8_t r[BT463_NCMAP_ENTRIES];
356 uint8_t g[BT463_NCMAP_ENTRIES];
357 uint8_t b[BT463_NCMAP_ENTRIES];
358 int s, error;
360 if (cmapp->index >= BT463_NCMAP_ENTRIES ||
361 cmapp->count > BT463_NCMAP_ENTRIES - cmapp->index)
362 return (EINVAL);
364 index = cmapp->index;
365 count = cmapp->count;
366 error = copyin(cmapp->red, &r[index], count);
367 if (error)
368 return error;
369 error = copyin(cmapp->green, &g[index], count);
370 if (error)
371 return error;
372 error = copyin(cmapp->blue, &b[index], count);
373 if (error)
374 return error;
375 s = spltty();
376 memcpy(&data->cmap_r[index], &r[index], count);
377 memcpy(&data->cmap_g[index], &g[index], count);
378 memcpy(&data->cmap_b[index], &b[index], count);
379 data->changed |= DATA_CMAP_CHANGED;
380 data->ramdac_sched_update(data->cookie, bt463_update);
381 splx(s);
382 return (0);
386 bt463_get_cmap(struct ramdac_cookie *rc, struct wsdisplay_cmap *cmapp)
388 struct bt463data *data = (struct bt463data *)rc;
389 u_int count, index;
390 int error;
392 if (cmapp->index >= BT463_NCMAP_ENTRIES ||
393 cmapp->count > BT463_NCMAP_ENTRIES - cmapp->index)
394 return (EINVAL);
396 count = cmapp->count;
397 index = cmapp->index;
399 error = copyout(&data->cmap_r[index], cmapp->red, count);
400 if (error)
401 return (error);
402 error = copyout(&data->cmap_g[index], cmapp->green, count);
403 if (error)
404 return (error);
405 error = copyout(&data->cmap_b[index], cmapp->blue, count);
406 return (error);
410 bt463_check_curcmap(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp)
412 struct bt463data *data = (struct bt463data *)rc;
413 int count, index, error;
415 if (cursorp->cmap.index > 2 ||
416 (cursorp->cmap.index + cursorp->cmap.count) > 2)
417 return (EINVAL);
418 count = cursorp->cmap.count;
419 index = cursorp->cmap.index;
420 error = copyin(cursorp->cmap.red, &data->tmpcurcmap_r[index], count);
421 if (error)
422 return error;
423 error = copyin(cursorp->cmap.green, &data->tmpcurcmap_g[index], count);
424 if (error)
425 return error;
426 error = copyin(cursorp->cmap.blue, &data->tmpcurcmap_b[index], count);
427 if (error)
428 return error;
429 return (0);
432 void
433 bt463_set_curcmap(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp)
435 struct bt463data *data = (struct bt463data *)rc;
436 int count, index;
438 count = cursorp->cmap.count;
439 index = cursorp->cmap.index;
440 memcpy(&data->curcmap_r[index], &data->tmpcurcmap_r[index], count);
441 memcpy(&data->curcmap_g[index], &data->tmpcurcmap_g[index], count);
442 memcpy(&data->curcmap_b[index], &data->tmpcurcmap_b[index], count);
443 data->changed |= DATA_CURCMAP_CHANGED;
444 data->ramdac_sched_update(data->cookie, bt463_update);
448 bt463_get_curcmap(struct ramdac_cookie *rc, struct wsdisplay_cursor *cursorp)
450 struct bt463data *data = (struct bt463data *)rc;
451 int error;
453 cursorp->cmap.index = 0; /* DOCMAP */
454 cursorp->cmap.count = 2;
455 if (cursorp->cmap.red != NULL) {
456 error = copyout(data->curcmap_r, cursorp->cmap.red, 2);
457 if (error)
458 return (error);
460 if (cursorp->cmap.green != NULL) {
461 error = copyout(data->curcmap_g, cursorp->cmap.green, 2);
462 if (error)
463 return (error);
465 if (cursorp->cmap.blue != NULL) {
466 error = copyout(data->curcmap_b, cursorp->cmap.blue, 2);
467 if (error)
468 return (error);
470 return (0);
474 /*****************************************************************************/
477 * Internal functions.
480 #ifdef BT463_DEBUG
481 int bt463_store(void *v)
483 struct bt463data *data = (struct bt463data *)v;
485 data->changed = DATA_ALL_CHANGED;
486 data->ramdac_sched_update(data->cookie, bt463_update);
487 printf("Scheduled bt463 store\n");
489 return 0;
493 int bt463_readback(void *v)
495 struct bt463data *data = (struct bt463data *)v;
497 data->ramdac_sched_update(data->cookie, bt463_copyback);
498 printf("Scheduled bt463 copyback\n");
499 return 0;
503 bt463_debug(void *v)
505 struct bt463data *data = (struct bt463data *)v;
506 int i;
507 u_int8_t val;
509 printf("BT463 main regs:\n");
510 for (i = 0x200; i < 0x20F; i ++) {
511 val = BTRREG(data, i);
512 printf(" $%04x %02x\n", i, val);
515 printf("BT463 revision register:\n");
516 val = BTRREG(data, 0x220);
517 printf(" $%04x %02x\n", 0x220, val);
519 printf("BT463 window type table (from softc):\n");
521 for (i = 0; i < BT463_NWTYPE_ENTRIES; i++) {
522 printf("%02x %06x\n", i, data->window_type[i]);
525 return 0;
528 void
529 bt463_copyback(void *p)
531 struct bt463data *data = (struct bt463data *)p;
532 int i;
534 for (i = 0; i < BT463_NWTYPE_ENTRIES; i++) {
535 bt463_wraddr(data, BT463_IREG_WINDOW_TYPE_TABLE + i);
536 data->window_type[i] = (BTRNREG(data) & 0xff); /* B0-7 */
537 data->window_type[i] |= (BTRNREG(data) & 0xff) << 8; /* B8-15 */
538 data->window_type[i] |= (BTRNREG(data) & 0xff) << 16; /* B16-23 */
541 #endif
543 inline void
544 bt463_wraddr(struct bt463data *data, u_int16_t ireg)
546 data->ramdac_wr(data->cookie, BT463_REG_ADDR_LOW, ireg & 0xff);
547 data->ramdac_wr(data->cookie, BT463_REG_ADDR_HIGH, (ireg >> 8) & 0xff);
550 void
551 bt463_update(void *p)
553 struct bt463data *data = (struct bt463data *)p;
554 int i, v;
556 if (console_data != NULL) {
557 /* The cookie passed in from sched_update is incorrect. Use the
558 * right one.
560 data = console_data;
563 v = data->changed;
565 /* The Bt463 won't accept window type data except during a blanking
566 * interval, so we do this early in the interrupt.
567 * Blanking the screen might also be a good idea, but it can cause
568 * unpleasant flashing and is hard to do from this side of the
569 * ramdac interface.
571 if (v & DATA_WTYPE_CHANGED) {
572 /* spit out the window type data */
573 for (i = 0; i < BT463_NWTYPE_ENTRIES; i++) {
574 bt463_wraddr(data, BT463_IREG_WINDOW_TYPE_TABLE + i);
575 BTWNREG(data, (data->window_type[i]) & 0xff); /* B0-7 */
576 BTWNREG(data, (data->window_type[i] >> 8) & 0xff); /* B8-15 */
577 BTWNREG(data, (data->window_type[i] >> 16) & 0xff); /* B16-23 */
581 if (v & DATA_CURCMAP_CHANGED) {
582 bt463_wraddr(data, BT463_IREG_CURSOR_COLOR_0);
583 /* spit out the cursor data */
584 for (i = 0; i < 2; i++) {
585 BTWNREG(data, data->curcmap_r[i]);
586 BTWNREG(data, data->curcmap_g[i]);
587 BTWNREG(data, data->curcmap_b[i]);
591 if (v & DATA_CMAP_CHANGED) {
592 bt463_wraddr(data, BT463_IREG_CPALETTE_RAM);
593 /* spit out the colormap data */
594 for (i = 0; i < BT463_NCMAP_ENTRIES; i++) {
595 data->ramdac_wr(data->cookie, BT463_REG_CMAP_DATA,
596 data->cmap_r[i]);
597 data->ramdac_wr(data->cookie, BT463_REG_CMAP_DATA,
598 data->cmap_g[i]);
599 data->ramdac_wr(data->cookie, BT463_REG_CMAP_DATA,
600 data->cmap_b[i]);
604 data->changed = 0;
607 int bt463_set_cursor (rc, cur)
608 struct ramdac_cookie *rc;
609 struct wsdisplay_cursor *cur;
611 struct bt463data *data = (struct bt463data *)rc;
612 return tga_builtin_set_cursor(data->cookie, cur);
615 int bt463_get_cursor (rc, cur)
616 struct ramdac_cookie *rc;
617 struct wsdisplay_cursor *cur;
619 struct bt463data *data = (struct bt463data *)rc;
620 return tga_builtin_get_cursor(data->cookie, cur);
623 int bt463_set_curpos (rc, cur)
624 struct ramdac_cookie *rc;
625 struct wsdisplay_curpos *cur;
627 struct bt463data *data = (struct bt463data *)rc;
628 return tga_builtin_set_curpos(data->cookie, cur);
631 int bt463_get_curpos (rc, cur)
632 struct ramdac_cookie *rc;
633 struct wsdisplay_curpos *cur;
635 struct bt463data *data = (struct bt463data *)rc;
636 return tga_builtin_get_curpos(data->cookie, cur);
639 int bt463_get_curmax (rc, cur)
640 struct ramdac_cookie *rc;
641 struct wsdisplay_curpos *cur;
643 struct bt463data *data = (struct bt463data *)rc;
644 return tga_builtin_get_curmax(data->cookie, cur);