vt_early_fb.c revision 271684
1257726Sray/*-
2257726Sray * Copyright (c) 2013 The FreeBSD Foundation
3257726Sray * All rights reserved.
4257726Sray *
5257726Sray * This software was developed by Aleksandr Rybalko under sponsorship from the
6257726Sray * FreeBSD Foundation.
7257726Sray *
8257726Sray * Redistribution and use in source and binary forms, with or without
9257726Sray * modification, are permitted provided that the following conditions
10257726Sray * are met:
11257726Sray * 1. Redistributions of source code must retain the above copyright
12257726Sray *    notice, this list of conditions and the following disclaimer.
13257726Sray * 2. Redistributions in binary form must reproduce the above copyright
14257726Sray *    notice, this list of conditions and the following disclaimer in the
15257726Sray *    documentation and/or other materials provided with the distribution.
16257726Sray *
17257726Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18257726Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19257726Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20257726Sray * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21257726Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22257726Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23257726Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24257726Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25257726Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26257726Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27257726Sray * SUCH DAMAGE.
28257726Sray *
29257726Sray * $FreeBSD: head/sys/dev/vt/hw/fb/vt_early_fb.c 271684 2014-09-16 18:02:24Z dumbbell $
30257726Sray */
31257726Sray
32257726Sray#include <sys/cdefs.h>
33257726Sray__FBSDID("$FreeBSD: head/sys/dev/vt/hw/fb/vt_early_fb.c 271684 2014-09-16 18:02:24Z dumbbell $");
34257726Sray
35257726Sray#include <sys/param.h>
36257726Sray#include <sys/systm.h>
37257726Sray#include <sys/kernel.h>
38257726Sray#include <sys/fbio.h>
39257726Sray
40257726Sray#include "opt_platform.h"
41257726Sray
42257726Sray#ifdef	FDT
43257726Sray#include <dev/fdt/fdt_common.h>
44257726Sray#include <dev/ofw/ofw_bus.h>
45257726Sray#include <dev/ofw/ofw_bus_subr.h>
46257726Sray#include <dev/ofw/ofw_pci.h>
47264182Srpaulo#include <machine/fdt.h>
48257726Sray#endif
49257726Sray
50257726Sray#include <dev/vt/vt.h>
51257726Sray#include <dev/vt/hw/fb/vt_fb.h>
52257726Sray#include <dev/vt/colors/vt_termcolors.h>
53257726Sray
54257726Sraystatic vd_init_t vt_efb_init;
55265397Sraystatic vd_probe_t vt_efb_probe;
56257726Sray
57257726Sraystatic struct vt_driver vt_fb_early_driver = {
58265397Sray	.vd_name = "efb",
59265397Sray	.vd_probe = vt_efb_probe,
60257726Sray	.vd_init = vt_efb_init,
61257726Sray	.vd_blank = vt_fb_blank,
62270411Sdumbbell	.vd_bitblt_text = vt_fb_bitblt_text,
63270431Sdumbbell	.vd_bitblt_bmp = vt_fb_bitblt_bitmap,
64271684Sdumbbell	.vd_drawrect = vt_fb_drawrect,
65271684Sdumbbell	.vd_setpixel = vt_fb_setpixel,
66257726Sray	.vd_priority = VD_PRIORITY_GENERIC,
67257726Sray};
68257726Sray
69265397Sraystatic struct fb_info local_info;
70265397SrayVT_DRIVER_DECLARE(vt_efb, vt_fb_early_driver);
71257726Sray
72257726Sraystatic void
73257726Sray#ifdef	FDT
74257726Srayvt_efb_initialize(struct fb_info *info, phandle_t node)
75257726Sray#else
76257726Srayvt_efb_initialize(struct fb_info *info)
77257726Sray#endif
78257726Sray{
79257726Sray#ifdef	FDT
80257726Sray	char name[64];
81257726Sray	cell_t retval;
82257726Sray	ihandle_t ih;
83257726Sray	int i;
84257726Sray
85257726Sray	/* Open display device, thereby initializing it */
86257726Sray	memset(name, 0, sizeof(name));
87257726Sray	OF_package_to_path(node, name, sizeof(name));
88257726Sray	ih = OF_open(name);
89257726Sray#endif
90257726Sray
91257726Sray	/*
92257726Sray	 * Set up the color map
93257726Sray	 */
94257726Sray	switch (info->fb_depth) {
95257726Sray	case 8:
96269783Sdumbbell		vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB,
97257726Sray		    0x7, 5, 0x7, 2, 0x3, 0);
98257726Sray		break;
99257726Sray	case 15:
100269783Sdumbbell		vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB,
101257726Sray		    0x1f, 10, 0x1f, 5, 0x1f, 0);
102257726Sray		break;
103257726Sray	case 16:
104269783Sdumbbell		vt_generate_cons_palette(info->fb_cmap, COLOR_FORMAT_RGB,
105257726Sray		    0x1f, 11, 0x3f, 5, 0x1f, 0);
106257726Sray		break;
107257726Sray	case 24:
108257726Sray	case 32:
109257726Sray#if BYTE_ORDER == BIG_ENDIAN
110269783Sdumbbell		vt_generate_cons_palette(info->fb_cmap,
111269783Sdumbbell		    COLOR_FORMAT_RGB, 255, 0, 255, 8, 255, 16);
112269783Sdumbbell#else
113269783Sdumbbell		vt_generate_cons_palette(info->fb_cmap,
114257726Sray		    COLOR_FORMAT_RGB, 255, 16, 255, 8, 255, 0);
115257726Sray#endif
116257726Sray#ifdef	FDT
117257726Sray		for (i = 0; i < 16; i++) {
118257726Sray			OF_call_method("color!", ih, 4, 1,
119257726Sray			    (cell_t)((info->fb_cmap[i] >> 16) & 0xff),
120257726Sray			    (cell_t)((info->fb_cmap[i] >> 8) & 0xff),
121257726Sray			    (cell_t)((info->fb_cmap[i] >> 0) & 0xff),
122257726Sray			    (cell_t)i, &retval);
123257726Sray		}
124257726Sray#endif
125257726Sray		break;
126257726Sray
127257726Sray	default:
128257726Sray		panic("Unknown color space fb_depth %d", info->fb_depth);
129257726Sray		break;
130257726Sray        }
131257726Sray}
132257726Sray
133265397Sraystatic phandle_t
134265397Srayvt_efb_get_fbnode()
135257726Sray{
136257726Sray	phandle_t chosen, node;
137257726Sray	ihandle_t stdout;
138257726Sray	char type[64];
139257726Sray
140257726Sray	chosen = OF_finddevice("/chosen");
141257726Sray	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
142257726Sray	node = OF_instance_to_package(stdout);
143265397Sray	if (node != -1) {
144265397Sray		/* The "/chosen/stdout" present. */
145265397Sray		OF_getprop(node, "device_type", type, sizeof(type));
146265397Sray		/* Check if it has "display" type. */
147265397Sray		if (strcmp(type, "display") == 0)
148265397Sray			return (node);
149257726Sray	}
150265397Sray	/* Try device with name "screen". */
151265397Sray	node = OF_finddevice("screen");
152265397Sray
153265397Sray	return (node);
154265397Sray}
155265397Sray
156265397Sraystatic int
157265397Srayvt_efb_probe(struct vt_device *vd)
158265397Sray{
159265397Sray	phandle_t node;
160265397Sray
161265397Sray	node = vt_efb_get_fbnode();
162265397Sray	if (node == -1)
163257726Sray		return (CN_DEAD);
164257726Sray
165265397Sray	if ((OF_getproplen(node, "height") <= 0) ||
166265397Sray	    (OF_getproplen(node, "width") <= 0) ||
167265397Sray	    (OF_getproplen(node, "depth") <= 0) ||
168265397Sray	    (OF_getproplen(node, "linebytes") <= 0))
169265397Sray		return (CN_DEAD);
170265397Sray
171265397Sray	return (CN_INTERNAL);
172265397Sray}
173265397Sray
174265397Sraystatic int
175265397Srayvt_efb_init(struct vt_device *vd)
176265397Sray{
177265397Sray	struct ofw_pci_register pciaddrs[8];
178265397Sray	struct fb_info *info;
179265397Sray	int i, len, n_pciaddrs;
180265397Sray	phandle_t node;
181265397Sray
182265397Sray	if (vd->vd_softc == NULL)
183265397Sray		vd->vd_softc = (void *)&local_info;
184265397Sray
185265397Sray	info = vd->vd_softc;
186265397Sray
187265397Sray	node = vt_efb_get_fbnode();
188265397Sray	if (node == -1)
189265397Sray		return (CN_DEAD);
190265397Sray
191257726Sray#define	GET(name, var)							\
192257726Sray	if (OF_getproplen(node, (name)) != sizeof(info->fb_##var))	\
193257726Sray		return (CN_DEAD);					\
194257726Sray	OF_getencprop(node, (name), &info->fb_##var, sizeof(info->fb_##var)); \
195257726Sray	if (info->fb_##var == 0)					\
196257726Sray		return (CN_DEAD);
197257726Sray
198257726Sray	GET("height", height)
199257726Sray	GET("width", width)
200257726Sray	GET("depth", depth)
201257726Sray	GET("linebytes", stride)
202257726Sray#undef GET
203257726Sray
204257726Sray	info->fb_size = info->fb_height * info->fb_stride;
205257726Sray
206257726Sray	/*
207257726Sray	 * Get the PCI addresses of the adapter, if present. The node may be the
208257726Sray	 * child of the PCI device: in that case, try the parent for
209257726Sray	 * the assigned-addresses property.
210257726Sray	 */
211257726Sray	len = OF_getprop(node, "assigned-addresses", pciaddrs,
212257726Sray	    sizeof(pciaddrs));
213257726Sray	if (len == -1) {
214257726Sray		len = OF_getprop(OF_parent(node), "assigned-addresses",
215257726Sray		    pciaddrs, sizeof(pciaddrs));
216257726Sray        }
217257726Sray        if (len == -1)
218257726Sray                len = 0;
219257726Sray	n_pciaddrs = len / sizeof(struct ofw_pci_register);
220257726Sray
221257726Sray	/*
222257726Sray	 * Grab the physical address of the framebuffer, and then map it
223257726Sray	 * into our memory space. If the MMU is not yet up, it will be
224257726Sray	 * remapped for us when relocation turns on.
225257726Sray	 */
226257726Sray	if (OF_getproplen(node, "address") == sizeof(info->fb_pbase)) {
227257726Sray	 	/* XXX We assume #address-cells is 1 at this point. */
228257726Sray		OF_getencprop(node, "address", &info->fb_pbase,
229257726Sray		    sizeof(info->fb_pbase));
230257726Sray
231257726Sray	#if defined(__powerpc__)
232257726Sray		sc->sc_memt = &bs_be_tag;
233257726Sray		bus_space_map(sc->sc_memt, info->fb_pbase, info->fb_size,
234257726Sray		    BUS_SPACE_MAP_PREFETCHABLE, &info->fb_vbase);
235257726Sray	#elif defined(__sparc64__)
236257726Sray		OF_decode_addr(node, 0, &space, &phys);
237257726Sray		sc->sc_memt = &vt_efb_memt[0];
238257726Sray		info->addr = sparc64_fake_bustag(space, fb_phys, sc->sc_memt);
239257726Sray	#else
240257726Sray		bus_space_map(fdtbus_bs_tag, info->fb_pbase, info->fb_size,
241257726Sray		    BUS_SPACE_MAP_PREFETCHABLE,
242257726Sray		    (bus_space_handle_t *)&info->fb_vbase);
243257726Sray	#endif
244257726Sray	} else {
245257726Sray		/*
246257726Sray		 * Some IBM systems don't have an address property. Try to
247257726Sray		 * guess the framebuffer region from the assigned addresses.
248257726Sray		 * This is ugly, but there doesn't seem to be an alternative.
249257726Sray		 * Linux does the same thing.
250257726Sray		 */
251257726Sray
252257726Sray		info->fb_pbase = n_pciaddrs;
253257726Sray		for (i = 0; i < n_pciaddrs; i++) {
254257726Sray			/* If it is too small, not the framebuffer */
255257726Sray			if (pciaddrs[i].size_lo < info->fb_size)
256257726Sray				continue;
257257726Sray			/* If it is not memory, it isn't either */
258257726Sray			if (!(pciaddrs[i].phys_hi &
259257726Sray			    OFW_PCI_PHYS_HI_SPACE_MEM32))
260257726Sray				continue;
261257726Sray
262257726Sray			/* This could be the framebuffer */
263257726Sray			info->fb_pbase = i;
264257726Sray
265257726Sray			/* If it is prefetchable, it certainly is */
266257726Sray			if (pciaddrs[i].phys_hi & OFW_PCI_PHYS_HI_PREFETCHABLE)
267257726Sray				break;
268257726Sray		}
269257726Sray
270257726Sray		if (info->fb_pbase == n_pciaddrs) /* No candidates found */
271257726Sray			return (CN_DEAD);
272257726Sray
273257726Sray	#if defined(__powerpc__)
274257726Sray		OF_decode_addr(node, info->fb_pbase, &sc->sc_memt,
275257726Sray		    &info->fb_vbase);
276257726Sray	#elif defined(__sparc64__)
277257726Sray		OF_decode_addr(node, info->fb_pbase, &space, &info->fb_pbase);
278257726Sray		sc->sc_memt = &vt_efb_memt[0];
279257726Sray		info->fb_vbase = sparc64_fake_bustag(space, info->fb_pbase,
280257726Sray		    sc->sc_memt);
281257726Sray	#else
282257726Sray		bus_space_map(fdtbus_bs_tag, info->fb_pbase, info->fb_size,
283257726Sray		    BUS_SPACE_MAP_PREFETCHABLE,
284257726Sray		    (bus_space_handle_t *)&info->fb_vbase);
285257726Sray	#endif
286257726Sray        }
287257726Sray
288257726Sray	/* blank full size */
289257726Sray	len = info->fb_size / 4;
290257726Sray	for (i = 0; i < len; i++) {
291257726Sray		((uint32_t *)info->fb_vbase)[i] = 0;
292257726Sray	}
293257726Sray
294257726Sray	/* Get pixel storage size. */
295257726Sray	info->fb_bpp = info->fb_stride / info->fb_width * 8;
296257726Sray
297257726Sray#ifdef	FDT
298257726Sray	vt_efb_initialize(info, node);
299257726Sray#else
300257726Sray	vt_efb_initialize(info);
301257726Sray#endif
302257726Sray	vt_fb_init(vd);
303257726Sray
304257726Sray	return (CN_INTERNAL);
305257726Sray}
306