1 /* $NetBSD: nvram.c,v 1.14 2009/03/14 15:36:09 dsl Exp $ */
4 * Copyright (C) 1998 Internet Research Institute, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by
18 * Internet Research Institute, Inc.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: nvram.c,v 1.14 2009/03/14 15:36:09 dsl Exp $");
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/malloc.h>
44 #include <sys/event.h>
46 #include <machine/autoconf.h>
47 #include <machine/pio.h>
53 #define NVRAM_SIZE 0x2000
55 static void nvram_attach(struct device
*, struct device
*, void *);
56 static int nvram_match(struct device
*, struct cfdata
*, void *);
65 CFATTACH_DECL(nvram
, sizeof(struct nvram_softc
),
66 nvram_match
, nvram_attach
, NULL
, NULL
);
68 extern struct cfdriver nvram_cd
;
70 dev_type_read(nvramread
);
71 dev_type_write(nvramwrite
);
72 dev_type_mmap(nvrammmap
);
74 const struct cdevsw nvram_cdevsw
= {
75 nullopen
, nullclose
, nvramread
, nvramwrite
, noioctl
,
76 nostop
, notty
, nopoll
, nvrammmap
, nokqfilter
,
80 nvram_match(struct device
*parent
, struct cfdata
*cf
, void *aux
)
82 struct confargs
*ca
= aux
;
84 if (strcmp(ca
->ca_name
, "nvram") != 0)
94 nvram_attach(struct device
*parent
, struct device
*self
, void *aux
)
96 struct nvram_softc
*sc
= (struct nvram_softc
*)self
;
97 struct confargs
*ca
= aux
;
98 int *reg
= ca
->ca_reg
;
102 switch (ca
->ca_nreg
) {
104 case 8: /* untested */
105 sc
->nv_type
= NVRAM_IOMEM
;
106 sc
->nv_data
= mapiodev(ca
->ca_baseaddr
+ reg
[0], reg
[1]);
110 sc
->nv_type
= NVRAM_PORT
;
111 sc
->nv_port
= mapiodev(ca
->ca_baseaddr
+ reg
[0], reg
[1]);
112 sc
->nv_data
= mapiodev(ca
->ca_baseaddr
+ reg
[2], reg
[3]);
117 sc
->nv_type
= NVRAM_NONE
;
123 nvramread(dev_t dev
, struct uio
*uio
, int flag
)
125 struct nvram_softc
*sc
;
131 sc
= device_lookup_private(&nvram_cd
, 0);
133 off
= uio
->uio_offset
;
134 cnt
= uio
->uio_resid
;
136 if (off
> NVRAM_SIZE
|| cnt
> NVRAM_SIZE
)
139 if (off
+ cnt
> NVRAM_SIZE
)
140 cnt
= NVRAM_SIZE
- off
;
142 buf
= malloc(NVRAM_SIZE
, M_DEVBUF
, M_WAITOK
);
148 switch (sc
->nv_type
) {
151 for (i
= 0; i
< NVRAM_SIZE
; i
++)
152 buf
[i
] = sc
->nv_data
[i
* 16];
157 for (i
= 0; i
< NVRAM_SIZE
; i
+= 32) {
160 out8(sc
->nv_port
, i
/ 32);
161 for (j
= 0; j
< 32; j
++) {
162 buf
[i
+ j
] = sc
->nv_data
[j
* 16];
171 error
= uiomove(buf
+ off
, cnt
, uio
);
181 nvramwrite(dev_t dev
, struct uio
*uio
, int flag
)
187 nvrammmap(dev_t dev
, off_t off
, int prot
)