sunxi_fb.c revision 1.1
11.1Sjmcneill/* $NetBSD: sunxi_fb.c,v 1.1 2019/01/30 01:24:00 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2015-2019 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include "opt_wsdisplay_compat.h"
301.1Sjmcneill
311.1Sjmcneill#include <sys/cdefs.h>
321.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: sunxi_fb.c,v 1.1 2019/01/30 01:24:00 jmcneill Exp $");
331.1Sjmcneill
341.1Sjmcneill#include <sys/param.h>
351.1Sjmcneill#include <sys/bus.h>
361.1Sjmcneill#include <sys/device.h>
371.1Sjmcneill
381.1Sjmcneill#include <dev/fdt/fdtvar.h>
391.1Sjmcneill
401.1Sjmcneill#include <drm/drmP.h>
411.1Sjmcneill#include <drm/drmfb.h>
421.1Sjmcneill
431.1Sjmcneill#include <arm/sunxi/sunxi_drm.h>
441.1Sjmcneill
451.1Sjmcneillstatic int	sunxi_fb_match(device_t, cfdata_t, void *);
461.1Sjmcneillstatic void	sunxi_fb_attach(device_t, device_t, void *);
471.1Sjmcneill
481.1Sjmcneillstatic bool	sunxi_fb_shutdown(device_t, int);
491.1Sjmcneill
501.1Sjmcneillstruct sunxi_fb_softc {
511.1Sjmcneill	struct drmfb_softc	sc_drmfb;
521.1Sjmcneill	device_t		sc_dev;
531.1Sjmcneill	struct sunxi_drm_softc	*sc_drm;
541.1Sjmcneill	struct sunxi_drm_framebuffer *sc_fb;
551.1Sjmcneill	struct sunxi_drmfb_attach_args sc_sfa;
561.1Sjmcneill};
571.1Sjmcneill
581.1Sjmcneillstatic paddr_t	sunxi_fb_mmapfb(struct drmfb_softc *, off_t, int);
591.1Sjmcneillstatic int	sunxi_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
601.1Sjmcneill			       lwp_t *);
611.1Sjmcneill
621.1Sjmcneillstatic const struct drmfb_params sunxifb_drmfb_params = {
631.1Sjmcneill	.dp_mmapfb = sunxi_fb_mmapfb,
641.1Sjmcneill	.dp_ioctl = sunxi_fb_ioctl,
651.1Sjmcneill
661.1Sjmcneill};
671.1Sjmcneill
681.1SjmcneillCFATTACH_DECL_NEW(sunxi_fb, sizeof(struct sunxi_fb_softc),
691.1Sjmcneill	sunxi_fb_match, sunxi_fb_attach, NULL, NULL);
701.1Sjmcneill
711.1Sjmcneillstatic int
721.1Sjmcneillsunxi_fb_match(device_t parent, cfdata_t cf, void *aux)
731.1Sjmcneill{
741.1Sjmcneill	return 1;
751.1Sjmcneill}
761.1Sjmcneill
771.1Sjmcneillstatic void
781.1Sjmcneillsunxi_fb_attach(device_t parent, device_t self, void *aux)
791.1Sjmcneill{
801.1Sjmcneill	struct sunxi_fb_softc * const sc = device_private(self);
811.1Sjmcneill	struct sunxi_drm_softc * const drmsc = device_private(parent);
821.1Sjmcneill	struct sunxi_drmfb_attach_args * const sfa = aux;
831.1Sjmcneill	int error;
841.1Sjmcneill
851.1Sjmcneill	sc->sc_dev = self;
861.1Sjmcneill	sc->sc_drm = drmsc;
871.1Sjmcneill	sc->sc_sfa = *sfa;
881.1Sjmcneill	sc->sc_fb = to_sunxi_drm_framebuffer(sfa->sfa_fb_helper->fb);
891.1Sjmcneill
901.1Sjmcneill	aprint_naive("\n");
911.1Sjmcneill	aprint_normal("\n");
921.1Sjmcneill
931.1Sjmcneill#ifdef WSDISPLAY_MULTICONS
941.1Sjmcneill	prop_dictionary_t dict = device_properties(self);
951.1Sjmcneill	const bool is_console = true;
961.1Sjmcneill	prop_dictionary_set_bool(dict, "is_console", is_console);
971.1Sjmcneill#endif
981.1Sjmcneill
991.1Sjmcneill	const struct drmfb_attach_args da = {
1001.1Sjmcneill		.da_dev = self,
1011.1Sjmcneill		.da_fb_helper = sfa->sfa_fb_helper,
1021.1Sjmcneill		.da_fb_sizes = &sfa->sfa_fb_sizes,
1031.1Sjmcneill		.da_fb_vaddr = sc->sc_fb->obj->vaddr,
1041.1Sjmcneill		.da_fb_linebytes = sfa->sfa_fb_linebytes,
1051.1Sjmcneill		.da_params = &sunxifb_drmfb_params,
1061.1Sjmcneill	};
1071.1Sjmcneill
1081.1Sjmcneill	error = drmfb_attach(&sc->sc_drmfb, &da);
1091.1Sjmcneill	if (error) {
1101.1Sjmcneill		aprint_error_dev(self, "failed to attach drmfb: %d\n", error);
1111.1Sjmcneill		return;
1121.1Sjmcneill	}
1131.1Sjmcneill
1141.1Sjmcneill	pmf_device_register1(self, NULL, NULL, sunxi_fb_shutdown);
1151.1Sjmcneill}
1161.1Sjmcneill
1171.1Sjmcneillstatic bool
1181.1Sjmcneillsunxi_fb_shutdown(device_t self, int flags)
1191.1Sjmcneill{
1201.1Sjmcneill	struct sunxi_fb_softc * const sc = device_private(self);
1211.1Sjmcneill
1221.1Sjmcneill	return drmfb_shutdown(&sc->sc_drmfb, flags);
1231.1Sjmcneill}
1241.1Sjmcneill
1251.1Sjmcneillstatic paddr_t
1261.1Sjmcneillsunxi_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
1271.1Sjmcneill{
1281.1Sjmcneill	struct sunxi_fb_softc * const tfb_sc = (struct sunxi_fb_softc *)sc;
1291.1Sjmcneill	struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
1301.1Sjmcneill
1311.1Sjmcneill	KASSERT(off >= 0);
1321.1Sjmcneill	KASSERT(off < obj->dmasize);
1331.1Sjmcneill
1341.1Sjmcneill	return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
1351.1Sjmcneill	    BUS_DMA_PREFETCHABLE);
1361.1Sjmcneill}
1371.1Sjmcneill
1381.1Sjmcneillstatic int
1391.1Sjmcneillsunxi_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
1401.1Sjmcneill    lwp_t *l)
1411.1Sjmcneill{
1421.1Sjmcneill	struct wsdisplayio_bus_id *busid;
1431.1Sjmcneill	struct wsdisplayio_fbinfo *fbi;
1441.1Sjmcneill	struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
1451.1Sjmcneill	int error;
1461.1Sjmcneill
1471.1Sjmcneill	switch (cmd) {
1481.1Sjmcneill	case WSDISPLAYIO_GET_BUSID:
1491.1Sjmcneill		busid = data;
1501.1Sjmcneill		busid->bus_type = WSDISPLAYIO_BUS_SOC;
1511.1Sjmcneill		return 0;
1521.1Sjmcneill	case WSDISPLAYIO_GTYPE:
1531.1Sjmcneill		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
1541.1Sjmcneill		return 0;
1551.1Sjmcneill	case WSDISPLAYIO_GET_FBINFO:
1561.1Sjmcneill		fbi = data;
1571.1Sjmcneill		error = wsdisplayio_get_fbinfo(ri, fbi);
1581.1Sjmcneill		fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
1591.1Sjmcneill		return error;
1601.1Sjmcneill	default:
1611.1Sjmcneill		return EPASSTHROUGH;
1621.1Sjmcneill	}
1631.1Sjmcneill}
164