p9100.c revision 1.2.6.1 1 /* $NetBSD: p9100.c,v 1.2.6.1 2001/10/01 12:46:19 fvdl Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas.
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 (p9100) 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 #include <machine/bus.h>
58 #include <machine/autoconf.h>
59
60 #include <dev/sun/fbio.h>
61 #include <dev/sun/fbvar.h>
62 #include <dev/sun/btreg.h>
63 #include <dev/sun/btvar.h>
64 #if 0
65 #include <dev/sbus/p9100reg.h>
66 #endif
67
68 #include <dev/sbus/sbusvar.h>
69
70 #include "tctrl.h"
71 #if NTCTRL > 0
72 #include <machine/tctrl.h>
73 #include <sparc/dev/tctrlvar.h>/*XXX*/
74 #endif
75
76 #include <machine/conf.h>
77
78 /* per-display variables */
79 struct p9100_softc {
80 struct device sc_dev; /* base device */
81 struct sbusdev sc_sd; /* sbus device */
82 struct fbdevice sc_fb; /* frame buffer device */
83 bus_space_tag_t sc_bustag;
84 bus_type_t sc_ctl_btype; /* phys address description */
85 bus_addr_t sc_ctl_paddr; /* for device mmap() */
86 bus_size_t sc_ctl_psize; /* for device mmap() */
87 bus_space_handle_t sc_ctl_memh; /* bus space handle */
88 bus_type_t sc_cmd_btype; /* phys address description */
89 bus_addr_t sc_cmd_paddr; /* for device mmap() */
90 bus_size_t sc_cmd_psize; /* for device mmap() */
91 bus_space_handle_t sc_cmd_memh; /* bus space handle */
92 bus_type_t sc_fb_btype; /* phys address description */
93 bus_addr_t sc_fb_paddr; /* for device mmap() */
94 bus_size_t sc_fb_psize; /* for device mmap() */
95 bus_space_handle_t sc_fb_memh; /* bus space handle */
96 uint32_t sc_junk;
97
98 union bt_cmap sc_cmap; /* Brooktree color map */
99 };
100
101 /* The Tadpole 3GX Technical Reference Manual lies. The ramdac registers
102 * are map in 4 byte increments, not 8.
103 */
104 #define SCRN_RPNT_CTL_1 0x0138 /* Screen Respaint Timing Control 1 */
105 #define VIDEO_ENABLED 0x00000020
106 #define PWRUP_CNFG 0x0194 /* Power Up Configuration */
107 #define DAC_CMAP_WRIDX 0x0200 /* IBM RGB528 Palette Address (Write) */
108 #define DAC_CMAP_DATA 0x0204 /* IBM RGB528 Palette Data */
109 #define DAC_PXL_MASK 0x0208 /* IBM RGB528 Pixel Mask */
110 #define DAC_CMAP_RDIDX 0x020c /* IBM RGB528 Palette Address (Read) */
111 #define DAC_INDX_LO 0x0210 /* IBM RGB528 Index Low */
112 #define DAC_INDX_HI 0x0214 /* IBM RGB528 Index High */
113 #define DAC_INDX_DATA 0x0218 /* IBM RGB528 Index Data (Indexed Registers) */
114 #define DAC_INDX_CTL 0x021c /* IBM RGB528 Index Control */
115
116 /* autoconfiguration driver */
117 static int p9100_sbus_match(struct device *, struct cfdata *, void *);
118 static void p9100_sbus_attach(struct device *, struct device *, void *);
119
120 static void p9100unblank(struct device *);
121 static void p9100_shutdown(void *);
122
123 /* cdevsw prototypes */
124 cdev_decl(p9100);
125
126 struct cfattach pnozz_ca = {
127 sizeof(struct p9100_softc), p9100_sbus_match, p9100_sbus_attach
128 };
129
130 extern struct cfdriver pnozz_cd;
131
132 /* frame buffer generic driver */
133 static struct fbdriver p9100fbdriver = {
134 p9100unblank, p9100open, p9100close, p9100ioctl, p9100poll,
135 p9100mmap
136 };
137
138 static void p9100loadcmap(struct p9100_softc *, int, int);
139 static void p9100_set_video(struct p9100_softc *, int);
140 static int p9100_get_video(struct p9100_softc *);
141 static uint32_t p9100_ctl_read_4(struct p9100_softc *, bus_size_t);
142 static void p9100_ctl_write_4(struct p9100_softc *, bus_size_t, uint32_t);
143 #if 0
144 static uint8_t p9100_ramdac_read(struct p9100_softc *, bus_size_t);
145 #endif
146 static void p9100_ramdac_write(struct p9100_softc *, bus_size_t, uint8_t);
147
148 /*
149 * Match a p9100.
150 */
151 static int
152 p9100_sbus_match(struct device *parent, struct cfdata *cf, void *aux)
153 {
154 struct sbus_attach_args *sa = aux;
155
156 return (strcmp("p9100", sa->sa_name) == 0);
157 }
158
159
160 /*
161 * Attach a display. We need to notice if it is the console, too.
162 */
163 static void
164 p9100_sbus_attach(struct device *parent, struct device *self, void *args)
165 {
166 struct p9100_softc *sc = (struct p9100_softc *)self;
167 struct sbus_attach_args *sa = args;
168 struct fbdevice *fb = &sc->sc_fb;
169 int isconsole;
170 int node;
171 int i;
172
173 /* Remember cookies for p9100_mmap() */
174 sc->sc_bustag = sa->sa_bustag;
175 sc->sc_ctl_btype = (bus_type_t)sa->sa_reg[0].sbr_slot;
176 sc->sc_ctl_paddr = sbus_bus_addr(sa->sa_bustag,
177 sa->sa_reg[0].sbr_slot, sa->sa_reg[0].sbr_offset);
178 sc->sc_ctl_psize = (bus_size_t)sa->sa_reg[0].sbr_size;
179
180 sc->sc_cmd_btype = (bus_type_t)sa->sa_reg[1].sbr_slot;
181 sc->sc_cmd_paddr = sbus_bus_addr(sa->sa_bustag,
182 sa->sa_reg[1].sbr_slot, sa->sa_reg[1].sbr_offset);
183 sc->sc_cmd_psize = (bus_size_t)sa->sa_reg[1].sbr_size;
184
185 sc->sc_fb_btype = (bus_type_t)sa->sa_reg[2].sbr_slot;
186 sc->sc_fb_paddr = sbus_bus_addr(sa->sa_bustag,
187 sa->sa_reg[2].sbr_slot, sa->sa_reg[2].sbr_offset);
188 sc->sc_fb_psize = (bus_size_t)sa->sa_reg[2].sbr_size;
189
190 fb->fb_driver = &p9100fbdriver;
191 fb->fb_device = &sc->sc_dev;
192 fb->fb_flags = sc->sc_dev.dv_cfdata->cf_flags & FB_USERMASK;
193 fb->fb_type.fb_type = FBTYPE_SUN3COLOR;
194
195 node = sa->sa_node;
196
197 /*
198 * When the ROM has mapped in a p9100 display, the address
199 * maps only the video RAM, so in any case we have to map the
200 * registers ourselves. We only need the video RAM if we are
201 * going to print characters via rconsole.
202 */
203 if (sbus_bus_map(sc->sc_bustag, sc->sc_ctl_btype,
204 sc->sc_ctl_paddr, sc->sc_ctl_psize,
205 BUS_SPACE_MAP_LINEAR, 0,
206 &sc->sc_ctl_memh) != 0) {
207 printf("%s: cannot map control registers\n", self->dv_xname);
208 return;
209 }
210
211 if (sbus_bus_map(sc->sc_bustag, sc->sc_cmd_btype,
212 sc->sc_cmd_paddr, sc->sc_cmd_psize,
213 BUS_SPACE_MAP_LINEAR, 0,
214 &sc->sc_cmd_memh) != 0) {
215 printf("%s: cannot map command registers\n", self->dv_xname);
216 return;
217 }
218
219 isconsole = fb_is_console(node);
220
221 if (sa->sa_npromvaddrs != 0)
222 fb->fb_pixels = (caddr_t)sa->sa_promvaddrs[0];
223 if (isconsole && fb->fb_pixels == NULL) {
224 if (sbus_bus_map(sc->sc_bustag, sc->sc_fb_btype,
225 sc->sc_fb_paddr, sc->sc_fb_psize,
226 BUS_SPACE_MAP_LINEAR, 0,
227 &sc->sc_fb_memh) != 0) {
228 printf("%s: cannot map framebuffer\n", self->dv_xname);
229 return;
230 }
231 fb->fb_pixels = (char *)sc->sc_fb_memh;
232 } else {
233 sc->sc_fb_memh = (bus_space_handle_t) fb->fb_pixels;
234 }
235
236 i = p9100_ctl_read_4(sc, 0x0004);
237 switch ((i >> 26) & 7) {
238 case 5: fb->fb_type.fb_depth = 32; break;
239 case 7: fb->fb_type.fb_depth = 24; break;
240 case 3: fb->fb_type.fb_depth = 16; break;
241 case 2: fb->fb_type.fb_depth = 8; break;
242 default: {
243 panic("pnozz: can't determine screen depth (0x%02x)", i);
244 }
245 }
246 fb_setsize_obp(fb, fb->fb_type.fb_depth, 800, 600, node);
247
248 sbus_establish(&sc->sc_sd, &sc->sc_dev);
249
250 fb->fb_type.fb_size = fb->fb_type.fb_height * fb->fb_linebytes;
251 printf(": rev %d, %dx%d, depth %d",
252 (i & 7), fb->fb_type.fb_width, fb->fb_type.fb_height,
253 fb->fb_type.fb_depth);
254
255 fb->fb_type.fb_cmsize = PROM_getpropint(node, "cmsize", 256);
256 if ((1 << fb->fb_type.fb_depth) != fb->fb_type.fb_cmsize)
257 printf(", %d entry colormap", fb->fb_type.fb_cmsize);
258
259 /* Initialize the default color map. */
260 bt_initcmap(&sc->sc_cmap, 256);
261 p9100loadcmap(sc, 0, 256);
262
263 /* make sure we are not blanked */
264 p9100_set_video(sc, 1);
265
266 if (shutdownhook_establish(p9100_shutdown, sc) == NULL) {
267 panic("%s: could not establish shutdown hook",
268 sc->sc_dev.dv_xname);
269 }
270
271 if (isconsole) {
272 printf(" (console)\n");
273 #ifdef RASTERCONSOLE
274 for (i = 0; i < fb->fb_type.fb_size; i++) {
275 if (fb->fb_pixels[i] == 0) {
276 fb->fb_pixels[i] = 1;
277 } else if (fb->fb_pixels[i] == (char) 255) {
278 fb->fb_pixels[i] = 0;
279 }
280 }
281 p9100loadcmap(sc, 255, 1);
282 fbrcons_init(fb);
283 #endif
284 } else
285 printf("\n");
286
287 fb_attach(fb, isconsole);
288 }
289
290 static void
291 p9100_shutdown(arg)
292 void *arg;
293 {
294 struct p9100_softc *sc = arg;
295 #ifdef RASTERCONSOLE
296 struct fbdevice *fb = &sc->sc_fb;
297 int i;
298
299 for (i = 0; i < fb->fb_type.fb_size; i++) {
300 if (fb->fb_pixels[i] == 1) {
301 fb->fb_pixels[i] = 0;
302 } else if (fb->fb_pixels[i] == 0) {
303 fb->fb_pixels[i] = 255;
304 }
305 }
306 sc->sc_cmap.cm_map[0][0] = 0xff;
307 sc->sc_cmap.cm_map[0][1] = 0xff;
308 sc->sc_cmap.cm_map[0][2] = 0xff;
309 sc->sc_cmap.cm_map[1][0] = 0;
310 sc->sc_cmap.cm_map[1][1] = 0;
311 sc->sc_cmap.cm_map[1][2] = 0x80;
312 p9100loadcmap(sc, 0, 2);
313 sc->sc_cmap.cm_map[255][0] = 0;
314 sc->sc_cmap.cm_map[255][1] = 0;
315 sc->sc_cmap.cm_map[255][2] = 0;
316 p9100loadcmap(sc, 255, 1);
317 #endif
318 p9100_set_video(sc, 1);
319 }
320
321 int
322 p9100open(dev_t dev, int flags, int mode, struct proc *p)
323 {
324 int unit = minor(dev);
325
326 if (unit >= pnozz_cd.cd_ndevs || pnozz_cd.cd_devs[unit] == NULL)
327 return (ENXIO);
328 return (0);
329 }
330
331 int
332 p9100close(dev_t dev, int flags, int mode, struct proc *p)
333 {
334 return (0);
335 }
336
337 int
338 p9100ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
339 {
340 struct p9100_softc *sc = pnozz_cd.cd_devs[minor(dev)];
341 struct fbgattr *fba;
342 int error;
343
344 switch (cmd) {
345
346 case FBIOGTYPE:
347 *(struct fbtype *)data = sc->sc_fb.fb_type;
348 break;
349
350 case FBIOGATTR:
351 fba = (struct fbgattr *)data;
352 fba->real_type = sc->sc_fb.fb_type.fb_type;
353 fba->owner = 0; /* XXX ??? */
354 fba->fbtype = sc->sc_fb.fb_type;
355 fba->sattr.flags = 0;
356 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
357 fba->sattr.dev_specific[0] = -1;
358 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
359 fba->emu_types[1] = -1;
360 break;
361
362 case FBIOGETCMAP:
363 #define p ((struct fbcmap *)data)
364 return (bt_getcmap(p, &sc->sc_cmap, 256, 1));
365
366 case FBIOPUTCMAP:
367 /* copy to software map */
368 error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
369 if (error)
370 return (error);
371 /* now blast them into the chip */
372 /* XXX should use retrace interrupt */
373 p9100loadcmap(sc, p->index, p->count);
374 #undef p
375 break;
376
377 case FBIOGVIDEO:
378 *(int *)data = p9100_get_video(sc);
379 break;
380
381 case FBIOSVIDEO:
382 p9100_set_video(sc, *(int *)data);
383 break;
384
385 default:
386 return (ENOTTY);
387 }
388 return (0);
389 }
390
391 int
392 p9100poll(dev_t dev, int events, struct proc *p)
393 {
394 return seltrue(dev, events, p);
395 }
396
397 static uint32_t
398 p9100_ctl_read_4(struct p9100_softc *sc, bus_size_t off)
399 {
400 sc->sc_junk = bus_space_read_4(sc->sc_bustag, sc->sc_fb_memh, off);
401 return bus_space_read_4(sc->sc_bustag, sc->sc_ctl_memh, off);
402 }
403
404 static void
405 p9100_ctl_write_4(struct p9100_softc *sc, bus_size_t off, uint32_t v)
406 {
407 sc->sc_junk = bus_space_read_4(sc->sc_bustag, sc->sc_fb_memh, off);
408 bus_space_write_4(sc->sc_bustag, sc->sc_ctl_memh, off, v);
409 }
410
411 #if 0
412 static uint8_t
413 p9100_ramdac_read(struct p9100_softc *sc, bus_size_t off)
414 {
415 sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
416 return p9100_ctl_read_4(sc, off) >> 16;
417 }
418 #endif
419
420 static void
421 p9100_ramdac_write(struct p9100_softc *sc, bus_size_t off, uint8_t v)
422 {
423 sc->sc_junk = p9100_ctl_read_4(sc, PWRUP_CNFG);
424 p9100_ctl_write_4(sc, off, v << 16);
425 }
426
427 /*
428 * Undo the effect of an FBIOSVIDEO that turns the video off.
429 */
430 static void
431 p9100unblank(struct device *dev)
432 {
433
434 p9100_set_video((struct p9100_softc *)dev, 1);
435 }
436
437 static void
438 p9100_set_video(struct p9100_softc *sc, int enable)
439 {
440 u_int32_t v = p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1);
441 if (enable)
442 v |= VIDEO_ENABLED;
443 else
444 v &= ~VIDEO_ENABLED;
445 p9100_ctl_write_4(sc, SCRN_RPNT_CTL_1, v);
446 #if NTCTRL > 0
447 /* Turn On/Off the TFT if we know how.
448 */
449 tadpole_set_video(enable);
450 #endif
451 }
452
453 static int
454 p9100_get_video(struct p9100_softc *sc)
455 {
456 return (p9100_ctl_read_4(sc, SCRN_RPNT_CTL_1) & VIDEO_ENABLED) != 0;
457 }
458
459 /*
460 * Load a subset of the current (new) colormap into the IBM RAMDAC.
461 */
462 static void
463 p9100loadcmap(struct p9100_softc *sc, int start, int ncolors)
464 {
465 u_char *p;
466
467 p9100_ramdac_write(sc, DAC_CMAP_WRIDX, start);
468
469 for (p = sc->sc_cmap.cm_map[start], ncolors *= 3; ncolors-- > 0; p++) {
470 p9100_ramdac_write(sc, DAC_CMAP_DATA, *p);
471 }
472 }
473
474 /*
475 * Return the address that would map the given device at the given
476 * offset, allowing for the given protection, or return -1 for error.
477 */
478 paddr_t
479 p9100mmap(dev_t dev, off_t off, int prot)
480 {
481 struct p9100_softc *sc = pnozz_cd.cd_devs[minor(dev)];
482
483 if (off & PGOFSET)
484 panic("p9100mmap");
485 if (off < 0)
486 return (-1);
487
488 #define CG3_MMAP_OFFSET 0x04000000
489 /* Make Xsun think we are a CG3 (SUN3COLOR)
490 */
491 if (off >= CG3_MMAP_OFFSET && off < CG3_MMAP_OFFSET + sc->sc_fb_psize) {
492 off -= CG3_MMAP_OFFSET;
493 return (bus_space_mmap(sc->sc_bustag,
494 sc->sc_fb_paddr,
495 off,
496 prot,
497 BUS_SPACE_MAP_LINEAR));
498 }
499
500 if (off >= sc->sc_fb_psize + sc->sc_ctl_psize + sc->sc_cmd_psize)
501 return (-1);
502
503 if (off < sc->sc_fb_psize) {
504 return (bus_space_mmap(sc->sc_bustag,
505 sc->sc_fb_paddr,
506 off,
507 prot,
508 BUS_SPACE_MAP_LINEAR));
509 }
510 off -= sc->sc_fb_psize;
511 if (off < sc->sc_ctl_psize) {
512 return (bus_space_mmap(sc->sc_bustag,
513 sc->sc_ctl_paddr,
514 off,
515 prot,
516 BUS_SPACE_MAP_LINEAR));
517 }
518 off -= sc->sc_ctl_psize;
519
520 return (bus_space_mmap(sc->sc_bustag,
521 sc->sc_cmd_paddr,
522 off,
523 prot,
524 BUS_SPACE_MAP_LINEAR));
525 }
526