1*6e54367aSthorpej /* $NetBSD: ofrom.c,v 1.29 2021/01/27 03:10:21 thorpej Exp $ */
280675955Sthorpej
380675955Sthorpej /*
480675955Sthorpej * Copyright 1998
580675955Sthorpej * Digital Equipment Corporation. All rights reserved.
680675955Sthorpej *
780675955Sthorpej * This software is furnished under license and may be used and
880675955Sthorpej * copied only in accordance with the following terms and conditions.
980675955Sthorpej * Subject to these conditions, you may download, copy, install,
1080675955Sthorpej * use, modify and distribute this software in source and/or binary
1180675955Sthorpej * form. No title or ownership is transferred hereby.
1280675955Sthorpej *
1380675955Sthorpej * 1) Any source code used, modified or distributed must reproduce
1480675955Sthorpej * and retain this copyright notice and list of conditions as
1580675955Sthorpej * they appear in the source file.
1680675955Sthorpej *
1780675955Sthorpej * 2) No right is granted to use any trade name, trademark, or logo of
1880675955Sthorpej * Digital Equipment Corporation. Neither the "Digital Equipment
1980675955Sthorpej * Corporation" name nor any trademark or logo of Digital Equipment
2080675955Sthorpej * Corporation may be used to endorse or promote products derived
2180675955Sthorpej * from this software without the prior written permission of
2280675955Sthorpej * Digital Equipment Corporation.
2380675955Sthorpej *
2480675955Sthorpej * 3) This software is provided "AS-IS" and any express or implied
2580675955Sthorpej * warranties, including but not limited to, any implied warranties
2680675955Sthorpej * of merchantability, fitness for a particular purpose, or
2780675955Sthorpej * non-infringement are disclaimed. In no event shall DIGITAL be
2880675955Sthorpej * liable for any damages whatsoever, and in particular, DIGITAL
2980675955Sthorpej * shall not be liable for special, indirect, consequential, or
3080675955Sthorpej * incidental damages or damages for lost profits, loss of
3180675955Sthorpej * revenue or loss of use, whether such damages arise in contract,
3280675955Sthorpej * negligence, tort, under statute, in equity, at law or otherwise,
3380675955Sthorpej * even if advised of the possibility of such damage.
3480675955Sthorpej */
3580675955Sthorpej
3680675955Sthorpej /*
3780675955Sthorpej * XXX open for writing (for user programs to mmap, and write contents)
3880675955Sthorpej */
3980675955Sthorpej
40ed517291Slukem #include <sys/cdefs.h>
41*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: ofrom.c,v 1.29 2021/01/27 03:10:21 thorpej Exp $");
42ed517291Slukem
4380675955Sthorpej #include <sys/param.h>
4480675955Sthorpej #include <sys/device.h>
4580675955Sthorpej #include <sys/systm.h>
4680675955Sthorpej #include <sys/conf.h>
4780675955Sthorpej #include <sys/fcntl.h>
48703fe5baSdyoung #include <sys/bus.h>
4980675955Sthorpej
5080675955Sthorpej #include <uvm/uvm_extern.h>
5180675955Sthorpej
5280675955Sthorpej #include <dev/ofw/openfirm.h>
5380675955Sthorpej
5480675955Sthorpej struct ofrom_softc {
5580675955Sthorpej int enabled;
56cac3513fStsutsui paddr_t base;
57cac3513fStsutsui paddr_t size;
5880675955Sthorpej };
5980675955Sthorpej
609af887f0Smrg int ofromprobe(device_t, cfdata_t, void *);
619af887f0Smrg void ofromattach(device_t, device_t, void *);
6280675955Sthorpej
639af887f0Smrg CFATTACH_DECL_NEW(ofrom, sizeof(struct ofrom_softc),
6489bf5a8fSthorpej ofromprobe, ofromattach, NULL, NULL);
6580675955Sthorpej
6680675955Sthorpej extern struct cfdriver ofrom_cd;
6780675955Sthorpej
6877a6b82bSgehenna dev_type_open(ofromopen);
6977a6b82bSgehenna dev_type_read(ofromrw);
7077a6b82bSgehenna dev_type_mmap(ofrommmap);
7177a6b82bSgehenna
7277a6b82bSgehenna const struct cdevsw ofrom_cdevsw = {
73a68f9396Sdholland .d_open = ofromopen,
74a68f9396Sdholland .d_close = nullclose,
75a68f9396Sdholland .d_read = ofromrw,
76a68f9396Sdholland .d_write = ofromrw,
77a68f9396Sdholland .d_ioctl = noioctl,
78a68f9396Sdholland .d_stop = nostop,
79a68f9396Sdholland .d_tty = notty,
80a68f9396Sdholland .d_poll = nopoll,
81a68f9396Sdholland .d_mmap = ofrommmap,
82a68f9396Sdholland .d_kqfilter = nokqfilter,
83f9228f42Sdholland .d_discard = nodiscard,
84a68f9396Sdholland .d_flag = 0
8577a6b82bSgehenna };
8680675955Sthorpej
87*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
88*6e54367aSthorpej { .compat = "rom" },
89*6e54367aSthorpej DEVICE_COMPAT_EOL
90*6e54367aSthorpej };
91*6e54367aSthorpej
9280675955Sthorpej int
ofromprobe(device_t parent,cfdata_t cf,void * aux)939af887f0Smrg ofromprobe(device_t parent, cfdata_t cf, void *aux)
9480675955Sthorpej {
9580675955Sthorpej struct ofbus_attach_args *oba = aux;
9680675955Sthorpej
97*6e54367aSthorpej return of_compatible_match(oba->oba_phandle, compat_data) * 5;
9880675955Sthorpej }
9980675955Sthorpej
10080675955Sthorpej
10180675955Sthorpej void
ofromattach(device_t parent,device_t self,void * aux)1029af887f0Smrg ofromattach(device_t parent, device_t self, void *aux)
10380675955Sthorpej {
1049af887f0Smrg struct ofrom_softc *sc = device_private(self);
10580675955Sthorpej struct ofbus_attach_args *oba = aux;
10680675955Sthorpej char regbuf[8];
10780675955Sthorpej
10880675955Sthorpej if (OF_getproplen(oba->oba_phandle, "reg") != 8) {
10980675955Sthorpej printf(": invalid reg property\n");
11080675955Sthorpej return;
11180675955Sthorpej }
11280675955Sthorpej if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) {
11380675955Sthorpej printf(": couldn't read reg property\n");
11480675955Sthorpej return;
11580675955Sthorpej }
11680675955Sthorpej sc->base = of_decode_int(®buf[0]);
11780675955Sthorpej sc->size = of_decode_int(®buf[4]);
11880675955Sthorpej sc->enabled = 1;
11980675955Sthorpej
12080675955Sthorpej printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1);
12180675955Sthorpej }
12280675955Sthorpej
12380675955Sthorpej int
ofromopen(dev_t dev,int oflags,int devtype,struct lwp * l)1243ebed4b3Scegger ofromopen(dev_t dev, int oflags, int devtype, struct lwp *l)
12580675955Sthorpej {
12680675955Sthorpej struct ofrom_softc *sc;
12780675955Sthorpej
1283ebed4b3Scegger sc = device_lookup_private(&ofrom_cd, minor(dev));
12980675955Sthorpej if (!sc || !sc->enabled)
13080675955Sthorpej return (ENXIO);
13180675955Sthorpej
13280675955Sthorpej if (oflags & FWRITE)
13380675955Sthorpej return (EINVAL);
13480675955Sthorpej
13580675955Sthorpej return (0);
13680675955Sthorpej }
13780675955Sthorpej
13880675955Sthorpej int
ofromrw(dev_t dev,struct uio * uio,int flags)1393ebed4b3Scegger ofromrw(dev_t dev, struct uio *uio, int flags)
14080675955Sthorpej {
141723d94f9Smatt pmap_t kpm = pmap_kernel();
14280675955Sthorpej struct ofrom_softc *sc;
1433ebed4b3Scegger int c, error = 0;
14480675955Sthorpej struct iovec *iov;
145cac3513fStsutsui paddr_t v;
146cac3513fStsutsui psize_t o;
14722d492acShe extern kmutex_t memlock;
14880675955Sthorpej extern char *memhook;
14980675955Sthorpej
1503ebed4b3Scegger sc = device_lookup_private(&ofrom_cd, minor(dev));
15180675955Sthorpej if (!sc || !sc->enabled)
15280675955Sthorpej return (ENXIO); /* XXX PANIC */
15380675955Sthorpej
15422d492acShe mutex_enter(&memlock);
15580675955Sthorpej while (uio->uio_resid > 0 && error == 0) {
15680675955Sthorpej iov = uio->uio_iov;
15780675955Sthorpej if (iov->iov_len == 0) {
15880675955Sthorpej uio->uio_iov++;
15980675955Sthorpej uio->uio_iovcnt--;
16080675955Sthorpej if (uio->uio_iovcnt < 0)
16180675955Sthorpej panic("ofromrw");
16280675955Sthorpej continue;
16380675955Sthorpej }
16480675955Sthorpej
16580675955Sthorpej /*
16680675955Sthorpej * Since everything is page aligned and no more
16780675955Sthorpej * than the rest of a page is done at once, we
16880675955Sthorpej * can just check that the offset isn't too big.
16980675955Sthorpej */
17080675955Sthorpej if (uio->uio_offset >= sc->size)
17180675955Sthorpej break;
17280675955Sthorpej
17380675955Sthorpej v = sc->base + uio->uio_offset;
174723d94f9Smatt pmap_kenter_pa((vaddr_t)memhook, trunc_page(v),
175723d94f9Smatt uio->uio_rw == UIO_READ ? VM_PROT_READ : VM_PROT_WRITE,
176723d94f9Smatt 0);
177723d94f9Smatt pmap_update(kpm);
17880675955Sthorpej o = uio->uio_offset & PGOFSET;
179d1579b2dSriastradh c = uimin(uio->uio_resid, (int)(PAGE_SIZE - o));
1805ebcdbe8Schristos error = uiomove((char *)memhook + o, c, uio);
181723d94f9Smatt pmap_kremove((vaddr_t)memhook, (vaddr_t)memhook + PAGE_SIZE);
182723d94f9Smatt pmap_update(kpm);
18380675955Sthorpej }
18422d492acShe mutex_exit(&memlock);
18580675955Sthorpej
18680675955Sthorpej return (error);
18780675955Sthorpej }
18880675955Sthorpej
18980675955Sthorpej paddr_t
ofrommmap(dev_t dev,off_t off,int prot)1903ebed4b3Scegger ofrommmap(dev_t dev, off_t off, int prot)
19180675955Sthorpej {
19280675955Sthorpej struct ofrom_softc *sc;
19380675955Sthorpej
1943ebed4b3Scegger sc = device_lookup_private(&ofrom_cd, minor(dev));
19580675955Sthorpej if (!sc || !sc->enabled)
19680675955Sthorpej return (-1); /* XXX PANIC */
19780675955Sthorpej
19880675955Sthorpej if ((u_int)off >= sc->size)
19980675955Sthorpej return (-1);
20080675955Sthorpej
201aa156394Sthorpej return arm_btop(sc->base + off);
20280675955Sthorpej }
203