ofrom.c revision 1.11
1/* $NetBSD: ofrom.c,v 1.11 2003/07/15 03:36:02 lukem Exp $ */ 2 3/* 4 * Copyright 1998 5 * Digital Equipment Corporation. All rights reserved. 6 * 7 * This software is furnished under license and may be used and 8 * copied only in accordance with the following terms and conditions. 9 * Subject to these conditions, you may download, copy, install, 10 * use, modify and distribute this software in source and/or binary 11 * form. No title or ownership is transferred hereby. 12 * 13 * 1) Any source code used, modified or distributed must reproduce 14 * and retain this copyright notice and list of conditions as 15 * they appear in the source file. 16 * 17 * 2) No right is granted to use any trade name, trademark, or logo of 18 * Digital Equipment Corporation. Neither the "Digital Equipment 19 * Corporation" name nor any trademark or logo of Digital Equipment 20 * Corporation may be used to endorse or promote products derived 21 * from this software without the prior written permission of 22 * Digital Equipment Corporation. 23 * 24 * 3) This software is provided "AS-IS" and any express or implied 25 * warranties, including but not limited to, any implied warranties 26 * of merchantability, fitness for a particular purpose, or 27 * non-infringement are disclaimed. In no event shall DIGITAL be 28 * liable for any damages whatsoever, and in particular, DIGITAL 29 * shall not be liable for special, indirect, consequential, or 30 * incidental damages or damages for lost profits, loss of 31 * revenue or loss of use, whether such damages arise in contract, 32 * negligence, tort, under statute, in equity, at law or otherwise, 33 * even if advised of the possibility of such damage. 34 */ 35 36/* 37 * XXX open for writing (for user programs to mmap, and write contents) 38 */ 39 40#include <sys/cdefs.h> 41__KERNEL_RCSID(0, "$NetBSD: ofrom.c,v 1.11 2003/07/15 03:36:02 lukem Exp $"); 42 43#include <sys/param.h> 44#include <sys/device.h> 45#include <sys/systm.h> 46#include <sys/conf.h> 47#include <sys/fcntl.h> 48 49#include <uvm/uvm_extern.h> 50 51#include <machine/bus.h> 52#include <dev/ofw/openfirm.h> 53 54struct ofrom_softc { 55 struct device sc_dev; 56 int enabled; 57 vm_offset_t base; 58 vm_size_t size; 59}; 60 61int ofromprobe __P((struct device *, struct cfdata *, void *)); 62void ofromattach __P((struct device *, struct device *, void *)); 63 64CFATTACH_DECL(ofrom, sizeof(struct ofrom_softc), 65 ofromprobe, ofromattach, NULL, NULL); 66 67extern struct cfdriver ofrom_cd; 68 69dev_type_open(ofromopen); 70dev_type_read(ofromrw); 71dev_type_mmap(ofrommmap); 72 73const struct cdevsw ofrom_cdevsw = { 74 ofromopen, nullclose, ofromrw, ofromrw, noioctl, 75 nostop, notty, nopoll, ofrommmap, nokqfilter, 76}; 77 78int 79ofromprobe(parent, cf, aux) 80 struct device *parent; 81 struct cfdata *cf; 82 void *aux; 83{ 84 struct ofbus_attach_args *oba = aux; 85 static const char *const compatible_strings[] = { "rom", NULL }; 86 87 return (of_compatible(oba->oba_phandle, compatible_strings) == -1) ? 88 0 : 5; 89} 90 91 92void 93ofromattach(parent, self, aux) 94 struct device *parent, *self; 95 void *aux; 96{ 97 struct ofrom_softc *sc = (struct ofrom_softc *)self; 98 struct ofbus_attach_args *oba = aux; 99 char regbuf[8]; 100 101 if (OF_getproplen(oba->oba_phandle, "reg") != 8) { 102 printf(": invalid reg property\n"); 103 return; 104 } 105 if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) { 106 printf(": couldn't read reg property\n"); 107 return; 108 } 109 sc->base = of_decode_int(®buf[0]); 110 sc->size = of_decode_int(®buf[4]); 111 sc->enabled = 1; 112 113 printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1); 114} 115 116int 117ofromopen(dev, oflags, devtype, p) 118 dev_t dev; 119 int oflags, devtype; 120 struct proc *p; 121{ 122 struct ofrom_softc *sc; 123 int unit = minor(dev); 124 125 if (unit >= ofrom_cd.cd_ndevs) 126 return (ENXIO); 127 sc = ofrom_cd.cd_devs[unit]; 128 if (!sc || !sc->enabled) 129 return (ENXIO); 130 131 if (oflags & FWRITE) 132 return (EINVAL); 133 134 return (0); 135} 136 137int 138ofromrw(dev, uio, flags) 139 dev_t dev; 140 struct uio *uio; 141 int flags; 142{ 143 struct ofrom_softc *sc; 144 int c, error = 0, unit = minor(dev); 145 struct iovec *iov; 146 vm_offset_t o, v; 147 extern int physlock; 148 extern char *memhook; 149 150 if (unit >= ofrom_cd.cd_ndevs) 151 return (ENXIO); /* XXX PANIC */ 152 sc = ofrom_cd.cd_devs[unit]; 153 if (!sc || !sc->enabled) 154 return (ENXIO); /* XXX PANIC */ 155 156 /* lock against other uses of shared vmmap */ 157 while (physlock > 0) { 158 physlock++; 159 error = tsleep((caddr_t)&physlock, PZERO | PCATCH, "ofromrw", 160 0); 161 if (error) 162 return (error); 163 } 164 physlock = 1; 165 166 while (uio->uio_resid > 0 && error == 0) { 167 iov = uio->uio_iov; 168 if (iov->iov_len == 0) { 169 uio->uio_iov++; 170 uio->uio_iovcnt--; 171 if (uio->uio_iovcnt < 0) 172 panic("ofromrw"); 173 continue; 174 } 175 176 /* 177 * Since everything is page aligned and no more 178 * than the rest of a page is done at once, we 179 * can just check that the offset isn't too big. 180 */ 181 if (uio->uio_offset >= sc->size) 182 break; 183 184 v = sc->base + uio->uio_offset; 185 pmap_enter(pmap_kernel(), (vm_offset_t)memhook, 186 trunc_page(v), uio->uio_rw == UIO_READ ? 187 VM_PROT_READ : VM_PROT_WRITE, PMAP_WIRED); 188 pmap_update(pmap_kernel()); 189 o = uio->uio_offset & PGOFSET; 190 c = min(uio->uio_resid, (int)(PAGE_SIZE - o)); 191 error = uiomove((caddr_t)memhook + o, c, uio); 192 pmap_remove(pmap_kernel(), (vm_offset_t)memhook, 193 (vm_offset_t)memhook + PAGE_SIZE); 194 pmap_update(pmap_kernel()); 195 } 196 197 if (physlock > 1) 198 wakeup((caddr_t)&physlock); 199 physlock = 0; 200 201 return (error); 202} 203 204paddr_t 205ofrommmap(dev, off, prot) 206 dev_t dev; 207 off_t off; 208 int prot; 209{ 210 struct ofrom_softc *sc; 211 int unit = minor(dev); 212 213 if (unit >= ofrom_cd.cd_ndevs) 214 return (-1); /* XXX PANIC */ 215 sc = ofrom_cd.cd_devs[unit]; 216 if (!sc || !sc->enabled) 217 return (-1); /* XXX PANIC */ 218 219 if ((u_int)off >= sc->size) 220 return (-1); 221 222 return arm_btop(sc->base + off); 223} 224