ofrom.c revision 1.23
1/* $NetBSD: ofrom.c,v 1.23 2011/07/26 08:56:26 mrg 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.23 2011/07/26 08:56:26 mrg 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#include <sys/bus.h> 49 50#include <uvm/uvm_extern.h> 51 52#include <dev/ofw/openfirm.h> 53 54struct ofrom_softc { 55 int enabled; 56 paddr_t base; 57 paddr_t size; 58}; 59 60int ofromprobe(device_t, cfdata_t, void *); 61void ofromattach(device_t, device_t, void *); 62 63CFATTACH_DECL_NEW(ofrom, sizeof(struct ofrom_softc), 64 ofromprobe, ofromattach, NULL, NULL); 65 66extern struct cfdriver ofrom_cd; 67 68dev_type_open(ofromopen); 69dev_type_read(ofromrw); 70dev_type_mmap(ofrommmap); 71 72const struct cdevsw ofrom_cdevsw = { 73 ofromopen, nullclose, ofromrw, ofromrw, noioctl, 74 nostop, notty, nopoll, ofrommmap, nokqfilter, 75}; 76 77int 78ofromprobe(device_t parent, cfdata_t cf, void *aux) 79{ 80 struct ofbus_attach_args *oba = aux; 81 static const char *const compatible_strings[] = { "rom", NULL }; 82 83 return (of_compatible(oba->oba_phandle, compatible_strings) == -1) ? 84 0 : 5; 85} 86 87 88void 89ofromattach(device_t parent, device_t self, void *aux) 90{ 91 struct ofrom_softc *sc = device_private(self); 92 struct ofbus_attach_args *oba = aux; 93 char regbuf[8]; 94 95 if (OF_getproplen(oba->oba_phandle, "reg") != 8) { 96 printf(": invalid reg property\n"); 97 return; 98 } 99 if (OF_getprop(oba->oba_phandle, "reg", regbuf, sizeof regbuf) != 8) { 100 printf(": couldn't read reg property\n"); 101 return; 102 } 103 sc->base = of_decode_int(®buf[0]); 104 sc->size = of_decode_int(®buf[4]); 105 sc->enabled = 1; 106 107 printf(": %#lx-%#lx\n", sc->base, sc->base + sc->size - 1); 108} 109 110int 111ofromopen(dev_t dev, int oflags, int devtype, struct lwp *l) 112{ 113 struct ofrom_softc *sc; 114 115 sc = device_lookup_private(&ofrom_cd, minor(dev)); 116 if (!sc || !sc->enabled) 117 return (ENXIO); 118 119 if (oflags & FWRITE) 120 return (EINVAL); 121 122 return (0); 123} 124 125int 126ofromrw(dev_t dev, struct uio *uio, int flags) 127{ 128 struct ofrom_softc *sc; 129 int c, error = 0; 130 struct iovec *iov; 131 paddr_t v; 132 psize_t o; 133 extern kmutex_t memlock; 134 extern char *memhook; 135 136 sc = device_lookup_private(&ofrom_cd, minor(dev)); 137 if (!sc || !sc->enabled) 138 return (ENXIO); /* XXX PANIC */ 139 140 mutex_enter(&memlock); 141 while (uio->uio_resid > 0 && error == 0) { 142 iov = uio->uio_iov; 143 if (iov->iov_len == 0) { 144 uio->uio_iov++; 145 uio->uio_iovcnt--; 146 if (uio->uio_iovcnt < 0) 147 panic("ofromrw"); 148 continue; 149 } 150 151 /* 152 * Since everything is page aligned and no more 153 * than the rest of a page is done at once, we 154 * can just check that the offset isn't too big. 155 */ 156 if (uio->uio_offset >= sc->size) 157 break; 158 159 /* XXX: Use unamanged mapping. */ 160 v = sc->base + uio->uio_offset; 161 pmap_enter(pmap_kernel(), (vaddr_t)memhook, 162 trunc_page(v), uio->uio_rw == UIO_READ ? 163 VM_PROT_READ : VM_PROT_WRITE, PMAP_WIRED); 164 pmap_update(pmap_kernel()); 165 o = uio->uio_offset & PGOFSET; 166 c = min(uio->uio_resid, (int)(PAGE_SIZE - o)); 167 error = uiomove((char *)memhook + o, c, uio); 168 pmap_remove(pmap_kernel(), (vaddr_t)memhook, 169 (vaddr_t)memhook + PAGE_SIZE); 170 pmap_update(pmap_kernel()); 171 } 172 mutex_exit(&memlock); 173 174 return (error); 175} 176 177paddr_t 178ofrommmap(dev_t dev, off_t off, int prot) 179{ 180 struct ofrom_softc *sc; 181 182 sc = device_lookup_private(&ofrom_cd, minor(dev)); 183 if (!sc || !sc->enabled) 184 return (-1); /* XXX PANIC */ 185 186 if ((u_int)off >= sc->size) 187 return (-1); 188 189 return arm_btop(sc->base + off); 190} 191