tcx.c revision 1.1 1 /* $NetBSD: tcx.c,v 1.1 2000/08/20 22:27:07 pk Exp $ */
2
3 /*
4 * Copyright (c) 1996,1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * color display (TCX) driver.
41 *
42 * Does not handle interrupts, even though they can occur.
43 *
44 * XXX should defer colormap updates to vertical retrace interrupts
45 */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/buf.h>
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #include <sys/malloc.h>
53 #include <sys/mman.h>
54 #include <sys/tty.h>
55 #include <sys/conf.h>
56
57 #ifdef DEBUG
58 #include <sys/proc.h>
59 #include <sys/syslog.h>
60 #endif
61
62 #include <machine/bus.h>
63 #include <machine/autoconf.h>
64
65 #include <dev/sbus/sbusvar.h>
66
67 #include <dev/sun/fbio.h>
68 #include <dev/sun/fbvar.h>
69
70 #include <dev/sun/btreg.h>
71 #include <dev/sun/btvar.h>
72 #include <dev/sun/tcxreg.h>
73
74 #include <machine/conf.h>
75
76 /* per-display variables */
77 struct tcx_softc {
78 struct device sc_dev; /* base device */
79 struct sbusdev sc_sd; /* sbus device */
80 struct fbdevice sc_fb; /* frame buffer device */
81 bus_space_tag_t sc_bustag;
82 struct sbus_reg sc_physadr[TCX_NREG]; /* phys addr of h/w */
83
84 volatile struct bt_regs *sc_bt; /* Brooktree registers */
85 volatile struct tcx_thc *sc_thc;/* THC registers */
86 short sc_blanked; /* true if blanked */
87 union bt_cmap sc_cmap; /* Brooktree color map */
88 };
89
90 /* autoconfiguration driver */
91 static void tcxattach __P((struct device *, struct device *, void *));
92 static int tcxmatch __P((struct device *, struct cfdata *, void *));
93 static void tcx_unblank __P((struct device *));
94
95 /* cdevsw prototypes */
96 cdev_decl(tcx);
97
98 struct cfattach tcx_ca = {
99 sizeof(struct tcx_softc), tcxmatch, tcxattach
100 };
101
102 extern struct cfdriver tcx_cd;
103
104 /* frame buffer generic driver */
105 static struct fbdriver tcx_fbdriver = {
106 tcx_unblank, tcxopen, tcxclose, tcxioctl, tcxpoll, tcxmmap
107 };
108
109 static void tcx_reset __P((struct tcx_softc *));
110 static void tcx_loadcmap __P((struct tcx_softc *, int, int));
111
112 #define OBPNAME "SUNW,tcx"
113 /*
114 * Match a tcx.
115 */
116 int
117 tcxmatch(parent, cf, aux)
118 struct device *parent;
119 struct cfdata *cf;
120 void *aux;
121 {
122 struct sbus_attach_args *sa = aux;
123
124 return (strcmp(sa->sa_name, OBPNAME) == 0);
125 }
126
127 /*
128 * Attach a display.
129 */
130 void
131 tcxattach(parent, self, args)
132 struct device *parent, *self;
133 void *args;
134 {
135 struct tcx_softc *sc = (struct tcx_softc *)self;
136 struct sbus_attach_args *sa = args;
137 int node, ramsize;
138 volatile struct bt_regs *bt;
139 struct fbdevice *fb = &sc->sc_fb;
140 bus_space_handle_t bh;
141 int isconsole;
142
143 sc->sc_bustag = sa->sa_bustag;
144 node = sa->sa_node;
145
146 fb->fb_driver = &tcx_fbdriver;
147 fb->fb_device = &sc->sc_dev;
148 /* Mask out invalid flags from the user. */
149 fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
150 fb->fb_type.fb_depth = node_has_property(node, "tcx-24-bit")
151 ? 24
152 : (node_has_property(node, "tcx-8-bit")
153 ? 8
154 : 8);
155
156 fb_setsize_obp(fb, fb->fb_type.fb_depth, 1152, 900, node);
157
158 ramsize = fb->fb_type.fb_height * fb->fb_linebytes;
159 fb->fb_type.fb_cmsize = 256;
160 fb->fb_type.fb_size = ramsize;
161 printf(": %s, %d x %d", OBPNAME,
162 fb->fb_type.fb_width,
163 fb->fb_type.fb_height);
164
165 /*
166 * XXX - should be set to FBTYPE_TCX.
167 * XXX For CG3 emulation to work in current (96/6) X11 servers,
168 * XXX `fbtype' must point to an "unregocnised" entry.
169 */
170 fb->fb_type.fb_type = FBTYPE_RESERVED3;
171
172
173 if (sa->sa_nreg != TCX_NREG) {
174 printf("%s: only %d register sets\n",
175 self->dv_xname, sa->sa_nreg);
176 return;
177 }
178 bcopy(sa->sa_reg, sc->sc_physadr,
179 sa->sa_nreg * sizeof(struct sbus_reg));
180
181 /* XXX - fix THC and TEC offsets */
182 sc->sc_physadr[TCX_REG_TEC].sbr_offset += 0x1000;
183 sc->sc_physadr[TCX_REG_THC].sbr_offset += 0x1000;
184
185 /* Map the register banks we care about */
186 if (sbus_bus_map(sa->sa_bustag,
187 (bus_type_t)sc->sc_physadr[TCX_REG_THC].sbr_slot,
188 (bus_addr_t)sc->sc_physadr[TCX_REG_THC].sbr_offset,
189 sizeof (struct tcx_thc),
190 BUS_SPACE_MAP_LINEAR,
191 0, &bh) != 0) {
192 printf("tcxattach: cannot map thc registers\n");
193 return;
194 }
195 sc->sc_thc = (volatile struct tcx_thc *)bh;
196
197 if (sbus_bus_map(sa->sa_bustag,
198 (bus_type_t)sc->sc_physadr[TCX_REG_CMAP].sbr_slot,
199 (bus_addr_t)sc->sc_physadr[TCX_REG_CMAP].sbr_offset,
200 sizeof (struct bt_regs),
201 BUS_SPACE_MAP_LINEAR,
202 0, &bh) != 0) {
203 printf("tcxattach: cannot map bt registers\n");
204 return;
205 }
206 sc->sc_bt = bt = (volatile struct bt_regs *)bh;
207
208 isconsole = fb_is_console(node);
209
210 printf(", id %d, rev %d, sense %d",
211 (sc->sc_thc->thc_config & THC_CFG_FBID) >> THC_CFG_FBID_SHIFT,
212 (sc->sc_thc->thc_config & THC_CFG_REV) >> THC_CFG_REV_SHIFT,
213 (sc->sc_thc->thc_config & THC_CFG_SENSE) >> THC_CFG_SENSE_SHIFT
214 );
215
216 /* reset cursor & frame buffer controls */
217 tcx_reset(sc);
218
219 /* Initialize the default color map. */
220 bt_initcmap(&sc->sc_cmap, 256);
221 tcx_loadcmap(sc, 0, 256);
222
223 /* enable video */
224 sc->sc_thc->thc_hcmisc |= THC_MISC_VIDEN;
225
226 if (isconsole) {
227 printf(" (console)\n");
228 } else
229 printf("\n");
230
231 sbus_establish(&sc->sc_sd, &sc->sc_dev);
232 fb_attach(&sc->sc_fb, isconsole);
233 }
234
235 int
236 tcxopen(dev, flags, mode, p)
237 dev_t dev;
238 int flags, mode;
239 struct proc *p;
240 {
241 int unit = minor(dev);
242
243 if (unit >= tcx_cd.cd_ndevs || tcx_cd.cd_devs[unit] == NULL)
244 return (ENXIO);
245 return (0);
246 }
247
248 int
249 tcxclose(dev, flags, mode, p)
250 dev_t dev;
251 int flags, mode;
252 struct proc *p;
253 {
254 struct tcx_softc *sc = tcx_cd.cd_devs[minor(dev)];
255
256 tcx_reset(sc);
257 return (0);
258 }
259
260 int
261 tcxioctl(dev, cmd, data, flags, p)
262 dev_t dev;
263 u_long cmd;
264 caddr_t data;
265 int flags;
266 struct proc *p;
267 {
268 struct tcx_softc *sc = tcx_cd.cd_devs[minor(dev)];
269 int error;
270
271 switch (cmd) {
272
273 case FBIOGTYPE:
274 *(struct fbtype *)data = sc->sc_fb.fb_type;
275 break;
276
277 case FBIOGATTR:
278 #define fba ((struct fbgattr *)data)
279 fba->real_type = sc->sc_fb.fb_type.fb_type;
280 fba->owner = 0; /* XXX ??? */
281 fba->fbtype = sc->sc_fb.fb_type;
282 fba->sattr.flags = 0;
283 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
284 fba->sattr.dev_specific[0] = -1;
285 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
286 fba->emu_types[1] = FBTYPE_SUN3COLOR;
287 fba->emu_types[2] = -1;
288 #undef fba
289 break;
290
291 case FBIOGETCMAP:
292 #define p ((struct fbcmap *)data)
293 return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
294
295 case FBIOPUTCMAP:
296 /* copy to software map */
297 error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
298 if (error)
299 return (error);
300 /* now blast them into the chip */
301 /* XXX should use retrace interrupt */
302 tcx_loadcmap(sc, p->index, p->count);
303 #undef p
304 break;
305
306 case FBIOGVIDEO:
307 *(int *)data = sc->sc_blanked;
308 break;
309
310 case FBIOSVIDEO:
311 if (*(int *)data)
312 tcx_unblank(&sc->sc_dev);
313 else if (!sc->sc_blanked) {
314 sc->sc_blanked = 1;
315 sc->sc_thc->thc_hcmisc &= ~THC_MISC_VIDEN;
316 /* Put monitor in `power-saving mode' */
317 sc->sc_thc->thc_hcmisc |= THC_MISC_VSYNC_DISABLE;
318 sc->sc_thc->thc_hcmisc |= THC_MISC_HSYNC_DISABLE;
319 }
320 break;
321
322 default:
323 #ifdef DEBUG
324 log(LOG_NOTICE, "tcxioctl(0x%lx) (%s[%d])\n", cmd,
325 p->p_comm, p->p_pid);
326 #endif
327 return (ENOTTY);
328 }
329 return (0);
330 }
331
332 int
333 tcxpoll(dev, events, p)
334 dev_t dev;
335 int events;
336 struct proc *p;
337 {
338
339 return (seltrue(dev, events, p));
340 }
341
342 /*
343 * Clean up hardware state (e.g., after bootup or after X crashes).
344 */
345 static void
346 tcx_reset(sc)
347 struct tcx_softc *sc;
348 {
349 volatile struct bt_regs *bt;
350
351 /* Enable cursor in Brooktree DAC. */
352 bt = sc->sc_bt;
353 bt->bt_addr = 0x06 << 24;
354 bt->bt_ctrl |= 0x03 << 24;
355 }
356
357 /*
358 * Load a subset of the current (new) colormap into the color DAC.
359 */
360 static void
361 tcx_loadcmap(sc, start, ncolors)
362 struct tcx_softc *sc;
363 int start, ncolors;
364 {
365 volatile struct bt_regs *bt;
366 u_int *ip, i;
367 int count;
368
369 ip = &sc->sc_cmap.cm_chip[BT_D4M3(start)]; /* start/4 * 3 */
370 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
371 bt = sc->sc_bt;
372 bt->bt_addr = BT_D4M4(start) << 24;
373 while (--count >= 0) {
374 i = *ip++;
375 /* hardware that makes one want to pound boards with hammers */
376 bt->bt_cmap = i;
377 bt->bt_cmap = i << 8;
378 bt->bt_cmap = i << 16;
379 bt->bt_cmap = i << 24;
380 }
381 }
382
383 static void
384 tcx_unblank(dev)
385 struct device *dev;
386 {
387 struct tcx_softc *sc = (struct tcx_softc *)dev;
388
389 if (sc->sc_blanked) {
390 sc->sc_blanked = 0;
391 sc->sc_thc->thc_hcmisc &= ~THC_MISC_VSYNC_DISABLE;
392 sc->sc_thc->thc_hcmisc &= ~THC_MISC_HSYNC_DISABLE;
393 sc->sc_thc->thc_hcmisc |= THC_MISC_VIDEN;
394 }
395 }
396
397 /*
398 * Base addresses at which users can mmap() the various pieces of a tcx.
399 */
400 #define TCX_USER_RAM 0x00000000
401 #define TCX_USER_RAM24 0x01000000
402 #define TCX_USER_RAM_COMPAT 0x04000000 /* cg3 emulation */
403 #define TCX_USER_STIP 0x10000000
404 #define TCX_USER_BLIT 0x20000000
405 #define TCX_USER_RDFB32 0x28000000
406 #define TCX_USER_RSTIP 0x30000000
407 #define TCX_USER_RBLIT 0x38000000
408 #define TCX_USER_TEC 0x70001000
409 #define TCX_USER_BTREGS 0x70002000
410 #define TCX_USER_THC 0x70004000
411 #define TCX_USER_DHC 0x70008000
412 #define TCX_USER_ALT 0x7000a000
413 #define TCX_USER_UART 0x7000c000
414 #define TCX_USER_VRT 0x7000e000
415 #define TCX_USER_ROM 0x70010000
416
417 struct mmo {
418 u_int mo_uaddr; /* user (virtual) address */
419 u_int mo_size; /* size, or 0 for video ram size */
420 u_int mo_bank; /* register bank number */
421 };
422
423 /*
424 * Return the address that would map the given device at the given
425 * offset, allowing for the given protection, or return -1 for error.
426 *
427 * XXX needs testing against `demanding' applications (e.g., aviator)
428 */
429 paddr_t
430 tcxmmap(dev, off, prot)
431 dev_t dev;
432 off_t off;
433 int prot;
434 {
435 struct tcx_softc *sc = tcx_cd.cd_devs[minor(dev)];
436 bus_space_handle_t bh;
437 struct sbus_reg *rr = sc->sc_physadr;
438 struct mmo *mo;
439 u_int u, sz;
440 static struct mmo mmo[] = {
441 { TCX_USER_RAM, 0, TCX_REG_DFB8 },
442 { TCX_USER_RAM24, 0, TCX_REG_DFB24 },
443 { TCX_USER_RAM_COMPAT, 0, TCX_REG_DFB8 },
444
445 { TCX_USER_STIP, 1, TCX_REG_STIP },
446 { TCX_USER_BLIT, 1, TCX_REG_BLIT },
447 { TCX_USER_RDFB32, 1, TCX_REG_RDFB32 },
448 { TCX_USER_RSTIP, 1, TCX_REG_RSTIP },
449 { TCX_USER_RBLIT, 1, TCX_REG_RBLIT },
450 { TCX_USER_TEC, 1, TCX_REG_TEC },
451 { TCX_USER_BTREGS, 8192 /* XXX */, TCX_REG_CMAP },
452 { TCX_USER_THC, sizeof(struct tcx_thc), TCX_REG_THC },
453 { TCX_USER_DHC, 1, TCX_REG_DHC },
454 { TCX_USER_ALT, 1, TCX_REG_ALT },
455 { TCX_USER_ROM, 65536, TCX_REG_ROM },
456 };
457 #define NMMO (sizeof mmo / sizeof *mmo)
458
459 if (off & PGOFSET)
460 panic("tcxmmap");
461
462 /*
463 * Entries with size 0 map video RAM (i.e., the size in fb data).
464 *
465 * Since we work in pages, the fact that the map offset table's
466 * sizes are sometimes bizarre (e.g., 1) is effectively ignored:
467 * one byte is as good as one page.
468 */
469 for (mo = mmo; mo < &mmo[NMMO]; mo++) {
470 if ((u_int)off < mo->mo_uaddr)
471 continue;
472 u = off - mo->mo_uaddr;
473 sz = mo->mo_size ? mo->mo_size : sc->sc_fb.fb_type.fb_size;
474 if (u < sz) {
475 bus_type_t t = (bus_type_t)rr[mo->mo_bank].sbr_slot;
476 bus_addr_t a = (bus_addr_t)rr[mo->mo_bank].sbr_offset;
477
478 if (bus_space_mmap(sc->sc_bustag,
479 t,
480 a + u,
481 BUS_SPACE_MAP_LINEAR, &bh))
482 return (-1);
483
484 return ((paddr_t)bh);
485 }
486 }
487 return (-1); /* not a user-map offset */
488 }
489