1 /* $NetBSD: ofrom.c,v 1.19 2009/03/14 15:36:13 dsl Exp $ */
5 * Digital Equipment Corporation. All rights reserved.
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.
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.
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.
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.
37 * XXX open for writing (for user programs to mmap, and write contents)
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ofrom.c,v 1.19 2009/03/14 15:36:13 dsl Exp $");
43 #include <sys/param.h>
44 #include <sys/device.h>
45 #include <sys/systm.h>
47 #include <sys/fcntl.h>
49 #include <uvm/uvm_extern.h>
51 #include <machine/bus.h>
52 #include <dev/ofw/openfirm.h>
61 int ofromprobe(struct device
*, struct cfdata
*, void *);
62 void ofromattach(struct device
*, struct device
*, void *);
64 CFATTACH_DECL(ofrom
, sizeof(struct ofrom_softc
),
65 ofromprobe
, ofromattach
, NULL
, NULL
);
67 extern struct cfdriver ofrom_cd
;
69 dev_type_open(ofromopen
);
70 dev_type_read(ofromrw
);
71 dev_type_mmap(ofrommmap
);
73 const struct cdevsw ofrom_cdevsw
= {
74 ofromopen
, nullclose
, ofromrw
, ofromrw
, noioctl
,
75 nostop
, notty
, nopoll
, ofrommmap
, nokqfilter
,
79 ofromprobe(struct device
*parent
, struct cfdata
*cf
, void *aux
)
81 struct ofbus_attach_args
*oba
= aux
;
82 static const char *const compatible_strings
[] = { "rom", NULL
};
84 return (of_compatible(oba
->oba_phandle
, compatible_strings
) == -1) ?
90 ofromattach(struct device
*parent
, struct device
*self
, void *aux
)
92 struct ofrom_softc
*sc
= (struct ofrom_softc
*)self
;
93 struct ofbus_attach_args
*oba
= aux
;
96 if (OF_getproplen(oba
->oba_phandle
, "reg") != 8) {
97 printf(": invalid reg property\n");
100 if (OF_getprop(oba
->oba_phandle
, "reg", regbuf
, sizeof regbuf
) != 8) {
101 printf(": couldn't read reg property\n");
104 sc
->base
= of_decode_int(®buf
[0]);
105 sc
->size
= of_decode_int(®buf
[4]);
108 printf(": %#lx-%#lx\n", sc
->base
, sc
->base
+ sc
->size
- 1);
112 ofromopen(dev_t dev
, int oflags
, int devtype
, struct lwp
*l
)
114 struct ofrom_softc
*sc
;
116 sc
= device_lookup_private(&ofrom_cd
, minor(dev
));
117 if (!sc
|| !sc
->enabled
)
127 ofromrw(dev_t dev
, struct uio
*uio
, int flags
)
129 struct ofrom_softc
*sc
;
134 extern kmutex_t memlock
;
135 extern char *memhook
;
137 sc
= device_lookup_private(&ofrom_cd
, minor(dev
));
138 if (!sc
|| !sc
->enabled
)
139 return (ENXIO
); /* XXX PANIC */
141 /* lock against other uses of shared vmmap */
142 mutex_enter(&memlock
);
144 while (uio
->uio_resid
> 0 && error
== 0) {
146 if (iov
->iov_len
== 0) {
149 if (uio
->uio_iovcnt
< 0)
155 * Since everything is page aligned and no more
156 * than the rest of a page is done at once, we
157 * can just check that the offset isn't too big.
159 if (uio
->uio_offset
>= sc
->size
)
162 v
= sc
->base
+ uio
->uio_offset
;
163 pmap_enter(pmap_kernel(), (vaddr_t
)memhook
,
164 trunc_page(v
), uio
->uio_rw
== UIO_READ
?
165 VM_PROT_READ
: VM_PROT_WRITE
, PMAP_WIRED
);
166 pmap_update(pmap_kernel());
167 o
= uio
->uio_offset
& PGOFSET
;
168 c
= min(uio
->uio_resid
, (int)(PAGE_SIZE
- o
));
169 error
= uiomove((char *)memhook
+ o
, c
, uio
);
170 pmap_remove(pmap_kernel(), (vaddr_t
)memhook
,
171 (vaddr_t
)memhook
+ PAGE_SIZE
);
172 pmap_update(pmap_kernel());
175 mutex_exit(&memlock
);
181 ofrommmap(dev_t dev
, off_t off
, int prot
)
183 struct ofrom_softc
*sc
;
185 sc
= device_lookup_private(&ofrom_cd
, minor(dev
));
186 if (!sc
|| !sc
->enabled
)
187 return (-1); /* XXX PANIC */
189 if ((u_int
)off
>= sc
->size
)
192 return arm_btop(sc
->base
+ off
);