sunxi_fb.c revision 1.8
11.8Sriastrad/* $NetBSD: sunxi_fb.c,v 1.8 2022/09/25 07:50:23 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.8Sriastrad__KERNEL_RCSID(0, "$NetBSD: sunxi_fb.c,v 1.8 2022/09/25 07:50:23 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.7Sriastradstatic void	sunxi_fb_init(struct sunxi_drm_task *);
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.7Sriastrad	struct sunxi_drm_task	sc_attach_task;
591.1Sjmcneill};
601.1Sjmcneill
611.1Sjmcneillstatic paddr_t	sunxi_fb_mmapfb(struct drmfb_softc *, off_t, int);
621.1Sjmcneillstatic int	sunxi_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
631.1Sjmcneill			       lwp_t *);
641.1Sjmcneill
651.1Sjmcneillstatic const struct drmfb_params sunxifb_drmfb_params = {
661.1Sjmcneill	.dp_mmapfb = sunxi_fb_mmapfb,
671.1Sjmcneill	.dp_ioctl = sunxi_fb_ioctl,
681.1Sjmcneill
691.1Sjmcneill};
701.1Sjmcneill
711.1SjmcneillCFATTACH_DECL_NEW(sunxi_fb, sizeof(struct sunxi_fb_softc),
721.1Sjmcneill	sunxi_fb_match, sunxi_fb_attach, NULL, NULL);
731.1Sjmcneill
741.1Sjmcneillstatic int
751.1Sjmcneillsunxi_fb_match(device_t parent, cfdata_t cf, void *aux)
761.1Sjmcneill{
771.1Sjmcneill	return 1;
781.1Sjmcneill}
791.1Sjmcneill
801.1Sjmcneillstatic void
811.1Sjmcneillsunxi_fb_attach(device_t parent, device_t self, void *aux)
821.1Sjmcneill{
831.1Sjmcneill	struct sunxi_fb_softc * const sc = device_private(self);
841.1Sjmcneill	struct sunxi_drm_softc * const drmsc = device_private(parent);
851.1Sjmcneill	struct sunxi_drmfb_attach_args * const sfa = aux;
861.1Sjmcneill
871.1Sjmcneill	sc->sc_dev = self;
881.1Sjmcneill	sc->sc_drm = drmsc;
891.1Sjmcneill	sc->sc_sfa = *sfa;
901.1Sjmcneill	sc->sc_fb = to_sunxi_drm_framebuffer(sfa->sfa_fb_helper->fb);
911.1Sjmcneill
921.1Sjmcneill	aprint_naive("\n");
931.1Sjmcneill	aprint_normal("\n");
941.1Sjmcneill
951.7Sriastrad	sunxi_task_init(&sc->sc_attach_task, &sunxi_fb_init);
961.7Sriastrad	sunxi_task_schedule(parent, &sc->sc_attach_task);
971.6Sriastrad}
981.6Sriastrad
991.6Sriastradstatic void
1001.7Sriastradsunxi_fb_init(struct sunxi_drm_task *task)
1011.6Sriastrad{
1021.7Sriastrad	struct sunxi_fb_softc * const sc =
1031.7Sriastrad	    container_of(task, struct sunxi_fb_softc, sc_attach_task);
1041.7Sriastrad	device_t dev = sc->sc_dev;
1051.6Sriastrad	struct sunxi_drmfb_attach_args * const sfa = &sc->sc_sfa;
1061.6Sriastrad	int error;
1071.1Sjmcneill
1081.1Sjmcneill	const struct drmfb_attach_args da = {
1091.6Sriastrad		.da_dev = dev,
1101.1Sjmcneill		.da_fb_helper = sfa->sfa_fb_helper,
1111.1Sjmcneill		.da_fb_sizes = &sfa->sfa_fb_sizes,
1121.1Sjmcneill		.da_fb_vaddr = sc->sc_fb->obj->vaddr,
1131.1Sjmcneill		.da_fb_linebytes = sfa->sfa_fb_linebytes,
1141.1Sjmcneill		.da_params = &sunxifb_drmfb_params,
1151.1Sjmcneill	};
1161.1Sjmcneill
1171.6Sriastrad
1181.1Sjmcneill	error = drmfb_attach(&sc->sc_drmfb, &da);
1191.1Sjmcneill	if (error) {
1201.6Sriastrad		aprint_error_dev(dev, "failed to attach drmfb: %d\n", error);
1211.1Sjmcneill		return;
1221.1Sjmcneill	}
1231.1Sjmcneill
1241.6Sriastrad	pmf_device_register1(dev, NULL, NULL, sunxi_fb_shutdown);
1251.1Sjmcneill}
1261.1Sjmcneill
1271.1Sjmcneillstatic bool
1281.1Sjmcneillsunxi_fb_shutdown(device_t self, int flags)
1291.1Sjmcneill{
1301.1Sjmcneill	struct sunxi_fb_softc * const sc = device_private(self);
1311.1Sjmcneill
1321.1Sjmcneill	return drmfb_shutdown(&sc->sc_drmfb, flags);
1331.1Sjmcneill}
1341.1Sjmcneill
1351.1Sjmcneillstatic paddr_t
1361.1Sjmcneillsunxi_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
1371.1Sjmcneill{
1381.1Sjmcneill	struct sunxi_fb_softc * const tfb_sc = (struct sunxi_fb_softc *)sc;
1391.1Sjmcneill	struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
1401.1Sjmcneill
1411.1Sjmcneill	KASSERT(off >= 0);
1421.1Sjmcneill	KASSERT(off < obj->dmasize);
1431.1Sjmcneill
1441.1Sjmcneill	return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
1451.1Sjmcneill	    BUS_DMA_PREFETCHABLE);
1461.1Sjmcneill}
1471.1Sjmcneill
1481.1Sjmcneillstatic int
1491.1Sjmcneillsunxi_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
1501.1Sjmcneill    lwp_t *l)
1511.1Sjmcneill{
1521.1Sjmcneill	struct wsdisplayio_bus_id *busid;
1531.1Sjmcneill	struct wsdisplayio_fbinfo *fbi;
1541.1Sjmcneill	struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
1551.1Sjmcneill	int error;
1561.1Sjmcneill
1571.1Sjmcneill	switch (cmd) {
1581.1Sjmcneill	case WSDISPLAYIO_GET_BUSID:
1591.1Sjmcneill		busid = data;
1601.1Sjmcneill		busid->bus_type = WSDISPLAYIO_BUS_SOC;
1611.1Sjmcneill		return 0;
1621.1Sjmcneill	case WSDISPLAYIO_GTYPE:
1631.1Sjmcneill		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
1641.1Sjmcneill		return 0;
1651.1Sjmcneill	case WSDISPLAYIO_GET_FBINFO:
1661.1Sjmcneill		fbi = data;
1671.1Sjmcneill		error = wsdisplayio_get_fbinfo(ri, fbi);
1681.1Sjmcneill		fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
1691.1Sjmcneill		return error;
1701.1Sjmcneill	default:
1711.1Sjmcneill		return EPASSTHROUGH;
1721.1Sjmcneill	}
1731.1Sjmcneill}
174