ofrom.c revision 1.17
11.17She/*	$NetBSD: ofrom.c,v 1.17 2008/11/08 10:18:10 he 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.11Slukem
401.11Slukem#include <sys/cdefs.h>
411.17She__KERNEL_RCSID(0, "$NetBSD: ofrom.c,v 1.17 2008/11/08 10:18:10 he Exp $");
421.1Sthorpej
431.1Sthorpej#include <sys/param.h>
441.1Sthorpej#include <sys/device.h>
451.1Sthorpej#include <sys/systm.h>
461.1Sthorpej#include <sys/conf.h>
471.1Sthorpej#include <sys/fcntl.h>
481.1Sthorpej
491.1Sthorpej#include <uvm/uvm_extern.h>
501.1Sthorpej
511.1Sthorpej#include <machine/bus.h>
521.1Sthorpej#include <dev/ofw/openfirm.h>
531.1Sthorpej
541.1Sthorpejstruct ofrom_softc {
551.1Sthorpej	struct device	sc_dev;
561.1Sthorpej	int		enabled;
571.12Stsutsui	paddr_t		base;
581.12Stsutsui	paddr_t		size;
591.1Sthorpej};
601.1Sthorpej
611.1Sthorpejint ofromprobe __P((struct device *, struct cfdata *, void *));
621.1Sthorpejvoid ofromattach __P((struct device *, struct device *, void *));
631.1Sthorpej
641.7SthorpejCFATTACH_DECL(ofrom, sizeof(struct ofrom_softc),
651.8Sthorpej    ofromprobe, ofromattach, NULL, NULL);
661.1Sthorpej
671.1Sthorpejextern struct cfdriver ofrom_cd;
681.1Sthorpej
691.5Sgehennadev_type_open(ofromopen);
701.5Sgehennadev_type_read(ofromrw);
711.5Sgehennadev_type_mmap(ofrommmap);
721.5Sgehenna
731.5Sgehennaconst struct cdevsw ofrom_cdevsw = {
741.5Sgehenna	ofromopen, nullclose, ofromrw, ofromrw, noioctl,
751.9Sjdolecek	nostop, notty, nopoll, ofrommmap, nokqfilter,
761.5Sgehenna};
771.1Sthorpej
781.1Sthorpejint
791.1Sthorpejofromprobe(parent, cf, aux)
801.1Sthorpej	struct device *parent;
811.1Sthorpej	struct cfdata *cf;
821.1Sthorpej	void *aux;
831.1Sthorpej{
841.1Sthorpej	struct ofbus_attach_args *oba = aux;
851.4Syamt	static const char *const compatible_strings[] = { "rom", NULL };
861.1Sthorpej
871.1Sthorpej	return (of_compatible(oba->oba_phandle, compatible_strings) == -1) ?
881.1Sthorpej	    0 : 5;
891.1Sthorpej}
901.1Sthorpej
911.1Sthorpej
921.1Sthorpejvoid
931.1Sthorpejofromattach(parent, self, aux)
941.1Sthorpej	struct device *parent, *self;
951.1Sthorpej	void *aux;
961.1Sthorpej{
971.1Sthorpej	struct ofrom_softc *sc = (struct ofrom_softc *)self;
981.1Sthorpej	struct ofbus_attach_args *oba = aux;
991.1Sthorpej	char regbuf[8];
1001.1Sthorpej
1011.1Sthorpej	if (OF_getproplen(oba->oba_phandle, "reg") != 8) {
1021.1Sthorpej		printf(": invalid reg property\n");
1031.1Sthorpej		return;
1041.1Sthorpej	}
1051.1Sthorpej	if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) {
1061.1Sthorpej		printf(": couldn't read reg property\n");
1071.1Sthorpej		return;
1081.1Sthorpej	}
1091.1Sthorpej	sc->base = of_decode_int(&regbuf[0]);
1101.1Sthorpej	sc->size = of_decode_int(&regbuf[4]);
1111.1Sthorpej	sc->enabled = 1;
1121.1Sthorpej
1131.1Sthorpej	printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1);
1141.1Sthorpej}
1151.1Sthorpej
1161.1Sthorpejint
1171.16Sceggerofromopen(dev_t dev, int oflags, int devtype, struct lwp *l)
1181.1Sthorpej{
1191.1Sthorpej	struct ofrom_softc *sc;
1201.1Sthorpej
1211.16Scegger	sc = device_lookup_private(&ofrom_cd, minor(dev));
1221.1Sthorpej	if (!sc || !sc->enabled)
1231.1Sthorpej		return (ENXIO);
1241.1Sthorpej
1251.1Sthorpej	if (oflags & FWRITE)
1261.1Sthorpej		return (EINVAL);
1271.1Sthorpej
1281.1Sthorpej	return (0);
1291.1Sthorpej}
1301.1Sthorpej
1311.1Sthorpejint
1321.16Sceggerofromrw(dev_t dev, struct uio *uio, int flags)
1331.1Sthorpej{
1341.1Sthorpej	struct ofrom_softc *sc;
1351.16Scegger	int c, error = 0;
1361.1Sthorpej	struct iovec *iov;
1371.12Stsutsui	paddr_t v;
1381.12Stsutsui	psize_t o;
1391.17She	extern kmutex_t memlock;
1401.1Sthorpej	extern char *memhook;
1411.1Sthorpej
1421.16Scegger	sc = device_lookup_private(&ofrom_cd, minor(dev));
1431.1Sthorpej	if (!sc || !sc->enabled)
1441.1Sthorpej		return (ENXIO);			/* XXX PANIC */
1451.1Sthorpej
1461.1Sthorpej	/* lock against other uses of shared vmmap */
1471.17She	mutex_enter(&memlock);
1481.1Sthorpej
1491.1Sthorpej	while (uio->uio_resid > 0 && error == 0) {
1501.1Sthorpej		iov = uio->uio_iov;
1511.1Sthorpej		if (iov->iov_len == 0) {
1521.1Sthorpej			uio->uio_iov++;
1531.1Sthorpej			uio->uio_iovcnt--;
1541.1Sthorpej			if (uio->uio_iovcnt < 0)
1551.1Sthorpej				panic("ofromrw");
1561.1Sthorpej			continue;
1571.1Sthorpej		}
1581.1Sthorpej
1591.1Sthorpej		/*
1601.1Sthorpej		 * Since everything is page aligned and no more
1611.1Sthorpej		 * than the rest of a page is done at once, we
1621.1Sthorpej		 * can just check that the offset isn't too big.
1631.1Sthorpej		 */
1641.1Sthorpej		if (uio->uio_offset >= sc->size)
1651.1Sthorpej			break;
1661.1Sthorpej
1671.1Sthorpej		v = sc->base + uio->uio_offset;
1681.12Stsutsui		pmap_enter(pmap_kernel(), (vaddr_t)memhook,
1691.1Sthorpej		    trunc_page(v), uio->uio_rw == UIO_READ ?
1701.1Sthorpej		    VM_PROT_READ : VM_PROT_WRITE, PMAP_WIRED);
1711.1Sthorpej		pmap_update(pmap_kernel());
1721.1Sthorpej		o = uio->uio_offset & PGOFSET;
1731.10Sthorpej		c = min(uio->uio_resid, (int)(PAGE_SIZE - o));
1741.15Schristos		error = uiomove((char *)memhook + o, c, uio);
1751.12Stsutsui		pmap_remove(pmap_kernel(), (vaddr_t)memhook,
1761.12Stsutsui		    (vaddr_t)memhook + PAGE_SIZE);
1771.1Sthorpej		pmap_update(pmap_kernel());
1781.1Sthorpej	}
1791.1Sthorpej
1801.17She	mutex_exit(&memlock);
1811.1Sthorpej
1821.1Sthorpej	return (error);
1831.1Sthorpej}
1841.1Sthorpej
1851.1Sthorpejpaddr_t
1861.16Sceggerofrommmap(dev_t dev, off_t off, int prot)
1871.1Sthorpej{
1881.1Sthorpej	struct ofrom_softc *sc;
1891.1Sthorpej
1901.16Scegger	sc = device_lookup_private(&ofrom_cd, minor(dev));
1911.1Sthorpej	if (!sc || !sc->enabled)
1921.1Sthorpej		return (-1);			/* XXX PANIC */
1931.1Sthorpej
1941.1Sthorpej	if ((u_int)off >= sc->size)
1951.1Sthorpej		return (-1);
1961.1Sthorpej
1971.3Sthorpej	return arm_btop(sc->base + off);
1981.1Sthorpej}
199