macfb.c revision 1.16 1 /* $NetBSD: macfb.c,v 1.16 2007/08/29 12:39:31 jmmv Exp $ */
2 /*
3 * Copyright (c) 1998 Matt DeBergalis
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Matt DeBergalis
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: macfb.c,v 1.16 2007/08/29 12:39:31 jmmv Exp $");
34
35 #include "opt_wsdisplay_compat.h"
36 #include "grf.h"
37
38 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45
46 #include <machine/cpu.h>
47 #include <machine/bus.h>
48
49 #include <machine/grfioctl.h>
50 #include <mac68k/nubus/nubus.h>
51 #include <mac68k/dev/grfvar.h>
52 #include <mac68k/dev/macfbvar.h>
53 #include <dev/wscons/wsconsio.h>
54
55 #include <dev/rcons/raster.h>
56 #include <dev/wscons/wscons_raster.h>
57 #include <dev/wscons/wsdisplayvar.h>
58
59 int macfb_match(struct device *, struct cfdata *, void *);
60 void macfb_attach(struct device *, struct device *, void *);
61
62 CFATTACH_DECL(macfb, sizeof(struct macfb_softc),
63 macfb_match, macfb_attach, NULL, NULL);
64
65 const struct wsdisplay_emulops macfb_emulops = {
66 rcons_cursor,
67 rcons_mapchar,
68 rcons_putchar,
69 rcons_copycols,
70 rcons_erasecols,
71 rcons_copyrows,
72 rcons_eraserows,
73 rcons_allocattr
74 };
75
76 struct wsscreen_descr macfb_stdscreen = {
77 "std",
78 0, 0, /* will be filled in -- XXX shouldn't, it's global */
79 &macfb_emulops,
80 0, 0,
81 WSSCREEN_REVERSE
82 };
83
84 const struct wsscreen_descr *_macfb_scrlist[] = {
85 &macfb_stdscreen,
86 };
87
88 const struct wsscreen_list macfb_screenlist = {
89 sizeof(_macfb_scrlist) / sizeof(struct wsscreen_descr *),
90 _macfb_scrlist
91 };
92
93 static int macfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
94 static paddr_t macfb_mmap(void *, void *, off_t, int);
95 static int macfb_alloc_screen(void *, const struct wsscreen_descr *,
96 void **, int *, int *, long *);
97 static void macfb_free_screen(void *, void *);
98 static int macfb_show_screen(void *, void *, int,
99 void (*)(void *, int, int), void *);
100
101 const struct wsdisplay_accessops macfb_accessops = {
102 macfb_ioctl,
103 macfb_mmap,
104 macfb_alloc_screen,
105 macfb_free_screen,
106 macfb_show_screen,
107 0 /* load_font */
108 };
109
110 void macfb_init(struct macfb_devconfig *);
111
112 paddr_t macfb_consaddr;
113 static int macfb_is_console(paddr_t);
114 #ifdef WSDISPLAY_COMPAT_ITEFONT
115 static void init_itefont(void);
116 #endif /* WSDISPLAY_COMPAT_ITEFONT */
117
118 static struct macfb_devconfig macfb_console_dc;
119
120 /* From Booter via locore */
121 extern long videoaddr;
122 extern long videorowbytes;
123 extern long videobitdepth;
124 extern long videowidth;
125 extern long videoheight;
126 extern u_int32_t mac68k_vidlog;
127 extern u_int32_t mac68k_vidphys;
128 extern u_int32_t mac68k_vidlen;
129
130 static int
131 macfb_is_console(paddr_t addr)
132 {
133 if (addr != macfb_consaddr &&
134 (addr >= 0xf9000000 && addr <= 0xfeffffff)) {
135 /*
136 * This is in the NuBus standard slot space range, so we
137 * may well have to look at 0xFssxxxxx, too. Mask off the
138 * slot number and duplicate it in bits 20-23, per IM:V
139 * pp 459, 463, and IM:VI ch 30 p 17.
140 * Note: this is an ugly hack and I wish I knew what
141 * to do about it. -- sr
142 */
143 addr = (paddr_t)(((u_long)addr & 0xff0fffff) |
144 (((u_long)addr & 0x0f000000) >> 4));
145 }
146 return ((mac68k_machine.serial_console & 0x03) == 0
147 && (addr == macfb_consaddr));
148 }
149
150 void
151 macfb_clear(struct macfb_devconfig *dc)
152 {
153 int i, rows;
154
155 /* clear the display */
156 rows = dc->dc_ht;
157 for (i = 0; rows-- > 0; i += dc->dc_rowbytes)
158 memset((u_char *)dc->dc_vaddr + dc->dc_offset + i,
159 0, dc->dc_rowbytes);
160 }
161
162 void
163 macfb_init(struct macfb_devconfig *dc)
164 {
165 struct raster *rap;
166 struct rcons *rcp;
167
168 macfb_clear(dc);
169
170 #ifdef WSDISPLAY_COMPAT_ITEFONT
171 init_itefont();
172 #endif /* WSDISPLAY_COMPAT_ITEFONT */
173
174 rap = &dc->dc_raster;
175 rap->width = dc->dc_wid;
176 rap->height = dc->dc_ht;
177 rap->depth = dc->dc_depth;
178 rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
179 rap->pixels = (u_int32_t *)(dc->dc_vaddr + dc->dc_offset);
180
181 /* initialize the raster console blitter */
182 rcp = &dc->dc_rcons;
183 rcp->rc_sp = rap;
184 rcp->rc_crow = rcp->rc_ccol = -1;
185 rcp->rc_crowp = &rcp->rc_crow;
186 rcp->rc_ccolp = &rcp->rc_ccol;
187 rcons_init(rcp, 128, 192);
188
189 macfb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
190 macfb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
191 }
192
193 int
194 macfb_match(struct device *parent, struct cfdata *match, void *aux)
195 {
196 return (1);
197 }
198
199 void
200 macfb_attach(struct device *parent, struct device *self, void *aux)
201 {
202 struct grfbus_attach_args *ga = aux;
203 struct grfmode *gm = ga->ga_grfmode;
204 struct macfb_softc *sc;
205 struct wsemuldisplaydev_attach_args waa;
206 int isconsole;
207
208 sc = (struct macfb_softc *)self;
209
210 printf("\n");
211
212 isconsole = macfb_is_console(ga->ga_phys + ga->ga_grfmode->fboff);
213
214 if (isconsole) {
215 sc->sc_dc = &macfb_console_dc;
216 sc->nscreens = 1;
217 } else {
218 sc->sc_dc = malloc(sizeof(struct macfb_devconfig), M_DEVBUF, M_WAITOK);
219 sc->sc_dc->dc_vaddr = (vaddr_t)gm->fbbase;
220 sc->sc_dc->dc_paddr = ga->ga_phys;
221 sc->sc_dc->dc_size = gm->fbsize;
222
223 sc->sc_dc->dc_wid = gm->width;
224 sc->sc_dc->dc_ht = gm->height;
225 sc->sc_dc->dc_depth = gm->psize;
226 sc->sc_dc->dc_rowbytes = gm->rowbytes;
227
228 sc->sc_dc->dc_offset = gm->fboff;
229
230 macfb_clear(sc->sc_dc);
231
232 sc->nscreens = 1;
233 }
234
235 /* initialize the raster */
236 waa.console = isconsole;
237 waa.scrdata = &macfb_screenlist;
238 waa.accessops = &macfb_accessops;
239 waa.accesscookie = sc;
240
241 config_found(self, &waa, wsemuldisplaydevprint);
242
243 #if NGRF > 0
244 grf_attach(sc, device_unit(self));
245 #endif
246 }
247
248
249 int
250 macfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
251 struct lwp *l)
252 {
253 struct macfb_softc *sc = v;
254 struct macfb_devconfig *dc = sc->sc_dc;
255 struct wsdisplay_fbinfo *wdf;
256
257 switch (cmd) {
258 case WSDISPLAYIO_GTYPE:
259 *(int *)data = dc->dc_type;
260 return 0;
261
262 case WSDISPLAYIO_GINFO:
263 wdf = (struct wsdisplay_fbinfo *)data;
264 wdf->height = dc->dc_raster.height;
265 wdf->width = dc->dc_raster.width;
266 wdf->depth = dc->dc_raster.depth;
267 wdf->cmsize = 256;
268 return 0;
269
270 case WSDISPLAYIO_GCURMAX:
271 case WSDISPLAYIO_GCURPOS:
272 case WSDISPLAYIO_GCURSOR:
273 case WSDISPLAYIO_GETCMAP:
274 case WSDISPLAYIO_GVIDEO:
275 case WSDISPLAYIO_PUTCMAP:
276 case WSDISPLAYIO_SCURPOS:
277 case WSDISPLAYIO_SCURSOR:
278 case WSDISPLAYIO_SVIDEO:
279 /* NONE of these operations are supported. */
280 return EPASSTHROUGH;
281 }
282
283 return EPASSTHROUGH;
284 }
285
286 static paddr_t
287 macfb_mmap(void *v, void *vs, off_t offset, int prot)
288 {
289 struct macfb_softc *sc = v;
290 struct macfb_devconfig *dc = sc->sc_dc;
291 paddr_t addr;
292
293 if (offset >= 0 &&
294 offset < m68k_round_page(dc->dc_rowbytes * dc->dc_ht))
295 addr = m68k_btop(dc->dc_paddr + dc->dc_offset + offset);
296 else
297 addr = (-1); /* XXX bogus */
298
299 return addr;
300 }
301
302 int
303 macfb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
304 int *curxp, int *curyp, long *defattrp)
305 {
306 struct macfb_softc *sc = v;
307 long defattr;
308
309 if (sc->nscreens > 0)
310 return (ENOMEM);
311
312 *cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
313 *curxp = 0;
314 *curyp = 0;
315 rcons_allocattr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
316 *defattrp = defattr;
317 sc->nscreens++;
318 return (0);
319 }
320
321 void
322 macfb_free_screen(void *v, void *cookie)
323 {
324 struct macfb_softc *sc = v;
325
326 if (sc->sc_dc == &macfb_console_dc)
327 panic("cfb_free_screen: console");
328
329 sc->nscreens--;
330 }
331
332 int
333 macfb_show_screen(void *v, void *cookie, int waitok,
334 void (*cb)(void *, int, int), void *cbarg)
335 {
336 return 0;
337 }
338
339 int
340 macfb_cnattach(paddr_t addr)
341 {
342 struct macfb_devconfig *dc = &macfb_console_dc;
343 long defattr;
344
345 dc->dc_vaddr = m68k_trunc_page(videoaddr);
346 dc->dc_paddr = m68k_trunc_page(mac68k_vidphys);
347
348 dc->dc_wid = videowidth;
349 dc->dc_ht = videoheight;
350 dc->dc_depth = videobitdepth;
351 dc->dc_rowbytes = videorowbytes;
352
353 dc->dc_size = (mac68k_vidlen > 0) ?
354 mac68k_vidlen : dc->dc_ht * dc->dc_rowbytes;
355 dc->dc_offset = m68k_page_offset(mac68k_vidphys);
356
357 /* set up the display */
358 macfb_init(&macfb_console_dc);
359
360 rcons_allocattr(&dc->dc_rcons, 0, 0, 0, &defattr);
361
362 wsdisplay_cnattach(&macfb_stdscreen, &dc->dc_rcons,
363 0, 0, defattr);
364
365 macfb_consaddr = addr;
366 dc->isconsole = 1;
367 return (0);
368 }
369
370 #ifdef WSDISPLAY_COMPAT_ITEFONT
371 #include <mac68k/dev/6x10.h>
372
373 void
374 init_itefont(void)
375 {
376 static int itefont_initted;
377 int i, j;
378
379 extern struct raster_font gallant19; /* XXX */
380
381 if (itefont_initted)
382 return;
383 itefont_initted = 1;
384
385 /* XXX but we cannot use malloc here... */
386 gallant19.width = 6;
387 gallant19.height = 10;
388 gallant19.ascent = 0;
389
390 for (i = 32; i < 128; i++) {
391 u_int *p;
392
393 if (gallant19.chars[i].r == NULL)
394 continue;
395
396 gallant19.chars[i].r->width = 6;
397 gallant19.chars[i].r->height = 10;
398 p = gallant19.chars[i].r->pixels;
399
400 for (j = 0; j < 10; j++)
401 *p++ = Font6x10[i * 10 + j] << 26;
402 }
403 }
404 #endif /* WSDISPLAY_COMPAT_ITEFONT */
405