xref: /netbsd-src/sys/arch/mac68k/dev/grf_compat.c (revision d47bcd296c8b39243dd81e9cc75ea86330d4eeaf)
1*d47bcd29Schs /*	$NetBSD: grf_compat.c,v 1.29 2019/11/10 21:16:29 chs Exp $	*/
28a007407Sscottr 
38a007407Sscottr /*
48a007407Sscottr  * Copyright (C) 1999 Scott Reynolds
58a007407Sscottr  * All rights reserved.
68a007407Sscottr  *
78a007407Sscottr  * Redistribution and use in source and binary forms, with or without
88a007407Sscottr  * modification, are permitted provided that the following conditions
98a007407Sscottr  * are met:
108a007407Sscottr  * 1. Redistributions of source code must retain the above copyright
118a007407Sscottr  *    notice, this list of conditions and the following disclaimer.
128a007407Sscottr  * 2. Redistributions in binary form must reproduce the above copyright
138a007407Sscottr  *    notice, this list of conditions and the following disclaimer in the
148a007407Sscottr  *    documentation and/or other materials provided with the distribution.
158a007407Sscottr  * 3. The name of the author may not be used to endorse or promote products
168a007407Sscottr  *    derived from this software without specific prior written permission.
178a007407Sscottr  *
188a007407Sscottr  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198a007407Sscottr  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208a007407Sscottr  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218a007407Sscottr  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228a007407Sscottr  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238a007407Sscottr  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248a007407Sscottr  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258a007407Sscottr  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268a007407Sscottr  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278a007407Sscottr  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288a007407Sscottr  */
298a007407Sscottr 
308a007407Sscottr /*
318a007407Sscottr  * macfb compatibility with legacy grf devices
328a007407Sscottr  */
338a007407Sscottr 
3499a1ab2bSmrg #include "opt_grf_compat.h"
3599a1ab2bSmrg 
364b2744bfSlukem #include <sys/cdefs.h>
37*d47bcd29Schs __KERNEL_RCSID(0, "$NetBSD: grf_compat.c,v 1.29 2019/11/10 21:16:29 chs Exp $");
384b2744bfSlukem 
398a007407Sscottr #include <sys/param.h>
408a007407Sscottr #include <sys/systm.h>
418a007407Sscottr #include <sys/conf.h>
428a007407Sscottr #include <sys/device.h>
438a007407Sscottr #include <sys/errno.h>
448a007407Sscottr #include <sys/ioctl.h>
458a007407Sscottr #include <sys/malloc.h>
468a007407Sscottr #include <sys/proc.h>
478a007407Sscottr #include <sys/resourcevar.h>
488a007407Sscottr 
498a007407Sscottr #include <machine/bus.h>
508a007407Sscottr #include <machine/grfioctl.h>
518a007407Sscottr 
528a007407Sscottr #include <mac68k/nubus/nubus.h>
538a007407Sscottr #include <mac68k/dev/grfvar.h>
548a007407Sscottr #include <mac68k/dev/macfbvar.h>
558a007407Sscottr 
568a007407Sscottr #include <miscfs/specfs/specdev.h>
578a007407Sscottr 
588a007407Sscottr #include <uvm/uvm_extern.h>
598a007407Sscottr 
60e7ae23fdSchristos #include "ioconf.h"
61e7ae23fdSchristos 
6277a6b82bSgehenna dev_type_open(grfopen);
6377a6b82bSgehenna dev_type_close(grfclose);
6477a6b82bSgehenna dev_type_ioctl(grfioctl);
6577a6b82bSgehenna dev_type_mmap(grfmmap);
6677a6b82bSgehenna 
6777a6b82bSgehenna const struct cdevsw grf_cdevsw = {
68a68f9396Sdholland 	.d_open = grfopen,
69a68f9396Sdholland 	.d_close = grfclose,
70a68f9396Sdholland 	.d_read = noread,
71a68f9396Sdholland 	.d_write = nowrite,
72a68f9396Sdholland 	.d_ioctl = grfioctl,
73a68f9396Sdholland 	.d_stop = nostop,
74a68f9396Sdholland 	.d_tty = notty,
75a68f9396Sdholland 	.d_poll = nopoll,
76a68f9396Sdholland 	.d_mmap = grfmmap,
77a68f9396Sdholland 	.d_kqfilter = nokqfilter,
78f9228f42Sdholland 	.d_discard = nodiscard,
79a68f9396Sdholland 	.d_flag = 0
8077a6b82bSgehenna };
818a007407Sscottr 
827acd68b1Schs void	grf_scinit(struct grf_softc *, const char *, int);
837acd68b1Schs void	grf_init(int);
8453524e44Schristos int	grfmap(dev_t, struct macfb_softc *, void **, struct proc *);
8553524e44Schristos int	grfunmap(dev_t, struct macfb_softc *, void *, struct proc *);
868a007407Sscottr 
878a007407Sscottr /* Non-private for the benefit of libkvm. */
888a007407Sscottr struct	grf_softc *grf_softc;
898a007407Sscottr int	numgrf = 0;
908a007407Sscottr 
918a007407Sscottr /*
928a007407Sscottr  * Initialize a softc to sane defaults.
938a007407Sscottr  */
948a007407Sscottr void
grf_scinit(struct grf_softc * sc,const char * name,int unit)957acd68b1Schs grf_scinit(struct grf_softc *sc, const char *name, int unit)
968a007407Sscottr {
978a007407Sscottr 	memset(sc, 0, sizeof(struct grf_softc));
988a007407Sscottr 	snprintf(sc->sc_xname, sizeof(sc->sc_xname), "%s%d", name, unit);
998a007407Sscottr 	sc->mfb_sc = NULL;
1008a007407Sscottr }
1018a007407Sscottr 
1028a007407Sscottr /*
1038a007407Sscottr  * (Re-)initialize the grf_softc block so that at least the requested
1048a007407Sscottr  * number of elements has been allocated.  If this results in more
1058a007407Sscottr  * elements than we had prior to getting here, we initialize each of
1068a007407Sscottr  * them to avoid problems down the road.
1078a007407Sscottr  */
1088a007407Sscottr void
grf_init(int n)1097acd68b1Schs grf_init(int n)
1108a007407Sscottr {
1118a007407Sscottr 	struct grf_softc *sc;
1128a007407Sscottr 	int i;
1138a007407Sscottr 
1148a007407Sscottr 	if (n >= numgrf) {
1158a007407Sscottr 		i = numgrf;
1168a007407Sscottr 		numgrf = n + 1;
1178a007407Sscottr 
1188a007407Sscottr 		if (grf_softc == NULL)
1198a007407Sscottr 			sc = (struct grf_softc *)
1208a007407Sscottr 			    malloc(numgrf * sizeof(*sc),
121*d47bcd29Schs 			    M_DEVBUF, M_WAITOK);
1228a007407Sscottr 		else
1238a007407Sscottr 			sc = (struct grf_softc *)
1248a007407Sscottr 			    realloc(grf_softc, numgrf * sizeof(*sc),
125*d47bcd29Schs 			    M_DEVBUF, M_WAITOK);
1268a007407Sscottr 		grf_softc = sc;
1278a007407Sscottr 
1288a007407Sscottr 		/* Initialize per-softc structures. */
1298a007407Sscottr 		while (i < numgrf) {
1308a007407Sscottr 			grf_scinit(&grf_softc[i], "grf", i);
1318a007407Sscottr 			i++;
1328a007407Sscottr 		}
1338a007407Sscottr 	}
1348a007407Sscottr }
1358a007407Sscottr 
1368a007407Sscottr /*
1378a007407Sscottr  * Called by main() during pseudo-device attachment.  If we had a
1388a007407Sscottr  * way to configure additional grf devices later, this would actually
1398a007407Sscottr  * allocate enough space for them.  As it stands, it's nonsensical,
1408a007407Sscottr  * so other than a basic sanity check we do nothing.
1418a007407Sscottr  */
1428a007407Sscottr void
grfattach(int n)1437acd68b1Schs grfattach(int n)
1448a007407Sscottr {
1458a007407Sscottr 	if (n <= 0) {
1468a007407Sscottr #ifdef DIAGNOSTIC
1478a007407Sscottr 		panic("grfattach: count <= 0");
1488a007407Sscottr #endif
1498a007407Sscottr 		return;
1508a007407Sscottr 	}
1518a007407Sscottr 
1528a007407Sscottr #if 0 /* XXX someday, if we implement a way to attach after autoconfig */
1538a007407Sscottr 	grf_init(n);
1548a007407Sscottr #endif
1558a007407Sscottr }
1568a007407Sscottr 
1578a007407Sscottr /*
1588a007407Sscottr  * Called from macfb_attach() after setting up the frame buffer.  Since
1598a007407Sscottr  * there is a 1:1 correspondence between the macfb device and the grf
1608a007407Sscottr  * device, the only bit of information we really need is the macfb_softc.
1618a007407Sscottr  */
1628a007407Sscottr void
grf_attach(struct macfb_softc * sc,int unit)1637acd68b1Schs grf_attach(struct macfb_softc *sc, int unit)
1648a007407Sscottr {
1658a007407Sscottr 
1667acd68b1Schs 	grf_init(unit);
1678a007407Sscottr 	if (unit < numgrf)
1688a007407Sscottr 		grf_softc[unit].mfb_sc = sc;
1698a007407Sscottr }
1708a007407Sscottr 
1718a007407Sscottr /*
1728a007407Sscottr  * Standard device ops
1738a007407Sscottr  */
1748a007407Sscottr int
grfopen(dev_t dev,int flag,int mode,struct lwp * l)17595e1ffb1Schristos grfopen(dev_t dev, int flag, int mode, struct lwp *l)
1768a007407Sscottr {
1778a007407Sscottr 	struct grf_softc *sc;
1788a007407Sscottr 	int unit = GRFUNIT(dev);
1798a007407Sscottr 	int rv = 0;
1808a007407Sscottr 
1818a007407Sscottr 	if (grf_softc == NULL || unit >= numgrf)
1828a007407Sscottr 		return ENXIO;
1838a007407Sscottr 
1848a007407Sscottr 	sc = &grf_softc[unit];
1858a007407Sscottr 	if (sc->mfb_sc == NULL)
1868a007407Sscottr 		rv = ENXIO;
1878a007407Sscottr 
1888a007407Sscottr 	return rv;
1898a007407Sscottr }
1908a007407Sscottr 
1918a007407Sscottr int
grfclose(dev_t dev,int flag,int mode,struct lwp * l)19295e1ffb1Schristos grfclose(dev_t dev, int flag, int mode, struct lwp *l)
1938a007407Sscottr {
1948a007407Sscottr 	struct grf_softc *sc;
1958a007407Sscottr 	int unit = GRFUNIT(dev);
1968a007407Sscottr 	int rv = 0;
1978a007407Sscottr 
1988a007407Sscottr 	if (grf_softc == NULL || unit >= numgrf)
1998a007407Sscottr 		return ENXIO;
2008a007407Sscottr 
2018a007407Sscottr 	sc = &grf_softc[unit];
2028a007407Sscottr 	if (sc->mfb_sc != NULL)
2038a007407Sscottr 		macfb_clear(sc->mfb_sc->sc_dc);	/* clear the display */
2048a007407Sscottr 	else
2058a007407Sscottr 		rv = ENXIO;
2068a007407Sscottr 
2078a007407Sscottr 	return rv;
2088a007407Sscottr }
2098a007407Sscottr 
2108a007407Sscottr int
grfioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)21153524e44Schristos grfioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
2128a007407Sscottr {
2138a007407Sscottr 	struct grf_softc *sc;
2148a007407Sscottr 	struct macfb_devconfig *dc;
215d220ba7dSscottr #if defined(GRF_COMPAT) || (NGRF > 0)
2168a007407Sscottr 	struct grfinfo *gd;
217d220ba7dSscottr #endif /* GRF_COMPAT || (NGRF > 0) */
2188a007407Sscottr 	struct grfmode *gm;
2198a007407Sscottr 	int unit = GRFUNIT(dev);
2208a007407Sscottr 	int rv;
2218a007407Sscottr 
2228a007407Sscottr 	if (grf_softc == NULL || unit >= numgrf)
2238a007407Sscottr 		return ENXIO;
2248a007407Sscottr 
2258a007407Sscottr 	sc = &grf_softc[unit];
2268a007407Sscottr 	if (sc->mfb_sc == NULL)
2278a007407Sscottr 		return ENXIO;
2288a007407Sscottr 
2298a007407Sscottr 	dc = sc->mfb_sc->sc_dc;
2308a007407Sscottr 
2318a007407Sscottr 	switch (cmd) {
232d220ba7dSscottr #if defined(GRF_COMPAT) || (NGRF > 0)
2338a007407Sscottr 	case GRFIOCGINFO:
2348a007407Sscottr 		gd = (struct grfinfo *)data;
2358a007407Sscottr 		memset(gd, 0, sizeof(struct grfinfo));
23653524e44Schristos 		gd->gd_fbaddr     = (void *)dc->dc_paddr;
2378a007407Sscottr 		gd->gd_fbsize     = dc->dc_size;
2388a007407Sscottr 		gd->gd_colors     = (short)(1 << dc->dc_depth);
2398a007407Sscottr 		gd->gd_planes     = (short)dc->dc_depth;
2408a007407Sscottr 		gd->gd_fbwidth    = dc->dc_wid;
2418a007407Sscottr 		gd->gd_fbheight   = dc->dc_ht;
2428a007407Sscottr 		gd->gd_fbrowbytes = dc->dc_rowbytes;
2438a007407Sscottr 		gd->gd_dwidth     = dc->dc_raster.width;
2448a007407Sscottr 		gd->gd_dheight    = dc->dc_raster.height;
2458a007407Sscottr 		rv = 0;
2468a007407Sscottr 		break;
247d220ba7dSscottr #endif /* GRF_COMPAT || (NGRF > 0) */
2488a007407Sscottr 
2498a007407Sscottr 	case GRFIOCON:
2508a007407Sscottr 	case GRFIOCOFF:
2518a007407Sscottr 		/* Nothing to do */
2528a007407Sscottr 		rv = 0;
2538a007407Sscottr 		break;
2548a007407Sscottr 
255d220ba7dSscottr #if defined(GRF_COMPAT) || (NGRF > 0)
2568a007407Sscottr 	case GRFIOCMAP:
25753524e44Schristos 		rv = grfmap(dev, sc->mfb_sc, (void **)data, l->l_proc);
2588a007407Sscottr 		break;
2598a007407Sscottr 
2608a007407Sscottr 	case GRFIOCUNMAP:
26153524e44Schristos 		rv = grfunmap(dev, sc->mfb_sc, *(void **)data, l->l_proc);
2628a007407Sscottr 		break;
263d220ba7dSscottr #endif /* GRF_COMPAT || (NGRF > 0) */
2648a007407Sscottr 
2658a007407Sscottr 	case GRFIOCGMODE:
2668a007407Sscottr 		gm = (struct grfmode *)data;
2678a007407Sscottr 		memset(gm, 0, sizeof(struct grfmode));
2688a007407Sscottr 		gm->fbbase   = (char *)dc->dc_vaddr;
2698a007407Sscottr 		gm->fbsize   = dc->dc_size;
2708a007407Sscottr 		gm->fboff    = dc->dc_offset;
2718a007407Sscottr 		gm->rowbytes = dc->dc_rowbytes;
2728a007407Sscottr 		gm->width    = dc->dc_wid;
2738a007407Sscottr 		gm->height   = dc->dc_ht;
2748a007407Sscottr 		gm->psize    = dc->dc_depth;
2758a007407Sscottr 		rv = 0;
2768a007407Sscottr 		break;
2778a007407Sscottr 
2788a007407Sscottr 	case GRFIOCLISTMODES:
2798a007407Sscottr 	case GRFIOCGETMODE:
2808a007407Sscottr 	case GRFIOCSETMODE:
2818a007407Sscottr 		/* NONE of these operations are (officially) supported. */
2828a007407Sscottr 	default:
2838a007407Sscottr 		rv = EINVAL;
2848a007407Sscottr 		break;
2858a007407Sscottr 	}
2868a007407Sscottr 	return rv;
2878a007407Sscottr }
2888a007407Sscottr 
289889c658bSsimonb paddr_t
grfmmap(dev_t dev,off_t off,int prot)2907acd68b1Schs grfmmap(dev_t dev, off_t off, int prot)
2918a007407Sscottr {
2928a007407Sscottr 	struct grf_softc *sc;
2938a007407Sscottr 	struct macfb_devconfig *dc;
2948a007407Sscottr 	int unit = GRFUNIT(dev);
2958a007407Sscottr 
2968a007407Sscottr 	if (grf_softc == NULL || unit >= numgrf)
2978a007407Sscottr 		return ENXIO;
2988a007407Sscottr 
2998a007407Sscottr 	sc = &grf_softc[unit];
3008a007407Sscottr 	if (sc->mfb_sc == NULL)
3018a007407Sscottr 		return ENXIO;
3028a007407Sscottr 
3038a007407Sscottr 	dc = sc->mfb_sc->sc_dc;
3048a007407Sscottr 
3059e93b0c3Sscottr 	if ((u_int)off < m68k_round_page(dc->dc_offset + dc->dc_size))
3069e93b0c3Sscottr 		return m68k_btop(dc->dc_paddr + off);
3078a007407Sscottr 
3089e93b0c3Sscottr 	return (-1);
3098a007407Sscottr }
3108a007407Sscottr 
3118a007407Sscottr int
grfmap(dev_t dev,struct macfb_softc * sc,void ** addrp,struct proc * p)31253524e44Schristos grfmap(dev_t dev, struct macfb_softc *sc, void **addrp, struct proc *p)
3138a007407Sscottr {
3146d40f9ffSchs 	size_t len;
3156d40f9ffSchs 	int error;
3168a007407Sscottr 
3178a007407Sscottr 	len = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
3186d40f9ffSchs 	error = uvm_mmap_dev(p, addrp, len, dev, 0);
3198a007407Sscottr 
3208a007407Sscottr 	/* Offset into page: */
321d938af03She 	*addrp = (char *)*addrp + sc->sc_dc->dc_offset;
3228a007407Sscottr 
3238a007407Sscottr 	return (error);
3248a007407Sscottr }
3258a007407Sscottr 
3268a007407Sscottr int
grfunmap(dev_t dev,struct macfb_softc * sc,void * addr,struct proc * p)32753524e44Schristos grfunmap(dev_t dev, struct macfb_softc *sc, void *addr, struct proc *p)
3288a007407Sscottr {
3296d40f9ffSchs 	size_t size;
3308a007407Sscottr 
331d938af03She 	addr = (char *)addr - sc->sc_dc->dc_offset;
3328a007407Sscottr 
3338a007407Sscottr 	if (addr <= 0)
3348a007407Sscottr 		return (-1);
3358a007407Sscottr 
3368a007407Sscottr 	size = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
337ac3bc537Schs 	uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr, (vaddr_t)addr + size);
338ac3bc537Schs 	return 0;
3398a007407Sscottr }
340