xref: /netbsd-src/sys/arch/arm/sunxi/sunxi_fb.c (revision 9a06f9833c40e8cc26ed9722cb01712cbaac38a5)
1*9a06f983Sriastradh /* $NetBSD: sunxi_fb.c,v 1.8 2022/09/25 07:50:23 riastradh Exp $ */
2a9d03646Sjmcneill 
3a9d03646Sjmcneill /*-
4a9d03646Sjmcneill  * Copyright (c) 2015-2019 Jared McNeill <jmcneill@invisible.ca>
5a9d03646Sjmcneill  * All rights reserved.
6a9d03646Sjmcneill  *
7a9d03646Sjmcneill  * Redistribution and use in source and binary forms, with or without
8a9d03646Sjmcneill  * modification, are permitted provided that the following conditions
9a9d03646Sjmcneill  * are met:
10a9d03646Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11a9d03646Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12a9d03646Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13a9d03646Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14a9d03646Sjmcneill  *    documentation and/or other materials provided with the distribution.
15a9d03646Sjmcneill  *
16a9d03646Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17a9d03646Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18a9d03646Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19a9d03646Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20a9d03646Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21a9d03646Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22a9d03646Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23a9d03646Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24a9d03646Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a9d03646Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a9d03646Sjmcneill  * SUCH DAMAGE.
27a9d03646Sjmcneill  */
28a9d03646Sjmcneill 
29a9d03646Sjmcneill #include "opt_wsdisplay_compat.h"
30a9d03646Sjmcneill 
31a9d03646Sjmcneill #include <sys/cdefs.h>
32*9a06f983Sriastradh __KERNEL_RCSID(0, "$NetBSD: sunxi_fb.c,v 1.8 2022/09/25 07:50:23 riastradh Exp $");
33a9d03646Sjmcneill 
34a9d03646Sjmcneill #include <sys/param.h>
35a9d03646Sjmcneill #include <sys/bus.h>
36a9d03646Sjmcneill #include <sys/device.h>
37a9d03646Sjmcneill 
38a9d03646Sjmcneill #include <dev/fdt/fdtvar.h>
39a9d03646Sjmcneill 
40dd47db3eSriastradh #include <arm/sunxi/sunxi_drm.h>
41dd47db3eSriastradh 
423973e774Sriastradh #include <drm/drm_drv.h>
43a9d03646Sjmcneill #include <drm/drmfb.h>
44a9d03646Sjmcneill 
45a9d03646Sjmcneill static int	sunxi_fb_match(device_t, cfdata_t, void *);
46a9d03646Sjmcneill static void	sunxi_fb_attach(device_t, device_t, void *);
47a9d03646Sjmcneill 
48607772e0Sriastradh static void	sunxi_fb_init(struct sunxi_drm_task *);
49dd7f7779Sriastradh 
50a9d03646Sjmcneill static bool	sunxi_fb_shutdown(device_t, int);
51a9d03646Sjmcneill 
52a9d03646Sjmcneill struct sunxi_fb_softc {
53a9d03646Sjmcneill 	struct drmfb_softc	sc_drmfb;
54a9d03646Sjmcneill 	device_t		sc_dev;
55a9d03646Sjmcneill 	struct sunxi_drm_softc	*sc_drm;
56a9d03646Sjmcneill 	struct sunxi_drm_framebuffer *sc_fb;
57a9d03646Sjmcneill 	struct sunxi_drmfb_attach_args sc_sfa;
58607772e0Sriastradh 	struct sunxi_drm_task	sc_attach_task;
59a9d03646Sjmcneill };
60a9d03646Sjmcneill 
61a9d03646Sjmcneill static paddr_t	sunxi_fb_mmapfb(struct drmfb_softc *, off_t, int);
62a9d03646Sjmcneill static int	sunxi_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
63a9d03646Sjmcneill 			       lwp_t *);
64a9d03646Sjmcneill 
65a9d03646Sjmcneill static const struct drmfb_params sunxifb_drmfb_params = {
66a9d03646Sjmcneill 	.dp_mmapfb = sunxi_fb_mmapfb,
67a9d03646Sjmcneill 	.dp_ioctl = sunxi_fb_ioctl,
68a9d03646Sjmcneill 
69a9d03646Sjmcneill };
70a9d03646Sjmcneill 
71a9d03646Sjmcneill CFATTACH_DECL_NEW(sunxi_fb, sizeof(struct sunxi_fb_softc),
72a9d03646Sjmcneill 	sunxi_fb_match, sunxi_fb_attach, NULL, NULL);
73a9d03646Sjmcneill 
74a9d03646Sjmcneill static int
sunxi_fb_match(device_t parent,cfdata_t cf,void * aux)75a9d03646Sjmcneill sunxi_fb_match(device_t parent, cfdata_t cf, void *aux)
76a9d03646Sjmcneill {
77a9d03646Sjmcneill 	return 1;
78a9d03646Sjmcneill }
79a9d03646Sjmcneill 
80a9d03646Sjmcneill static void
sunxi_fb_attach(device_t parent,device_t self,void * aux)81a9d03646Sjmcneill sunxi_fb_attach(device_t parent, device_t self, void *aux)
82a9d03646Sjmcneill {
83a9d03646Sjmcneill 	struct sunxi_fb_softc * const sc = device_private(self);
84a9d03646Sjmcneill 	struct sunxi_drm_softc * const drmsc = device_private(parent);
85a9d03646Sjmcneill 	struct sunxi_drmfb_attach_args * const sfa = aux;
86a9d03646Sjmcneill 
87a9d03646Sjmcneill 	sc->sc_dev = self;
88a9d03646Sjmcneill 	sc->sc_drm = drmsc;
89a9d03646Sjmcneill 	sc->sc_sfa = *sfa;
90a9d03646Sjmcneill 	sc->sc_fb = to_sunxi_drm_framebuffer(sfa->sfa_fb_helper->fb);
91a9d03646Sjmcneill 
92a9d03646Sjmcneill 	aprint_naive("\n");
93a9d03646Sjmcneill 	aprint_normal("\n");
94a9d03646Sjmcneill 
95607772e0Sriastradh 	sunxi_task_init(&sc->sc_attach_task, &sunxi_fb_init);
96607772e0Sriastradh 	sunxi_task_schedule(parent, &sc->sc_attach_task);
97dd7f7779Sriastradh }
98dd7f7779Sriastradh 
99dd7f7779Sriastradh static void
sunxi_fb_init(struct sunxi_drm_task * task)100607772e0Sriastradh sunxi_fb_init(struct sunxi_drm_task *task)
101dd7f7779Sriastradh {
102607772e0Sriastradh 	struct sunxi_fb_softc * const sc =
103607772e0Sriastradh 	    container_of(task, struct sunxi_fb_softc, sc_attach_task);
104607772e0Sriastradh 	device_t dev = sc->sc_dev;
105dd7f7779Sriastradh 	struct sunxi_drmfb_attach_args * const sfa = &sc->sc_sfa;
106dd7f7779Sriastradh 	int error;
107a9d03646Sjmcneill 
108a9d03646Sjmcneill 	const struct drmfb_attach_args da = {
109dd7f7779Sriastradh 		.da_dev = dev,
110a9d03646Sjmcneill 		.da_fb_helper = sfa->sfa_fb_helper,
111a9d03646Sjmcneill 		.da_fb_sizes = &sfa->sfa_fb_sizes,
112a9d03646Sjmcneill 		.da_fb_vaddr = sc->sc_fb->obj->vaddr,
113a9d03646Sjmcneill 		.da_fb_linebytes = sfa->sfa_fb_linebytes,
114a9d03646Sjmcneill 		.da_params = &sunxifb_drmfb_params,
115a9d03646Sjmcneill 	};
116a9d03646Sjmcneill 
117dd7f7779Sriastradh 
118a9d03646Sjmcneill 	error = drmfb_attach(&sc->sc_drmfb, &da);
119a9d03646Sjmcneill 	if (error) {
120dd7f7779Sriastradh 		aprint_error_dev(dev, "failed to attach drmfb: %d\n", error);
121a9d03646Sjmcneill 		return;
122a9d03646Sjmcneill 	}
123a9d03646Sjmcneill 
124dd7f7779Sriastradh 	pmf_device_register1(dev, NULL, NULL, sunxi_fb_shutdown);
125a9d03646Sjmcneill }
126a9d03646Sjmcneill 
127a9d03646Sjmcneill static bool
sunxi_fb_shutdown(device_t self,int flags)128a9d03646Sjmcneill sunxi_fb_shutdown(device_t self, int flags)
129a9d03646Sjmcneill {
130a9d03646Sjmcneill 	struct sunxi_fb_softc * const sc = device_private(self);
131a9d03646Sjmcneill 
132a9d03646Sjmcneill 	return drmfb_shutdown(&sc->sc_drmfb, flags);
133a9d03646Sjmcneill }
134a9d03646Sjmcneill 
135a9d03646Sjmcneill static paddr_t
sunxi_fb_mmapfb(struct drmfb_softc * sc,off_t off,int prot)136a9d03646Sjmcneill sunxi_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
137a9d03646Sjmcneill {
138a9d03646Sjmcneill 	struct sunxi_fb_softc * const tfb_sc = (struct sunxi_fb_softc *)sc;
139a9d03646Sjmcneill 	struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
140a9d03646Sjmcneill 
141a9d03646Sjmcneill 	KASSERT(off >= 0);
142a9d03646Sjmcneill 	KASSERT(off < obj->dmasize);
143a9d03646Sjmcneill 
144a9d03646Sjmcneill 	return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
145a9d03646Sjmcneill 	    BUS_DMA_PREFETCHABLE);
146a9d03646Sjmcneill }
147a9d03646Sjmcneill 
148a9d03646Sjmcneill static int
sunxi_fb_ioctl(struct drmfb_softc * sc,u_long cmd,void * data,int flag,lwp_t * l)149a9d03646Sjmcneill sunxi_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
150a9d03646Sjmcneill     lwp_t *l)
151a9d03646Sjmcneill {
152a9d03646Sjmcneill 	struct wsdisplayio_bus_id *busid;
153a9d03646Sjmcneill 	struct wsdisplayio_fbinfo *fbi;
154a9d03646Sjmcneill 	struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
155a9d03646Sjmcneill 	int error;
156a9d03646Sjmcneill 
157a9d03646Sjmcneill 	switch (cmd) {
158a9d03646Sjmcneill 	case WSDISPLAYIO_GET_BUSID:
159a9d03646Sjmcneill 		busid = data;
160a9d03646Sjmcneill 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
161a9d03646Sjmcneill 		return 0;
162a9d03646Sjmcneill 	case WSDISPLAYIO_GTYPE:
163a9d03646Sjmcneill 		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
164a9d03646Sjmcneill 		return 0;
165a9d03646Sjmcneill 	case WSDISPLAYIO_GET_FBINFO:
166a9d03646Sjmcneill 		fbi = data;
167a9d03646Sjmcneill 		error = wsdisplayio_get_fbinfo(ri, fbi);
168a9d03646Sjmcneill 		fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
169a9d03646Sjmcneill 		return error;
170a9d03646Sjmcneill 	default:
171a9d03646Sjmcneill 		return EPASSTHROUGH;
172a9d03646Sjmcneill 	}
173a9d03646Sjmcneill }
174