ofb.c revision 1.49 1 1.49 elad /* $NetBSD: ofb.c,v 1.49 2006/05/14 21:55:38 elad Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*
4 1.1 tsubai * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 1.1 tsubai * All rights reserved.
6 1.1 tsubai *
7 1.1 tsubai * Author: Chris G. Demetriou
8 1.1 tsubai *
9 1.1 tsubai * Permission to use, copy, modify and distribute this software and
10 1.1 tsubai * its documentation is hereby granted, provided that both the copyright
11 1.1 tsubai * notice and this permission notice appear in all copies of the
12 1.1 tsubai * software, derivative works or modified versions, and any portions
13 1.1 tsubai * thereof, and that both notices appear in supporting documentation.
14 1.1 tsubai *
15 1.1 tsubai * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.1 tsubai * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.1 tsubai * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.1 tsubai *
19 1.1 tsubai * Carnegie Mellon requests users of this software to return to
20 1.1 tsubai *
21 1.1 tsubai * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.1 tsubai * School of Computer Science
23 1.1 tsubai * Carnegie Mellon University
24 1.1 tsubai * Pittsburgh PA 15213-3890
25 1.1 tsubai *
26 1.1 tsubai * any improvements or extensions that they make and grant Carnegie the
27 1.1 tsubai * rights to redistribute these changes.
28 1.1 tsubai */
29 1.36 lukem
30 1.36 lukem #include <sys/cdefs.h>
31 1.49 elad __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.49 2006/05/14 21:55:38 elad Exp $");
32 1.1 tsubai
33 1.1 tsubai #include <sys/param.h>
34 1.1 tsubai #include <sys/buf.h>
35 1.1 tsubai #include <sys/conf.h>
36 1.1 tsubai #include <sys/device.h>
37 1.1 tsubai #include <sys/ioctl.h>
38 1.1 tsubai #include <sys/kernel.h>
39 1.1 tsubai #include <sys/malloc.h>
40 1.1 tsubai #include <sys/systm.h>
41 1.49 elad #include <sys/kauth.h>
42 1.1 tsubai
43 1.15 mrg #include <uvm/uvm_extern.h>
44 1.11 tsubai
45 1.3 tsubai #include <dev/pci/pcidevs.h>
46 1.1 tsubai #include <dev/pci/pcireg.h>
47 1.1 tsubai #include <dev/pci/pcivar.h>
48 1.27 nathanw #include <dev/pci/pciio.h>
49 1.1 tsubai
50 1.1 tsubai #include <dev/wscons/wsconsio.h>
51 1.1 tsubai #include <dev/wscons/wsdisplayvar.h>
52 1.11 tsubai #include <dev/rasops/rasops.h>
53 1.45 macallan #include <dev/wsfont/wsfont.h>
54 1.1 tsubai
55 1.20 matt #include <dev/ofw/openfirm.h>
56 1.13 tsubai #include <dev/ofw/ofw_pci.h>
57 1.13 tsubai
58 1.1 tsubai #include <machine/bus.h>
59 1.20 matt #include <machine/autoconf.h>
60 1.1 tsubai #include <machine/grfioctl.h>
61 1.37 matt
62 1.37 matt #include <powerpc/oea/bat.h>
63 1.1 tsubai
64 1.45 macallan #include <dev/wscons/wsdisplay_vconsvar.h>
65 1.1 tsubai #include <macppc/dev/ofbvar.h>
66 1.1 tsubai
67 1.18 tsubai #if OFB_ENABLE_CACHE
68 1.18 tsubai int ofb_enable_cache = 1;
69 1.18 tsubai #else
70 1.18 tsubai int ofb_enable_cache = 0;
71 1.18 tsubai #endif
72 1.18 tsubai
73 1.44 macallan static int ofbmatch(struct device *, struct cfdata *, void *);
74 1.44 macallan static void ofbattach(struct device *, struct device *, void *);
75 1.1 tsubai
76 1.32 thorpej CFATTACH_DECL(ofb, sizeof(struct ofb_softc),
77 1.32 thorpej ofbmatch, ofbattach, NULL, NULL);
78 1.1 tsubai
79 1.1 tsubai struct wsscreen_descr ofb_stdscreen = {
80 1.1 tsubai "std",
81 1.1 tsubai 0, 0, /* will be filled in -- XXX shouldn't, it's global */
82 1.11 tsubai 0,
83 1.1 tsubai 0, 0,
84 1.1 tsubai WSSCREEN_REVERSE
85 1.1 tsubai };
86 1.1 tsubai
87 1.1 tsubai const struct wsscreen_descr *_ofb_scrlist[] = {
88 1.1 tsubai &ofb_stdscreen,
89 1.1 tsubai /* XXX other formats, graphics screen? */
90 1.1 tsubai };
91 1.1 tsubai
92 1.1 tsubai struct wsscreen_list ofb_screenlist = {
93 1.1 tsubai sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
94 1.1 tsubai };
95 1.1 tsubai
96 1.48 jmmv static int ofb_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
97 1.48 jmmv static paddr_t ofb_mmap(void *, void *, off_t, int);
98 1.45 macallan static int copy_rom_font(void);
99 1.45 macallan
100 1.45 macallan static int ofb_init_rasops(int, struct rasops_info *);
101 1.45 macallan static void ofb_init_screen(void *, struct vcons_screen *, int, long *);
102 1.1 tsubai
103 1.1 tsubai struct wsdisplay_accessops ofb_accessops = {
104 1.1 tsubai ofb_ioctl,
105 1.1 tsubai ofb_mmap,
106 1.45 macallan NULL, /* vcons_alloc_screen */
107 1.45 macallan NULL, /* vcons_free_screen */
108 1.45 macallan NULL, /* vcons_show_screen */
109 1.45 macallan NULL /* load_font */
110 1.1 tsubai };
111 1.1 tsubai
112 1.45 macallan static struct vcons_screen ofb_console_screen;
113 1.11 tsubai static struct wsdisplay_font openfirm6x11;
114 1.45 macallan static int console_node, console_instance;
115 1.45 macallan static vaddr_t fbaddr;
116 1.45 macallan static int romfont_loaded = 0;
117 1.45 macallan
118 1.45 macallan static void ofb_putpalreg(struct ofb_softc *, int, uint8_t, uint8_t,
119 1.45 macallan uint8_t);
120 1.45 macallan
121 1.45 macallan static int ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *);
122 1.45 macallan static int ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *);
123 1.45 macallan static void ofb_init_cmap(struct ofb_softc *);
124 1.11 tsubai
125 1.45 macallan extern const u_char rasops_cmap[768];
126 1.1 tsubai
127 1.45 macallan static int
128 1.44 macallan ofbmatch(struct device *parent, struct cfdata *match, void *aux)
129 1.1 tsubai {
130 1.1 tsubai struct pci_attach_args *pa = aux;
131 1.1 tsubai
132 1.3 tsubai if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
133 1.11 tsubai PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
134 1.3 tsubai return 1;
135 1.1 tsubai
136 1.3 tsubai if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
137 1.3 tsubai return 1;
138 1.3 tsubai
139 1.3 tsubai return 0;
140 1.1 tsubai }
141 1.1 tsubai
142 1.44 macallan static void
143 1.44 macallan ofbattach(struct device *parent, struct device *self, void *aux)
144 1.1 tsubai {
145 1.22 tsubai struct ofb_softc *sc = (struct ofb_softc *)self;
146 1.1 tsubai struct pci_attach_args *pa = aux;
147 1.1 tsubai struct wsemuldisplaydev_attach_args a;
148 1.45 macallan struct rasops_info *ri = &ofb_console_screen.scr_ri;
149 1.45 macallan long defattr;
150 1.47 macallan int console, len, node, sub;
151 1.5 tsubai char devinfo[256];
152 1.1 tsubai
153 1.46 macallan node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
154 1.46 macallan console = (node == console_node);
155 1.47 macallan if (!console) {
156 1.47 macallan /* check if any of the childs matches */
157 1.47 macallan sub = OF_child(node);
158 1.47 macallan while ((sub != 0) && (sub != console_node)) {
159 1.47 macallan sub = OF_peer(sub);
160 1.47 macallan }
161 1.47 macallan if (sub == console_node) {
162 1.47 macallan console = TRUE;
163 1.47 macallan }
164 1.47 macallan }
165 1.46 macallan
166 1.42 macallan sc->sc_memt = pa->pa_memt;
167 1.42 macallan sc->sc_iot = pa->pa_iot;
168 1.45 macallan sc->sc_pc = pa->pa_pc;
169 1.45 macallan sc->sc_pcitag = pa->pa_tag;
170 1.45 macallan sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
171 1.42 macallan
172 1.45 macallan pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
173 1.45 macallan printf(": %s\n", devinfo);
174 1.46 macallan
175 1.46 macallan if (!console)
176 1.46 macallan return;
177 1.45 macallan
178 1.45 macallan vcons_init(&sc->vd, sc, &ofb_stdscreen, &ofb_accessops);
179 1.45 macallan sc->vd.init_screen = ofb_init_screen;
180 1.45 macallan
181 1.1 tsubai console = ofb_is_console();
182 1.1 tsubai
183 1.46 macallan sc->sc_node = node;
184 1.46 macallan
185 1.1 tsubai if (console) {
186 1.45 macallan sc->sc_ih = console_instance;
187 1.45 macallan vcons_init_screen(&sc->vd, &ofb_console_screen, 1, &defattr);
188 1.45 macallan ofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
189 1.45 macallan printf("%s: %d x %d, %dbpp\n", self->dv_xname,
190 1.45 macallan ri->ri_width, ri->ri_height, ri->ri_depth);
191 1.1 tsubai } else {
192 1.45 macallan char name[64];
193 1.45 macallan if (sc->sc_node == 0) {
194 1.3 tsubai printf(": ofdev not found\n");
195 1.3 tsubai return;
196 1.3 tsubai }
197 1.16 tsubai
198 1.16 tsubai /* XXX There are two child screens on PowerBook. */
199 1.24 wiz memset(devinfo, 0, sizeof(devinfo));
200 1.45 macallan OF_getprop(sc->sc_node, "device_type", devinfo, sizeof(devinfo));
201 1.16 tsubai len = strlen(devinfo);
202 1.16 tsubai if (strcmp(devinfo + len - 7, "-parent") == 0)
203 1.45 macallan sc->sc_node = OF_child(sc->sc_node);
204 1.16 tsubai
205 1.45 macallan memset(name, 0, 64);
206 1.45 macallan OF_package_to_path(sc->sc_node, name, sizeof(name));
207 1.45 macallan sc->sc_ih = OF_open(name);
208 1.45 macallan
209 1.45 macallan }
210 1.45 macallan
211 1.45 macallan sc->sc_fbaddr = 0;
212 1.45 macallan if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4)
213 1.45 macallan OF_interpret("frame-buffer-adr", 1, &sc->sc_fbaddr);
214 1.45 macallan if (sc->sc_fbaddr == 0) {
215 1.45 macallan printf("%s: Unable to find the framebuffer address.\n",
216 1.45 macallan sc->sc_dev.dv_xname);
217 1.45 macallan return;
218 1.1 tsubai }
219 1.45 macallan
220 1.16 tsubai /* XXX */
221 1.45 macallan if (OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
222 1.16 tsubai sizeof(sc->sc_addrs)) == -1) {
223 1.45 macallan sc->sc_node = OF_parent(sc->sc_node);
224 1.45 macallan OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
225 1.16 tsubai sizeof(sc->sc_addrs));
226 1.16 tsubai }
227 1.13 tsubai
228 1.45 macallan ofb_init_cmap(sc);
229 1.1 tsubai
230 1.1 tsubai a.console = console;
231 1.1 tsubai a.scrdata = &ofb_screenlist;
232 1.1 tsubai a.accessops = &ofb_accessops;
233 1.45 macallan a.accesscookie = &sc->vd;
234 1.1 tsubai
235 1.1 tsubai config_found(self, &a, wsemuldisplaydevprint);
236 1.1 tsubai }
237 1.1 tsubai
238 1.44 macallan static void
239 1.45 macallan ofb_init_screen(void *cookie, struct vcons_screen *scr,
240 1.45 macallan int existing, long *defattr)
241 1.1 tsubai {
242 1.45 macallan struct ofb_softc *sc = cookie;
243 1.45 macallan struct rasops_info *ri = &scr->scr_ri;
244 1.45 macallan
245 1.45 macallan if (scr != &ofb_console_screen) {
246 1.45 macallan ofb_init_rasops(sc->sc_node, ri);
247 1.45 macallan }
248 1.45 macallan }
249 1.1 tsubai
250 1.45 macallan static int
251 1.45 macallan ofb_init_rasops(int node, struct rasops_info *ri)
252 1.45 macallan {
253 1.45 macallan int32_t width, height, linebytes, depth;
254 1.3 tsubai
255 1.8 tsubai /* XXX /chaos/control doesn't have "width", "height", ... */
256 1.3 tsubai width = height = -1;
257 1.11 tsubai if (OF_getprop(node, "width", &width, 4) != 4)
258 1.3 tsubai OF_interpret("screen-width", 1, &width);
259 1.11 tsubai if (OF_getprop(node, "height", &height, 4) != 4)
260 1.3 tsubai OF_interpret("screen-height", 1, &height);
261 1.11 tsubai if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
262 1.3 tsubai linebytes = width; /* XXX */
263 1.11 tsubai if (OF_getprop(node, "depth", &depth, 4) != 4)
264 1.3 tsubai depth = 8; /* XXX */
265 1.45 macallan if (OF_getprop(node, "address", &fbaddr, 4) != 4)
266 1.45 macallan OF_interpret("frame-buffer-adr", 1, &fbaddr);
267 1.11 tsubai
268 1.45 macallan if (width == -1 || height == -1 || fbaddr == 0 || fbaddr == -1)
269 1.45 macallan return FALSE;
270 1.18 tsubai
271 1.18 tsubai /* Enable write-through cache. */
272 1.29 matt if (ofb_enable_cache) {
273 1.29 matt vaddr_t va;
274 1.29 matt /*
275 1.29 matt * Let's try to find an empty BAT to use
276 1.29 matt */
277 1.29 matt for (va = SEGMENT_LENGTH; va < (USER_SR << ADDR_SR_SHFT);
278 1.29 matt va += SEGMENT_LENGTH) {
279 1.29 matt if (battable[va >> ADDR_SR_SHFT].batu == 0) {
280 1.29 matt battable[va >> ADDR_SR_SHFT].batl =
281 1.45 macallan BATL(fbaddr & 0xf0000000,
282 1.33 briggs BAT_G | BAT_W | BAT_M, BAT_PP_RW);
283 1.29 matt battable[va >> ADDR_SR_SHFT].batu =
284 1.29 matt BATL(va, BAT_BL_256M, BAT_Vs);
285 1.45 macallan fbaddr &= 0x0fffffff;
286 1.45 macallan fbaddr |= va;
287 1.29 matt break;
288 1.29 matt }
289 1.29 matt }
290 1.18 tsubai }
291 1.1 tsubai
292 1.11 tsubai /* initialize rasops */
293 1.11 tsubai ri->ri_width = width;
294 1.11 tsubai ri->ri_height = height;
295 1.11 tsubai ri->ri_depth = depth;
296 1.11 tsubai ri->ri_stride = linebytes;
297 1.45 macallan ri->ri_bits = (char *)fbaddr;
298 1.45 macallan ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
299 1.1 tsubai
300 1.8 tsubai /* If screen is smaller than 1024x768, use small font. */
301 1.45 macallan if ((width < 1024 || height < 768) && (romfont_loaded)) {
302 1.11 tsubai int cols, rows;
303 1.8 tsubai
304 1.45 macallan /*
305 1.45 macallan * XXX this assumes we're the console which may or may not
306 1.45 macallan * be the case
307 1.45 macallan */
308 1.11 tsubai OF_interpret("#lines", 1, &rows);
309 1.11 tsubai OF_interpret("#columns", 1, &cols);
310 1.11 tsubai ri->ri_font = &openfirm6x11;
311 1.11 tsubai ri->ri_wsfcookie = -1; /* not using wsfont */
312 1.11 tsubai rasops_init(ri, rows, cols);
313 1.11 tsubai
314 1.42 macallan ri->ri_xorigin = (width - cols * ri->ri_font->fontwidth) >> 1;
315 1.42 macallan ri->ri_yorigin = (height - rows * ri->ri_font->fontheight)
316 1.42 macallan >> 1;
317 1.45 macallan ri->ri_bits = (char *)fbaddr + ri->ri_xorigin +
318 1.11 tsubai ri->ri_stride * ri->ri_yorigin;
319 1.17 tsubai } else {
320 1.45 macallan /* use as much of the screen as the font permits */
321 1.45 macallan rasops_init(ri, height/8, width/8);
322 1.45 macallan ri->ri_caps = WSSCREEN_WSCOLORS;
323 1.45 macallan rasops_reconfig(ri, height / ri->ri_font->fontheight,
324 1.45 macallan width / ri->ri_font->fontwidth);
325 1.17 tsubai }
326 1.11 tsubai
327 1.45 macallan return TRUE;
328 1.1 tsubai }
329 1.1 tsubai
330 1.1 tsubai int
331 1.1 tsubai ofb_is_console()
332 1.1 tsubai {
333 1.1 tsubai int chosen, stdout, node;
334 1.1 tsubai char type[16];
335 1.1 tsubai
336 1.1 tsubai chosen = OF_finddevice("/chosen");
337 1.1 tsubai OF_getprop(chosen, "stdout", &stdout, 4);
338 1.1 tsubai node = OF_instance_to_package(stdout);
339 1.1 tsubai OF_getprop(node, "device_type", type, sizeof(type));
340 1.1 tsubai if (strcmp(type, "display") == 0)
341 1.1 tsubai return 1;
342 1.1 tsubai else
343 1.1 tsubai return 0;
344 1.1 tsubai }
345 1.1 tsubai
346 1.44 macallan static int
347 1.48 jmmv ofb_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag, struct lwp *l)
348 1.1 tsubai {
349 1.45 macallan struct vcons_data *vd = v;
350 1.45 macallan struct ofb_softc *sc = vd->cookie;
351 1.1 tsubai struct wsdisplay_fbinfo *wdf;
352 1.45 macallan struct vcons_screen *ms = vd->active;
353 1.1 tsubai struct grfinfo *gm;
354 1.1 tsubai
355 1.1 tsubai switch (cmd) {
356 1.1 tsubai case WSDISPLAYIO_GTYPE:
357 1.1 tsubai *(u_int *)data = WSDISPLAY_TYPE_PCIMISC; /* XXX ? */
358 1.1 tsubai return 0;
359 1.1 tsubai
360 1.1 tsubai case WSDISPLAYIO_GINFO:
361 1.45 macallan /* we won't get here without any screen anyway */
362 1.45 macallan if (ms != NULL) {
363 1.45 macallan wdf = (void *)data;
364 1.45 macallan wdf->height = ms->scr_ri.ri_width;
365 1.45 macallan wdf->width = ms->scr_ri.ri_height;
366 1.45 macallan wdf->depth = ms->scr_ri.ri_depth;
367 1.45 macallan wdf->cmsize = 256;
368 1.45 macallan return 0;
369 1.45 macallan } else
370 1.45 macallan return ENODEV;
371 1.1 tsubai
372 1.11 tsubai case WSDISPLAYIO_GETCMAP:
373 1.11 tsubai return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
374 1.11 tsubai
375 1.1 tsubai case WSDISPLAYIO_PUTCMAP:
376 1.11 tsubai return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
377 1.1 tsubai
378 1.1 tsubai /* XXX There are no way to know framebuffer pa from a user program. */
379 1.1 tsubai case GRFIOCGINFO:
380 1.45 macallan if (ms != NULL) {
381 1.45 macallan gm = (void *)data;
382 1.45 macallan memset(gm, 0, sizeof(struct grfinfo));
383 1.45 macallan gm->gd_fbaddr = (caddr_t)sc->sc_fbaddr;
384 1.45 macallan gm->gd_fbrowbytes = ms->scr_ri.ri_stride;
385 1.45 macallan return 0;
386 1.45 macallan } else
387 1.45 macallan return ENODEV;
388 1.45 macallan case WSDISPLAYIO_SMODE:
389 1.45 macallan {
390 1.45 macallan int new_mode = *(int*)data;
391 1.45 macallan if (new_mode != sc->sc_mode)
392 1.45 macallan {
393 1.45 macallan sc->sc_mode = new_mode;
394 1.45 macallan if (new_mode == WSDISPLAYIO_MODE_EMUL)
395 1.45 macallan {
396 1.45 macallan ofb_init_cmap(sc);
397 1.45 macallan vcons_redraw_screen(ms);
398 1.45 macallan }
399 1.45 macallan }
400 1.45 macallan }
401 1.1 tsubai return 0;
402 1.27 nathanw /* PCI config read/write passthrough. */
403 1.27 nathanw case PCI_IOC_CFGREAD:
404 1.27 nathanw case PCI_IOC_CFGWRITE:
405 1.27 nathanw return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
406 1.43 christos cmd, data, flag, l));
407 1.1 tsubai }
408 1.26 atatat return EPASSTHROUGH;
409 1.1 tsubai }
410 1.1 tsubai
411 1.44 macallan static paddr_t
412 1.48 jmmv ofb_mmap(void *v, void *vs, off_t offset, int prot)
413 1.1 tsubai {
414 1.45 macallan struct vcons_data *vd = v;
415 1.45 macallan struct ofb_softc *sc = vd->cookie;
416 1.45 macallan struct rasops_info *ri;
417 1.13 tsubai u_int32_t *ap = sc->sc_addrs;
418 1.42 macallan struct proc *me;
419 1.13 tsubai int i;
420 1.13 tsubai
421 1.45 macallan if (vd->active == NULL) {
422 1.45 macallan printf("%s: no active screen.\n", sc->sc_dev.dv_xname);
423 1.45 macallan return -1;
424 1.45 macallan }
425 1.45 macallan
426 1.45 macallan ri = &vd->active->scr_ri;
427 1.45 macallan
428 1.42 macallan /* framebuffer at offset 0 */
429 1.13 tsubai if (offset >=0 && offset < (ri->ri_stride * ri->ri_height))
430 1.45 macallan return sc->sc_fbaddr + offset;
431 1.13 tsubai
432 1.42 macallan /*
433 1.42 macallan * restrict all other mappings to processes with superuser privileges
434 1.42 macallan * or the kernel itself
435 1.42 macallan */
436 1.42 macallan me = __curproc();
437 1.42 macallan if (me != NULL) {
438 1.49 elad if (kauth_authorize_generic(me->p_cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
439 1.45 macallan printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
440 1.42 macallan return -1;
441 1.42 macallan }
442 1.42 macallan }
443 1.42 macallan
444 1.42 macallan /* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
445 1.42 macallan #ifdef OFB_FAKE_VGA_FB
446 1.42 macallan if (offset >=0xa0000 && offset < 0xbffff)
447 1.45 macallan return sc->sc_fbaddr + offset - 0xa0000;
448 1.42 macallan #endif
449 1.42 macallan
450 1.42 macallan /* allow to map our IO space */
451 1.42 macallan if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
452 1.42 macallan return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
453 1.42 macallan BUS_SPACE_MAP_LINEAR);
454 1.42 macallan }
455 1.42 macallan
456 1.13 tsubai for (i = 0; i < 6; i++) {
457 1.13 tsubai switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
458 1.13 tsubai case OFW_PCI_PHYS_HI_SPACE_MEM32:
459 1.42 macallan if (offset >= ap[2] && offset < ap[2] + ap[4])
460 1.42 macallan return bus_space_mmap(sc->sc_memt, offset, 0,
461 1.42 macallan prot, BUS_SPACE_MAP_LINEAR);
462 1.13 tsubai }
463 1.13 tsubai ap += 5;
464 1.13 tsubai }
465 1.1 tsubai
466 1.13 tsubai return -1;
467 1.1 tsubai }
468 1.1 tsubai
469 1.1 tsubai int
470 1.1 tsubai ofb_cnattach()
471 1.1 tsubai {
472 1.45 macallan struct rasops_info *ri = &ofb_console_screen.scr_ri;
473 1.1 tsubai long defattr;
474 1.11 tsubai int crow = 0;
475 1.1 tsubai int chosen, stdout, node;
476 1.1 tsubai
477 1.1 tsubai chosen = OF_finddevice("/chosen");
478 1.1 tsubai OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
479 1.1 tsubai node = OF_instance_to_package(stdout);
480 1.45 macallan console_node = node;
481 1.45 macallan console_instance = stdout;
482 1.1 tsubai
483 1.11 tsubai /* get current cursor position */
484 1.11 tsubai OF_interpret("line#", 1, &crow);
485 1.8 tsubai
486 1.11 tsubai /* move (rom monitor) cursor to the lowest line - 1 */
487 1.11 tsubai OF_interpret("#lines 2 - to line#", 0);
488 1.45 macallan
489 1.45 macallan wsfont_init();
490 1.45 macallan if (copy_rom_font() == 0) {
491 1.45 macallan romfont_loaded = 1;
492 1.45 macallan }
493 1.45 macallan
494 1.45 macallan /* set up rasops */
495 1.45 macallan ofb_init_rasops(console_node, ri);
496 1.8 tsubai
497 1.45 macallan /*
498 1.45 macallan * no need to clear the screen here when we're mimicing firmware
499 1.45 macallan * output anyway
500 1.45 macallan */
501 1.45 macallan #if 0
502 1.11 tsubai if (ri->ri_width >= 1024 && ri->ri_height >= 768) {
503 1.11 tsubai int i, screenbytes = ri->ri_stride * ri->ri_height;
504 1.8 tsubai
505 1.8 tsubai for (i = 0; i < screenbytes; i += sizeof(u_int32_t))
506 1.45 macallan *(u_int32_t *)(fbaddr + i) = 0xffffffff;
507 1.8 tsubai crow = 0;
508 1.8 tsubai }
509 1.45 macallan #endif
510 1.45 macallan
511 1.45 macallan ofb_stdscreen.nrows = ri->ri_rows;
512 1.45 macallan ofb_stdscreen.ncols = ri->ri_cols;
513 1.45 macallan ofb_stdscreen.textops = &ri->ri_ops;
514 1.45 macallan ofb_stdscreen.capabilities = ri->ri_caps;
515 1.8 tsubai
516 1.45 macallan ri->ri_ops.allocattr(ri, 0, 0, 0, &defattr);
517 1.11 tsubai wsdisplay_cnattach(&ofb_stdscreen, ri, 0, crow, defattr);
518 1.8 tsubai
519 1.8 tsubai return 0;
520 1.8 tsubai }
521 1.8 tsubai
522 1.44 macallan static int
523 1.8 tsubai copy_rom_font()
524 1.8 tsubai {
525 1.8 tsubai u_char *romfont;
526 1.8 tsubai int char_width, char_height;
527 1.8 tsubai int chosen, mmu, m, e;
528 1.8 tsubai
529 1.8 tsubai /* Get ROM FONT address. */
530 1.8 tsubai OF_interpret("font-adr", 1, &romfont);
531 1.8 tsubai if (romfont == NULL)
532 1.8 tsubai return -1;
533 1.8 tsubai
534 1.8 tsubai chosen = OF_finddevice("/chosen");
535 1.8 tsubai OF_getprop(chosen, "mmu", &mmu, 4);
536 1.1 tsubai
537 1.8 tsubai /*
538 1.8 tsubai * Convert to physcal address. We cannot access to Open Firmware's
539 1.8 tsubai * virtual address space.
540 1.8 tsubai */
541 1.8 tsubai OF_call_method("translate", mmu, 1, 3, romfont, &romfont, &m, &e);
542 1.8 tsubai
543 1.8 tsubai /* Get character size */
544 1.8 tsubai OF_interpret("char-width", 1, &char_width);
545 1.8 tsubai OF_interpret("char-height", 1, &char_height);
546 1.8 tsubai
547 1.11 tsubai openfirm6x11.name = "Open Firmware";
548 1.11 tsubai openfirm6x11.firstchar = 32;
549 1.11 tsubai openfirm6x11.numchars = 96;
550 1.11 tsubai openfirm6x11.encoding = WSDISPLAY_FONTENC_ISO;
551 1.11 tsubai openfirm6x11.fontwidth = char_width;
552 1.11 tsubai openfirm6x11.fontheight = char_height;
553 1.11 tsubai openfirm6x11.stride = 1;
554 1.11 tsubai openfirm6x11.bitorder = WSDISPLAY_FONTORDER_L2R;
555 1.11 tsubai openfirm6x11.byteorder = WSDISPLAY_FONTORDER_L2R;
556 1.11 tsubai openfirm6x11.data = romfont;
557 1.11 tsubai
558 1.11 tsubai return 0;
559 1.11 tsubai }
560 1.11 tsubai
561 1.44 macallan static int
562 1.44 macallan ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
563 1.11 tsubai {
564 1.11 tsubai u_int index = cm->index;
565 1.11 tsubai u_int count = cm->count;
566 1.11 tsubai int error;
567 1.11 tsubai
568 1.11 tsubai if (index >= 256 || count > 256 || index + count > 256)
569 1.11 tsubai return EINVAL;
570 1.11 tsubai
571 1.11 tsubai error = copyout(&sc->sc_cmap_red[index], cm->red, count);
572 1.11 tsubai if (error)
573 1.11 tsubai return error;
574 1.11 tsubai error = copyout(&sc->sc_cmap_green[index], cm->green, count);
575 1.11 tsubai if (error)
576 1.11 tsubai return error;
577 1.11 tsubai error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
578 1.11 tsubai if (error)
579 1.11 tsubai return error;
580 1.8 tsubai
581 1.1 tsubai return 0;
582 1.1 tsubai }
583 1.1 tsubai
584 1.45 macallan int
585 1.44 macallan ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
586 1.1 tsubai {
587 1.25 jdolecek u_int index = cm->index;
588 1.25 jdolecek u_int count = cm->count;
589 1.38 chs int i, error;
590 1.38 chs u_char rbuf[256], gbuf[256], bbuf[256];
591 1.1 tsubai u_char *r, *g, *b;
592 1.1 tsubai
593 1.1 tsubai if (cm->index >= 256 || cm->count > 256 ||
594 1.1 tsubai (cm->index + cm->count) > 256)
595 1.1 tsubai return EINVAL;
596 1.38 chs error = copyin(cm->red, &rbuf[index], count);
597 1.38 chs if (error)
598 1.38 chs return error;
599 1.38 chs error = copyin(cm->green, &gbuf[index], count);
600 1.38 chs if (error)
601 1.38 chs return error;
602 1.38 chs error = copyin(cm->blue, &bbuf[index], count);
603 1.38 chs if (error)
604 1.38 chs return error;
605 1.38 chs
606 1.38 chs memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
607 1.38 chs memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
608 1.38 chs memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
609 1.1 tsubai
610 1.1 tsubai r = &sc->sc_cmap_red[index];
611 1.1 tsubai g = &sc->sc_cmap_green[index];
612 1.1 tsubai b = &sc->sc_cmap_blue[index];
613 1.1 tsubai for (i = 0; i < count; i++) {
614 1.45 macallan OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
615 1.1 tsubai r++, g++, b++, index++;
616 1.1 tsubai }
617 1.1 tsubai return 0;
618 1.1 tsubai }
619 1.45 macallan
620 1.45 macallan static void
621 1.45 macallan ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
622 1.45 macallan {
623 1.45 macallan if ((idx<0) || (idx > 255))
624 1.45 macallan return;
625 1.45 macallan OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
626 1.45 macallan sc->sc_cmap_red[idx] = r;
627 1.45 macallan sc->sc_cmap_green[idx] = g;
628 1.45 macallan sc->sc_cmap_blue[idx] = b;
629 1.45 macallan }
630 1.45 macallan
631 1.45 macallan static void
632 1.45 macallan ofb_init_cmap(struct ofb_softc *sc)
633 1.45 macallan {
634 1.45 macallan int idx, i;
635 1.45 macallan /* mess with the palette only when we're running in 8 bit */
636 1.45 macallan if (ofb_console_screen.scr_ri.ri_depth == 8) {
637 1.45 macallan idx = 0;
638 1.45 macallan for (i = 0; i < 256; i++) {
639 1.45 macallan ofb_putpalreg(sc, i, rasops_cmap[idx],
640 1.45 macallan rasops_cmap[idx + 1], rasops_cmap[idx + 2]);
641 1.45 macallan idx += 3;
642 1.45 macallan }
643 1.45 macallan }
644 1.45 macallan }
645