2 * Copyright (c) 2011 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
35 #include <dev/vt/vt.h>
36 #include <dev/vt/hw/fb/vt_fb.h>
37 #include <dev/vt/colors/vt_termcolors.h>
42 #include <machine/bus.h>
44 #include <machine/bus_private.h>
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_pci.h>
56 bus_space_tag_t sc_memt;
59 static vd_probe_t ofwfb_probe;
60 static vd_init_t ofwfb_init;
61 static vd_bitblt_text_t ofwfb_bitblt_text;
62 static vd_bitblt_bmp_t ofwfb_bitblt_bitmap;
64 static const struct vt_driver vt_ofwfb_driver = {
66 .vd_probe = ofwfb_probe,
67 .vd_init = ofwfb_init,
68 .vd_blank = vt_fb_blank,
69 .vd_bitblt_text = ofwfb_bitblt_text,
70 .vd_bitblt_bmp = ofwfb_bitblt_bitmap,
71 .vd_fb_ioctl = vt_fb_ioctl,
72 .vd_fb_mmap = vt_fb_mmap,
73 .vd_priority = VD_PRIORITY_GENERIC+1,
76 static struct ofwfb_softc ofwfb_conssoftc;
77 VT_DRIVER_DECLARE(vt_ofwfb, vt_ofwfb_driver);
80 ofwfb_probe(struct vt_device *vd)
82 phandle_t chosen, node;
86 chosen = OF_finddevice("/chosen");
87 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
88 node = OF_instance_to_package(stdout);
91 * The "/chosen/stdout" does not exist try
92 * using "screen" directly.
94 node = OF_finddevice("screen");
96 OF_getprop(node, "device_type", type, sizeof(type));
97 if (strcmp(type, "display") != 0)
101 return (CN_INTERNAL);
105 ofwfb_bitblt_bitmap(struct vt_device *vd, const struct vt_window *vw,
106 const uint8_t *pattern, const uint8_t *mask,
107 unsigned int width, unsigned int height,
108 unsigned int x, unsigned int y, term_color_t fg, term_color_t bg)
110 struct fb_info *sc = vd->vd_softc;
120 fgc = sc->fb_cmap[fg];
121 bgc = sc->fb_cmap[bg];
124 line = (sc->fb_stride * y) + x * sc->fb_bpp/8;
125 if (mask == NULL && sc->fb_bpp == 8 && (width % 8 == 0)) {
126 /* Don't try to put off screen pixels */
127 if (((x + width) > vd->vd_width) || ((y + height) >
131 for (; height > 0; height--) {
132 for (c = 0; c < width; c += 8) {
136 * Assume that there is more background than
137 * foreground in characters and init accordingly
139 ch1.l = ch2.l = (bg << 24) | (bg << 16) |
143 * Calculate 2 x 4-chars at a time, and then
146 if (b & 0x80) ch1.c[0] = fg;
147 if (b & 0x40) ch1.c[1] = fg;
148 if (b & 0x20) ch1.c[2] = fg;
149 if (b & 0x10) ch1.c[3] = fg;
151 if (b & 0x08) ch2.c[0] = fg;
152 if (b & 0x04) ch2.c[1] = fg;
153 if (b & 0x02) ch2.c[2] = fg;
154 if (b & 0x01) ch2.c[3] = fg;
156 *(uint32_t *)(sc->fb_vbase + line + c) = ch1.l;
157 *(uint32_t *)(sc->fb_vbase + line + c + 4) =
160 line += sc->fb_stride;
164 l < height && y + l < vw->vw_draw_area.tr_end.tp_row;
167 c < width && x + c < vw->vw_draw_area.tr_end.tp_col;
178 /* Skip pixel write, if mask not set. */
184 *(uint8_t *)(sc->fb_vbase + line + c) =
188 *(uint32_t *)(sc->fb_vbase + line + 4*c)
189 = (b & 0x80) ? fgc : bgc;
196 line += sc->fb_stride;
202 ofwfb_bitblt_text(struct vt_device *vd, const struct vt_window *vw,
203 const term_rect_t *area)
205 unsigned int col, row, x, y;
209 const uint8_t *pattern;
213 for (row = area->tr_begin.tp_row; row < area->tr_end.tp_row; ++row) {
214 for (col = area->tr_begin.tp_col; col < area->tr_end.tp_col;
216 x = col * vf->vf_width +
217 vw->vw_draw_area.tr_begin.tp_col;
218 y = row * vf->vf_height +
219 vw->vw_draw_area.tr_begin.tp_row;
221 c = VTBUF_GET_FIELD(&vw->vw_buf, row, col);
222 pattern = vtfont_lookup(vf, c);
223 vt_determine_colors(c,
224 VTBUF_ISCURSOR(&vw->vw_buf, row, col), &fg, &bg);
226 ofwfb_bitblt_bitmap(vd, vw,
227 pattern, NULL, vf->vf_width, vf->vf_height,
232 #ifndef SC_NO_CUTPASTE
236 term_rect_t drawn_area;
238 drawn_area.tr_begin.tp_col = area->tr_begin.tp_col * vf->vf_width;
239 drawn_area.tr_begin.tp_row = area->tr_begin.tp_row * vf->vf_height;
240 drawn_area.tr_end.tp_col = area->tr_end.tp_col * vf->vf_width;
241 drawn_area.tr_end.tp_row = area->tr_end.tp_row * vf->vf_height;
243 if (vt_is_cursor_in_area(vd, &drawn_area)) {
244 ofwfb_bitblt_bitmap(vd, vw,
245 vd->vd_mcursor->map, vd->vd_mcursor->mask,
246 vd->vd_mcursor->width, vd->vd_mcursor->height,
247 vd->vd_mx_drawn + vw->vw_draw_area.tr_begin.tp_col,
248 vd->vd_my_drawn + vw->vw_draw_area.tr_begin.tp_row,
249 vd->vd_mcursor_fg, vd->vd_mcursor_bg);
255 ofwfb_initialize(struct vt_device *vd)
257 struct ofwfb_softc *sc = vd->vd_softc;
263 * Set up the color map
266 switch (sc->fb.fb_bpp) {
268 vt_generate_cons_palette(sc->fb.fb_cmap, COLOR_FORMAT_RGB, 255,
271 for (i = 0; i < 16; i++) {
272 OF_call_method("color!", sc->sc_handle, 4, 1,
273 (cell_t)((sc->fb.fb_cmap[i] >> 16) & 0xff),
274 (cell_t)((sc->fb.fb_cmap[i] >> 8) & 0xff),
275 (cell_t)((sc->fb.fb_cmap[i] >> 0) & 0xff),
282 * We bypass the usual bus_space_() accessors here, mostly
283 * for performance reasons. In particular, we don't want
284 * any barrier operations that may be performed and handle
285 * endianness slightly different. Figure out the host-view
286 * endianness of the frame buffer.
288 oldpix = bus_space_read_4(sc->sc_memt, sc->fb.fb_vbase, 0);
289 bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, 0xff000000);
290 if (*(uint8_t *)(sc->fb.fb_vbase) == 0xff)
291 vt_generate_cons_palette(sc->fb.fb_cmap,
292 COLOR_FORMAT_RGB, 255, 0, 255, 8, 255, 16);
294 vt_generate_cons_palette(sc->fb.fb_cmap,
295 COLOR_FORMAT_RGB, 255, 16, 255, 8, 255, 0);
296 bus_space_write_4(sc->sc_memt, sc->fb.fb_vbase, 0, oldpix);
300 panic("Unknown color space depth %d", sc->fb.fb_bpp);
304 sc->fb.fb_cmsize = 16;
308 ofwfb_init(struct vt_device *vd)
310 struct ofwfb_softc *sc;
314 uint32_t depth, height, width, stride;
318 static struct bus_space_tag ofwfb_memt[1];
323 /* Initialize softc */
324 vd->vd_softc = sc = &ofwfb_conssoftc;
326 chosen = OF_finddevice("/chosen");
327 OF_getprop(chosen, "stdout", &sc->sc_handle, sizeof(ihandle_t));
328 node = OF_instance_to_package(sc->sc_handle);
331 * The "/chosen/stdout" does not exist try
332 * using "screen" directly.
334 node = OF_finddevice("screen");
335 sc->sc_handle = OF_open("screen");
337 OF_getprop(node, "device_type", type, sizeof(type));
338 if (strcmp(type, "display") != 0)
341 /* Keep track of the OF node */
345 * Try to use a 32-bit framebuffer if possible. This may be
346 * unimplemented and fail. That's fine -- it just means we are
347 * stuck with the defaults.
349 OF_call_method("set-depth", sc->sc_handle, 1, 1, (cell_t)32, &i);
351 /* Make sure we have needed properties */
352 if (OF_getproplen(node, "height") != sizeof(height) ||
353 OF_getproplen(node, "width") != sizeof(width) ||
354 OF_getproplen(node, "depth") != sizeof(depth) ||
355 OF_getproplen(node, "linebytes") != sizeof(sc->fb.fb_stride))
358 /* Only support 8 and 32-bit framebuffers */
359 OF_getprop(node, "depth", &depth, sizeof(depth));
360 if (depth != 8 && depth != 32)
362 sc->fb.fb_bpp = sc->fb.fb_depth = depth;
364 OF_getprop(node, "height", &height, sizeof(height));
365 OF_getprop(node, "width", &width, sizeof(width));
366 OF_getprop(node, "linebytes", &stride, sizeof(stride));
368 sc->fb.fb_height = height;
369 sc->fb.fb_width = width;
370 sc->fb.fb_stride = stride;
371 sc->fb.fb_size = sc->fb.fb_height * sc->fb.fb_stride;
374 * Grab the physical address of the framebuffer, and then map it
375 * into our memory space. If the MMU is not yet up, it will be
376 * remapped for us when relocation turns on.
378 if (OF_getproplen(node, "address") == sizeof(fb_phys)) {
379 /* XXX We assume #address-cells is 1 at this point. */
380 OF_getprop(node, "address", &fb_phys, sizeof(fb_phys));
382 #if defined(__powerpc__)
383 sc->sc_memt = &bs_be_tag;
384 bus_space_map(sc->sc_memt, fb_phys, sc->fb.fb_size,
385 BUS_SPACE_MAP_PREFETCHABLE, &sc->fb.fb_vbase);
386 #elif defined(__sparc64__)
387 OF_decode_addr(node, 0, &space, &phys);
388 sc->sc_memt = &ofwfb_memt[0];
390 sparc64_fake_bustag(space, fb_phys, sc->sc_memt);
391 #elif defined(__arm__)
392 sc->sc_memt = fdtbus_bs_tag;
393 bus_space_map(sc->sc_memt, sc->fb.fb_pbase, sc->fb.fb_size,
394 BUS_SPACE_MAP_PREFETCHABLE,
395 (bus_space_handle_t *)&sc->fb.fb_vbase);
397 #error Unsupported platform!
400 sc->fb.fb_pbase = fb_phys;
403 * Some IBM systems don't have an address property. Try to
404 * guess the framebuffer region from the assigned addresses.
405 * This is ugly, but there doesn't seem to be an alternative.
406 * Linux does the same thing.
409 struct ofw_pci_register pciaddrs[8];
410 int num_pciaddrs = 0;
413 * Get the PCI addresses of the adapter, if present. The node
414 * may be the child of the PCI device: in that case, try the
415 * parent for the assigned-addresses property.
417 len = OF_getprop(node, "assigned-addresses", pciaddrs,
420 len = OF_getprop(OF_parent(node), "assigned-addresses",
421 pciaddrs, sizeof(pciaddrs));
425 num_pciaddrs = len / sizeof(struct ofw_pci_register);
427 fb_phys = num_pciaddrs;
428 for (i = 0; i < num_pciaddrs; i++) {
429 /* If it is too small, not the framebuffer */
430 if (pciaddrs[i].size_lo < sc->fb.fb_stride * height)
432 /* If it is not memory, it isn't either */
433 if (!(pciaddrs[i].phys_hi &
434 OFW_PCI_PHYS_HI_SPACE_MEM32))
437 /* This could be the framebuffer */
440 /* If it is prefetchable, it certainly is */
441 if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE)
445 if (fb_phys == num_pciaddrs) /* No candidates found */
448 #if defined(__powerpc__)
449 OF_decode_addr(node, fb_phys, &sc->sc_memt, &sc->fb.fb_vbase);
450 sc->fb.fb_pbase = sc->fb.fb_vbase; /* 1:1 mapped */
452 /* No ability to interpret assigned-addresses otherwise */
458 ofwfb_initialize(vd);
461 return (CN_INTERNAL);