ofrom.c revision 1.3
11.3Sthorpej/*	$NetBSD: ofrom.c,v 1.3 2002/03/24 03:37:26 thorpej Exp $	*/
21.1Sthorpej
31.1Sthorpej/*
41.1Sthorpej * Copyright 1998
51.1Sthorpej * Digital Equipment Corporation. All rights reserved.
61.1Sthorpej *
71.1Sthorpej * This software is furnished under license and may be used and
81.1Sthorpej * copied only in accordance with the following terms and conditions.
91.1Sthorpej * Subject to these conditions, you may download, copy, install,
101.1Sthorpej * use, modify and distribute this software in source and/or binary
111.1Sthorpej * form. No title or ownership is transferred hereby.
121.1Sthorpej *
131.1Sthorpej * 1) Any source code used, modified or distributed must reproduce
141.1Sthorpej *    and retain this copyright notice and list of conditions as
151.1Sthorpej *    they appear in the source file.
161.1Sthorpej *
171.1Sthorpej * 2) No right is granted to use any trade name, trademark, or logo of
181.1Sthorpej *    Digital Equipment Corporation. Neither the "Digital Equipment
191.1Sthorpej *    Corporation" name nor any trademark or logo of Digital Equipment
201.1Sthorpej *    Corporation may be used to endorse or promote products derived
211.1Sthorpej *    from this software without the prior written permission of
221.1Sthorpej *    Digital Equipment Corporation.
231.1Sthorpej *
241.1Sthorpej * 3) This software is provided "AS-IS" and any express or implied
251.1Sthorpej *    warranties, including but not limited to, any implied warranties
261.1Sthorpej *    of merchantability, fitness for a particular purpose, or
271.1Sthorpej *    non-infringement are disclaimed. In no event shall DIGITAL be
281.1Sthorpej *    liable for any damages whatsoever, and in particular, DIGITAL
291.1Sthorpej *    shall not be liable for special, indirect, consequential, or
301.1Sthorpej *    incidental damages or damages for lost profits, loss of
311.1Sthorpej *    revenue or loss of use, whether such damages arise in contract,
321.1Sthorpej *    negligence, tort, under statute, in equity, at law or otherwise,
331.1Sthorpej *    even if advised of the possibility of such damage.
341.1Sthorpej */
351.1Sthorpej
361.1Sthorpej/*
371.1Sthorpej * XXX open for writing (for user programs to mmap, and write contents)
381.1Sthorpej */
391.1Sthorpej
401.1Sthorpej#include <sys/param.h>
411.1Sthorpej#include <sys/device.h>
421.1Sthorpej#include <sys/systm.h>
431.1Sthorpej#include <sys/conf.h>
441.1Sthorpej#include <sys/fcntl.h>
451.1Sthorpej
461.1Sthorpej#include <uvm/uvm_extern.h>
471.1Sthorpej
481.1Sthorpej#include <machine/bus.h>
491.1Sthorpej#include <dev/ofw/openfirm.h>
501.1Sthorpej
511.1Sthorpejstruct ofrom_softc {
521.1Sthorpej	struct device	sc_dev;
531.1Sthorpej	int		enabled;
541.1Sthorpej	vm_offset_t	base;
551.1Sthorpej	vm_size_t	size;
561.1Sthorpej};
571.1Sthorpej
581.1Sthorpejint ofromprobe __P((struct device *, struct cfdata *, void *));
591.1Sthorpejvoid ofromattach __P((struct device *, struct device *, void *));
601.1Sthorpej
611.1Sthorpejstruct cfattach ofrom_ca = {
621.1Sthorpej	sizeof(struct ofrom_softc), ofromprobe, ofromattach
631.1Sthorpej};
641.1Sthorpej
651.1Sthorpejextern struct cfdriver ofrom_cd;
661.1Sthorpej
671.1Sthorpejcdev_decl(ofrom);
681.1Sthorpej
691.1Sthorpejint
701.1Sthorpejofromprobe(parent, cf, aux)
711.1Sthorpej	struct device *parent;
721.1Sthorpej	struct cfdata *cf;
731.1Sthorpej	void *aux;
741.1Sthorpej{
751.1Sthorpej	struct ofbus_attach_args *oba = aux;
761.1Sthorpej	const char *compatible_strings[] = { "rom", NULL };
771.1Sthorpej
781.1Sthorpej	return (of_compatible(oba->oba_phandle, compatible_strings) == -1) ?
791.1Sthorpej	    0 : 5;
801.1Sthorpej}
811.1Sthorpej
821.1Sthorpej
831.1Sthorpejvoid
841.1Sthorpejofromattach(parent, self, aux)
851.1Sthorpej	struct device *parent, *self;
861.1Sthorpej	void *aux;
871.1Sthorpej{
881.1Sthorpej	struct ofrom_softc *sc = (struct ofrom_softc *)self;
891.1Sthorpej	struct ofbus_attach_args *oba = aux;
901.1Sthorpej	char regbuf[8];
911.1Sthorpej
921.1Sthorpej	if (OF_getproplen(oba->oba_phandle, "reg") != 8) {
931.1Sthorpej		printf(": invalid reg property\n");
941.1Sthorpej		return;
951.1Sthorpej	}
961.1Sthorpej	if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) {
971.1Sthorpej		printf(": couldn't read reg property\n");
981.1Sthorpej		return;
991.1Sthorpej	}
1001.1Sthorpej	sc->base = of_decode_int(&regbuf[0]);
1011.1Sthorpej	sc->size = of_decode_int(&regbuf[4]);
1021.1Sthorpej	sc->enabled = 1;
1031.1Sthorpej
1041.1Sthorpej	printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1);
1051.1Sthorpej}
1061.1Sthorpej
1071.1Sthorpejint
1081.1Sthorpejofromopen(dev, oflags, devtype, p)
1091.1Sthorpej	dev_t dev;
1101.1Sthorpej	int oflags, devtype;
1111.1Sthorpej	struct proc *p;
1121.1Sthorpej{
1131.1Sthorpej	struct ofrom_softc *sc;
1141.1Sthorpej	int unit = minor(dev);
1151.1Sthorpej
1161.1Sthorpej	if (unit >= ofrom_cd.cd_ndevs)
1171.1Sthorpej		return (ENXIO);
1181.1Sthorpej	sc = ofrom_cd.cd_devs[unit];
1191.1Sthorpej	if (!sc || !sc->enabled)
1201.1Sthorpej		return (ENXIO);
1211.1Sthorpej
1221.1Sthorpej	if (oflags & FWRITE)
1231.1Sthorpej		return (EINVAL);
1241.1Sthorpej
1251.1Sthorpej	return (0);
1261.1Sthorpej}
1271.1Sthorpej
1281.1Sthorpejint
1291.1Sthorpejofromclose(dev, fflag, devtype, p)
1301.1Sthorpej	dev_t dev;
1311.1Sthorpej	int fflag, devtype;
1321.1Sthorpej	struct proc *p;
1331.1Sthorpej{
1341.1Sthorpej
1351.1Sthorpej	return (0);
1361.1Sthorpej}
1371.1Sthorpej
1381.1Sthorpejint
1391.1Sthorpejofromrw(dev, uio, flags)
1401.1Sthorpej	dev_t dev;
1411.1Sthorpej	struct uio *uio;
1421.1Sthorpej	int flags;
1431.1Sthorpej{
1441.1Sthorpej	struct ofrom_softc *sc;
1451.1Sthorpej	int c, error = 0, unit = minor(dev);
1461.1Sthorpej	struct iovec *iov;
1471.1Sthorpej	vm_offset_t o, v;
1481.1Sthorpej	extern int physlock;
1491.1Sthorpej	extern char *memhook;
1501.1Sthorpej
1511.1Sthorpej	if (unit >= ofrom_cd.cd_ndevs)
1521.1Sthorpej		return (ENXIO);			/* XXX PANIC */
1531.1Sthorpej	sc = ofrom_cd.cd_devs[unit];
1541.1Sthorpej	if (!sc || !sc->enabled)
1551.1Sthorpej		return (ENXIO);			/* XXX PANIC */
1561.1Sthorpej
1571.1Sthorpej	/* lock against other uses of shared vmmap */
1581.1Sthorpej	while (physlock > 0) {
1591.1Sthorpej		physlock++;
1601.1Sthorpej		error = tsleep((caddr_t)&physlock, PZERO | PCATCH, "ofromrw",
1611.1Sthorpej		    0);
1621.1Sthorpej		if (error)
1631.1Sthorpej			return (error);
1641.1Sthorpej	}
1651.1Sthorpej	physlock = 1;
1661.1Sthorpej
1671.1Sthorpej	while (uio->uio_resid > 0 && error == 0) {
1681.1Sthorpej		iov = uio->uio_iov;
1691.1Sthorpej		if (iov->iov_len == 0) {
1701.1Sthorpej			uio->uio_iov++;
1711.1Sthorpej			uio->uio_iovcnt--;
1721.1Sthorpej			if (uio->uio_iovcnt < 0)
1731.1Sthorpej				panic("ofromrw");
1741.1Sthorpej			continue;
1751.1Sthorpej		}
1761.1Sthorpej
1771.1Sthorpej		/*
1781.1Sthorpej		 * Since everything is page aligned and no more
1791.1Sthorpej		 * than the rest of a page is done at once, we
1801.1Sthorpej		 * can just check that the offset isn't too big.
1811.1Sthorpej		 */
1821.1Sthorpej		if (uio->uio_offset >= sc->size)
1831.1Sthorpej			break;
1841.1Sthorpej
1851.1Sthorpej		v = sc->base + uio->uio_offset;
1861.1Sthorpej		pmap_enter(pmap_kernel(), (vm_offset_t)memhook,
1871.1Sthorpej		    trunc_page(v), uio->uio_rw == UIO_READ ?
1881.1Sthorpej		    VM_PROT_READ : VM_PROT_WRITE, PMAP_WIRED);
1891.1Sthorpej		pmap_update(pmap_kernel());
1901.1Sthorpej		o = uio->uio_offset & PGOFSET;
1911.1Sthorpej		c = min(uio->uio_resid, (int)(NBPG - o));
1921.1Sthorpej		error = uiomove((caddr_t)memhook + o, c, uio);
1931.1Sthorpej		pmap_remove(pmap_kernel(), (vm_offset_t)memhook,
1941.1Sthorpej		    (vm_offset_t)memhook + NBPG);
1951.1Sthorpej		pmap_update(pmap_kernel());
1961.1Sthorpej	}
1971.1Sthorpej
1981.1Sthorpej	if (physlock > 1)
1991.1Sthorpej		wakeup((caddr_t)&physlock);
2001.1Sthorpej	physlock = 0;
2011.1Sthorpej
2021.1Sthorpej	return (error);
2031.2Sthorpej}
2041.2Sthorpej
2051.2Sthorpejint
2061.2Sthorpejofromioctl(dev, cmd, data, flag, p)
2071.2Sthorpej	dev_t dev;
2081.2Sthorpej	u_long cmd;
2091.2Sthorpej	caddr_t data;
2101.2Sthorpej	int flag;
2111.2Sthorpej	struct proc *p;
2121.2Sthorpej{
2131.2Sthorpej
2141.2Sthorpej	return (ENOTTY);
2151.1Sthorpej}
2161.1Sthorpej
2171.1Sthorpejpaddr_t
2181.1Sthorpejofrommmap(dev, off, prot)
2191.1Sthorpej	dev_t dev;
2201.1Sthorpej	off_t off;
2211.1Sthorpej	int prot;
2221.1Sthorpej{
2231.1Sthorpej	struct ofrom_softc *sc;
2241.1Sthorpej	int unit = minor(dev);
2251.1Sthorpej
2261.1Sthorpej	if (unit >= ofrom_cd.cd_ndevs)
2271.1Sthorpej		return (-1);			/* XXX PANIC */
2281.1Sthorpej	sc = ofrom_cd.cd_devs[unit];
2291.1Sthorpej	if (!sc || !sc->enabled)
2301.1Sthorpej		return (-1);			/* XXX PANIC */
2311.1Sthorpej
2321.1Sthorpej	if ((u_int)off >= sc->size)
2331.1Sthorpej		return (-1);
2341.1Sthorpej
2351.3Sthorpej	return arm_btop(sc->base + off);
2361.1Sthorpej}
237