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