sunxi_fb.c revision 1.6
11.6Sriastrad/* $NetBSD: sunxi_fb.c,v 1.6 2021/12/19 11:25:25 riastradh 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.6Sriastrad__KERNEL_RCSID(0, "$NetBSD: sunxi_fb.c,v 1.6 2021/12/19 11:25:25 riastradh 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.5Sriastrad#include <arm/sunxi/sunxi_drm.h>
411.5Sriastrad
421.4Sriastrad#include <drm/drm_drv.h>
431.1Sjmcneill#include <drm/drmfb.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.6Sriastradstatic void	sunxi_fb_init(device_t);
491.6Sriastrad
501.1Sjmcneillstatic bool	sunxi_fb_shutdown(device_t, int);
511.1Sjmcneill
521.1Sjmcneillstruct sunxi_fb_softc {
531.1Sjmcneill	struct drmfb_softc	sc_drmfb;
541.1Sjmcneill	device_t		sc_dev;
551.1Sjmcneill	struct sunxi_drm_softc	*sc_drm;
561.1Sjmcneill	struct sunxi_drm_framebuffer *sc_fb;
571.1Sjmcneill	struct sunxi_drmfb_attach_args sc_sfa;
581.1Sjmcneill};
591.1Sjmcneill
601.1Sjmcneillstatic paddr_t	sunxi_fb_mmapfb(struct drmfb_softc *, off_t, int);
611.1Sjmcneillstatic int	sunxi_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
621.1Sjmcneill			       lwp_t *);
631.1Sjmcneill
641.1Sjmcneillstatic const struct drmfb_params sunxifb_drmfb_params = {
651.1Sjmcneill	.dp_mmapfb = sunxi_fb_mmapfb,
661.1Sjmcneill	.dp_ioctl = sunxi_fb_ioctl,
671.1Sjmcneill
681.1Sjmcneill};
691.1Sjmcneill
701.1SjmcneillCFATTACH_DECL_NEW(sunxi_fb, sizeof(struct sunxi_fb_softc),
711.1Sjmcneill	sunxi_fb_match, sunxi_fb_attach, NULL, NULL);
721.1Sjmcneill
731.1Sjmcneillstatic int
741.1Sjmcneillsunxi_fb_match(device_t parent, cfdata_t cf, void *aux)
751.1Sjmcneill{
761.1Sjmcneill	return 1;
771.1Sjmcneill}
781.1Sjmcneill
791.1Sjmcneillstatic void
801.1Sjmcneillsunxi_fb_attach(device_t parent, device_t self, void *aux)
811.1Sjmcneill{
821.1Sjmcneill	struct sunxi_fb_softc * const sc = device_private(self);
831.1Sjmcneill	struct sunxi_drm_softc * const drmsc = device_private(parent);
841.1Sjmcneill	struct sunxi_drmfb_attach_args * const sfa = aux;
851.1Sjmcneill
861.1Sjmcneill	sc->sc_dev = self;
871.1Sjmcneill	sc->sc_drm = drmsc;
881.1Sjmcneill	sc->sc_sfa = *sfa;
891.1Sjmcneill	sc->sc_fb = to_sunxi_drm_framebuffer(sfa->sfa_fb_helper->fb);
901.1Sjmcneill
911.1Sjmcneill	aprint_naive("\n");
921.1Sjmcneill	aprint_normal("\n");
931.1Sjmcneill
941.1Sjmcneill#ifdef WSDISPLAY_MULTICONS
951.1Sjmcneill	prop_dictionary_t dict = device_properties(self);
961.1Sjmcneill	const bool is_console = true;
971.1Sjmcneill	prop_dictionary_set_bool(dict, "is_console", is_console);
981.1Sjmcneill#endif
991.6Sriastrad	config_defer(self, sunxi_fb_init);
1001.6Sriastrad}
1011.6Sriastrad
1021.6Sriastradstatic void
1031.6Sriastradsunxi_fb_init(device_t dev)
1041.6Sriastrad{
1051.6Sriastrad	struct sunxi_fb_softc * const sc = device_private(dev);
1061.6Sriastrad	struct sunxi_drmfb_attach_args * const sfa = &sc->sc_sfa;
1071.6Sriastrad	int error;
1081.1Sjmcneill
1091.1Sjmcneill	const struct drmfb_attach_args da = {
1101.6Sriastrad		.da_dev = dev,
1111.1Sjmcneill		.da_fb_helper = sfa->sfa_fb_helper,
1121.1Sjmcneill		.da_fb_sizes = &sfa->sfa_fb_sizes,
1131.1Sjmcneill		.da_fb_vaddr = sc->sc_fb->obj->vaddr,
1141.1Sjmcneill		.da_fb_linebytes = sfa->sfa_fb_linebytes,
1151.1Sjmcneill		.da_params = &sunxifb_drmfb_params,
1161.1Sjmcneill	};
1171.1Sjmcneill
1181.6Sriastrad
1191.1Sjmcneill	error = drmfb_attach(&sc->sc_drmfb, &da);
1201.1Sjmcneill	if (error) {
1211.6Sriastrad		aprint_error_dev(dev, "failed to attach drmfb: %d\n", error);
1221.1Sjmcneill		return;
1231.1Sjmcneill	}
1241.1Sjmcneill
1251.6Sriastrad	pmf_device_register1(dev, NULL, NULL, sunxi_fb_shutdown);
1261.1Sjmcneill}
1271.1Sjmcneill
1281.1Sjmcneillstatic bool
1291.1Sjmcneillsunxi_fb_shutdown(device_t self, int flags)
1301.1Sjmcneill{
1311.1Sjmcneill	struct sunxi_fb_softc * const sc = device_private(self);
1321.1Sjmcneill
1331.1Sjmcneill	return drmfb_shutdown(&sc->sc_drmfb, flags);
1341.1Sjmcneill}
1351.1Sjmcneill
1361.1Sjmcneillstatic paddr_t
1371.1Sjmcneillsunxi_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
1381.1Sjmcneill{
1391.1Sjmcneill	struct sunxi_fb_softc * const tfb_sc = (struct sunxi_fb_softc *)sc;
1401.1Sjmcneill	struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
1411.1Sjmcneill
1421.1Sjmcneill	KASSERT(off >= 0);
1431.1Sjmcneill	KASSERT(off < obj->dmasize);
1441.1Sjmcneill
1451.1Sjmcneill	return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
1461.1Sjmcneill	    BUS_DMA_PREFETCHABLE);
1471.1Sjmcneill}
1481.1Sjmcneill
1491.1Sjmcneillstatic int
1501.1Sjmcneillsunxi_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
1511.1Sjmcneill    lwp_t *l)
1521.1Sjmcneill{
1531.1Sjmcneill	struct wsdisplayio_bus_id *busid;
1541.1Sjmcneill	struct wsdisplayio_fbinfo *fbi;
1551.1Sjmcneill	struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
1561.1Sjmcneill	int error;
1571.1Sjmcneill
1581.1Sjmcneill	switch (cmd) {
1591.1Sjmcneill	case WSDISPLAYIO_GET_BUSID:
1601.1Sjmcneill		busid = data;
1611.1Sjmcneill		busid->bus_type = WSDISPLAYIO_BUS_SOC;
1621.1Sjmcneill		return 0;
1631.1Sjmcneill	case WSDISPLAYIO_GTYPE:
1641.1Sjmcneill		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
1651.1Sjmcneill		return 0;
1661.1Sjmcneill	case WSDISPLAYIO_GET_FBINFO:
1671.1Sjmcneill		fbi = data;
1681.1Sjmcneill		error = wsdisplayio_get_fbinfo(ri, fbi);
1691.1Sjmcneill		fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
1701.1Sjmcneill		return error;
1711.1Sjmcneill	default:
1721.1Sjmcneill		return EPASSTHROUGH;
1731.1Sjmcneill	}
1741.1Sjmcneill}
175