1*04bc8e97Sriastradh /* $NetBSD: genfb_sbus.c,v 1.13 2020/09/06 17:22:44 riastradh Exp $ */
29b5f7f01Smacallan
39b5f7f01Smacallan /*-
49b5f7f01Smacallan * Copyright (c) 2007 Michael Lorenz
59b5f7f01Smacallan * All rights reserved.
69b5f7f01Smacallan *
79b5f7f01Smacallan * Redistribution and use in source and binary forms, with or without
89b5f7f01Smacallan * modification, are permitted provided that the following conditions
99b5f7f01Smacallan * are met:
109b5f7f01Smacallan * 1. Redistributions of source code must retain the above copyright
119b5f7f01Smacallan * notice, this list of conditions and the following disclaimer.
129b5f7f01Smacallan * 2. Redistributions in binary form must reproduce the above copyright
139b5f7f01Smacallan * notice, this list of conditions and the following disclaimer in the
149b5f7f01Smacallan * documentation and/or other materials provided with the distribution.
159b5f7f01Smacallan *
169b5f7f01Smacallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
179b5f7f01Smacallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
189b5f7f01Smacallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
199b5f7f01Smacallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
209b5f7f01Smacallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
219b5f7f01Smacallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
229b5f7f01Smacallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
239b5f7f01Smacallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
249b5f7f01Smacallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
259b5f7f01Smacallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
269b5f7f01Smacallan * POSSIBILITY OF SUCH DAMAGE.
279b5f7f01Smacallan */
289b5f7f01Smacallan
299b5f7f01Smacallan /* an SBus frontend for the generic fb console driver */
309b5f7f01Smacallan
319b5f7f01Smacallan #include <sys/cdefs.h>
32*04bc8e97Sriastradh __KERNEL_RCSID(0, "$NetBSD: genfb_sbus.c,v 1.13 2020/09/06 17:22:44 riastradh Exp $");
339b5f7f01Smacallan
349b5f7f01Smacallan #include <sys/param.h>
35*04bc8e97Sriastradh #include <sys/types.h>
36*04bc8e97Sriastradh
379b5f7f01Smacallan #include <sys/buf.h>
38*04bc8e97Sriastradh #include <sys/bus.h>
39*04bc8e97Sriastradh #include <sys/conf.h>
409b5f7f01Smacallan #include <sys/device.h>
419b5f7f01Smacallan #include <sys/ioctl.h>
42*04bc8e97Sriastradh #include <sys/systm.h>
439b5f7f01Smacallan
44*04bc8e97Sriastradh #include <uvm/uvm_extern.h>
45*04bc8e97Sriastradh
469b5f7f01Smacallan #include <machine/autoconf.h>
479b5f7f01Smacallan
489b5f7f01Smacallan #include <dev/sbus/sbusvar.h>
499b5f7f01Smacallan #include <dev/sun/fbio.h>
509b5f7f01Smacallan #include <dev/sun/fbvar.h>
519b5f7f01Smacallan #include <dev/wsfb/genfbvar.h>
529b5f7f01Smacallan
539b5f7f01Smacallan struct genfb_sbus_softc {
549b5f7f01Smacallan struct genfb_softc sc_gen;
559b5f7f01Smacallan bus_space_tag_t sc_tag;
569b5f7f01Smacallan paddr_t sc_paddr;
579b5f7f01Smacallan };
589b5f7f01Smacallan
597cf29912Scegger static int genfb_match_sbus(device_t, cfdata_t, void *);
607cf29912Scegger static void genfb_attach_sbus(device_t, device_t, void *);
619b5f7f01Smacallan static int genfb_ioctl_sbus(void *, void *, u_long, void *, int,
629b5f7f01Smacallan struct lwp*);
639b5f7f01Smacallan static paddr_t genfb_mmap_sbus(void *, void *, off_t, int);
649b5f7f01Smacallan
654fbc897fSmacallan CFATTACH_DECL_NEW(genfb_sbus, sizeof(struct genfb_sbus_softc),
669b5f7f01Smacallan genfb_match_sbus, genfb_attach_sbus, NULL, NULL);
679b5f7f01Smacallan
689b5f7f01Smacallan /*
699b5f7f01Smacallan * Match a graphics device.
709b5f7f01Smacallan */
719b5f7f01Smacallan static int
genfb_match_sbus(device_t parent,cfdata_t cf,void * aux)727cf29912Scegger genfb_match_sbus(device_t parent, cfdata_t cf, void *aux)
739b5f7f01Smacallan {
749b5f7f01Smacallan struct sbus_attach_args *sa = aux;
759b5f7f01Smacallan
769b5f7f01Smacallan /* if there's no address property we can't use the device */
779b5f7f01Smacallan if (prom_getpropint(sa->sa_node, "address", -1) == -1)
789b5f7f01Smacallan return 0;
799b5f7f01Smacallan if (strcmp("display", prom_getpropstring(sa->sa_node, "device_type"))
809b5f7f01Smacallan == 0)
819b5f7f01Smacallan return 1;
829b5f7f01Smacallan
839b5f7f01Smacallan return 0;
849b5f7f01Smacallan }
859b5f7f01Smacallan
869b5f7f01Smacallan /*
879b5f7f01Smacallan * Attach a display. We need to notice if it is the console, too.
889b5f7f01Smacallan */
899b5f7f01Smacallan static void
genfb_attach_sbus(device_t parent,device_t self,void * args)907cf29912Scegger genfb_attach_sbus(device_t parent, device_t self, void *args)
919b5f7f01Smacallan {
92f3504c80Stsutsui struct genfb_sbus_softc *sc = device_private(self);
939b5f7f01Smacallan struct sbus_attach_args *sa = args;
94b41195f8Sriastradh static const struct genfb_ops zero_ops;
95b41195f8Sriastradh struct genfb_ops ops = zero_ops;
969b5f7f01Smacallan prop_dictionary_t dict;
979b5f7f01Smacallan bus_space_handle_t bh;
989b5f7f01Smacallan paddr_t fbpa;
999b5f7f01Smacallan vaddr_t fbva;
1009b5f7f01Smacallan int node = sa->sa_node;
1019b5f7f01Smacallan int isconsole;
1029b5f7f01Smacallan
1039b5f7f01Smacallan aprint_normal("\n");
1044fbc897fSmacallan sc->sc_gen.sc_dev = self;
1059b5f7f01Smacallan /* Remember cookies for genfb_mmap_sbus() */
1069b5f7f01Smacallan sc->sc_tag = sa->sa_bustag;
1079b5f7f01Smacallan sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset);
1089b5f7f01Smacallan
1099b5f7f01Smacallan /* read geometry information from the device tree */
1109b5f7f01Smacallan sc->sc_gen.sc_width = prom_getpropint(sa->sa_node, "width", 1152);
1119b5f7f01Smacallan sc->sc_gen.sc_height = prom_getpropint(sa->sa_node, "height", 900);
1129b5f7f01Smacallan sc->sc_gen.sc_depth = prom_getpropint(sa->sa_node, "depth", 8);
1139b5f7f01Smacallan sc->sc_gen.sc_stride = prom_getpropint(sa->sa_node, "linebytes",
1149b5f7f01Smacallan (sc->sc_gen.sc_width * sc->sc_gen.sc_depth + 7) >> 3 );
11584b4b152Smacallan sc->sc_gen.sc_fbsize = sc->sc_gen.sc_height * sc->sc_gen.sc_stride;
1169b5f7f01Smacallan fbva = (uint32_t)prom_getpropint(sa->sa_node, "address", 0);
1179b5f7f01Smacallan if (fbva == 0)
1189b5f7f01Smacallan panic("this fb has no address property\n");
1194c5fa20dScegger aprint_normal_dev(self, "%d x %d at %d bit\n",
1209b5f7f01Smacallan sc->sc_gen.sc_width, sc->sc_gen.sc_height, sc->sc_gen.sc_depth);
1219b5f7f01Smacallan
1229b5f7f01Smacallan pmap_extract(pmap_kernel(), fbva, &fbpa);
1239b5f7f01Smacallan sc->sc_gen.sc_fboffset = (fbpa & 0x01ffffff) -
1249b5f7f01Smacallan (sc->sc_paddr & 0x01ffffff);
1254c5fa20dScegger aprint_normal_dev(self, "framebuffer at offset 0x%x\n",
1269b5f7f01Smacallan (uint32_t)sc->sc_gen.sc_fboffset);
1279b5f7f01Smacallan
1289b5f7f01Smacallan #if notyet
1299b5f7f01Smacallan if (sc->sc_gen.sc_depth <= 8) {
1309b5f7f01Smacallan /* setup some ANSIish colour map */
1319b5f7f01Smacallan char boo[256];
1329b5f7f01Smacallan snprintf(boo, 256, "\" pal!\" %x %x %x %x %x call",
1339b5f7f01Smacallan sa->sa_node, 0, 0xa0, 0xa0, 0);
1349b5f7f01Smacallan prom_interpret(boo);
1359b5f7f01Smacallan }
1369b5f7f01Smacallan #endif
1379b5f7f01Smacallan
1389b5f7f01Smacallan isconsole = fb_is_console(node);
1399b5f7f01Smacallan dict = device_properties(self);
1409b5f7f01Smacallan prop_dictionary_set_bool(dict, "is_console", isconsole);
1419b5f7f01Smacallan
1429b5f7f01Smacallan if (sbus_bus_map(sa->sa_bustag,
1439b5f7f01Smacallan sa->sa_slot,
1449b5f7f01Smacallan sa->sa_offset + sc->sc_gen.sc_fboffset,
1459b5f7f01Smacallan sc->sc_gen.sc_fbsize,
1469b5f7f01Smacallan BUS_SPACE_MAP_LINEAR, &bh) != 0) {
1474c5fa20dScegger aprint_error_dev(self, "cannot map framebuffer\n");
1489b5f7f01Smacallan return;
1499b5f7f01Smacallan }
1509b5f7f01Smacallan sc->sc_gen.sc_fbaddr = (void *)bus_space_vaddr(sa->sa_bustag, bh);
1519b5f7f01Smacallan
1529b5f7f01Smacallan ops.genfb_ioctl = genfb_ioctl_sbus;
1539b5f7f01Smacallan ops.genfb_mmap = genfb_mmap_sbus;
1549b5f7f01Smacallan
1559b5f7f01Smacallan genfb_attach(&sc->sc_gen, &ops);
1569b5f7f01Smacallan }
1579b5f7f01Smacallan
1589b5f7f01Smacallan static int
genfb_ioctl_sbus(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)1599b5f7f01Smacallan genfb_ioctl_sbus(void *v, void *vs, u_long cmd, void *data, int flag,
1609b5f7f01Smacallan struct lwp *l)
1619b5f7f01Smacallan {
1629b5f7f01Smacallan
1639b5f7f01Smacallan switch (cmd) {
1649b5f7f01Smacallan case WSDISPLAYIO_GTYPE:
1659b5f7f01Smacallan *(u_int *)data = WSDISPLAY_TYPE_GENFB;
1669b5f7f01Smacallan return 0;
1679b5f7f01Smacallan }
1689b5f7f01Smacallan
1699b5f7f01Smacallan return EPASSTHROUGH;
1709b5f7f01Smacallan }
1719b5f7f01Smacallan
1729b5f7f01Smacallan static paddr_t
genfb_mmap_sbus(void * v,void * vs,off_t offset,int prot)1739b5f7f01Smacallan genfb_mmap_sbus(void *v, void *vs, off_t offset, int prot)
1749b5f7f01Smacallan {
1759b5f7f01Smacallan struct genfb_sbus_softc *sc = v;
1769b5f7f01Smacallan
1779b5f7f01Smacallan /* regular fb mapping at 0 */
1789b5f7f01Smacallan if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize)) {
1799b5f7f01Smacallan
1809b5f7f01Smacallan return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
1819b5f7f01Smacallan sc->sc_gen.sc_fboffset + offset, prot,
1829b5f7f01Smacallan BUS_SPACE_MAP_LINEAR);
1839b5f7f01Smacallan }
1849b5f7f01Smacallan
1859b5f7f01Smacallan return -1;
1869b5f7f01Smacallan }
187