xref: /netbsd/sys/arch/macppc/dev/nvram.c (revision bf9ec67e)
1 /*	$NetBSD: nvram.c,v 1.4 2001/06/08 00:32:02 matt Exp $	*/
2 
3 /*-
4  * Copyright (C) 1998	Internet Research Institute, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
21  *
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.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 
42 #include <machine/autoconf.h>
43 #include <machine/pio.h>
44 
45 #define NVRAM_NONE  0
46 #define NVRAM_IOMEM 1
47 #define NVRAM_PORT  2
48 
49 #define NVRAM_SIZE 0x2000
50 
51 cdev_decl(nvram);
52 
53 static void nvram_attach __P((struct device *, struct device *, void *));
54 static int nvram_match __P((struct device *, struct cfdata *, void *));
55 
56 struct nvram_softc {
57 	struct device sc_dev;
58 	int nv_type;
59 	char *nv_port;
60 	char *nv_data;
61 };
62 
63 struct cfattach nvram_ca = {
64 	sizeof(struct nvram_softc), nvram_match, nvram_attach
65 };
66 
67 extern struct cfdriver nvram_cd;
68 
69 int
70 nvram_match(parent, cf, aux)
71 	struct device *parent;
72 	struct cfdata *cf;
73 	void *aux;
74 {
75 	struct confargs *ca = aux;
76 
77 	if (strcmp(ca->ca_name, "nvram") != 0)
78 		return 0;
79 
80 	if (ca->ca_nreg == 0)
81 		return 0;
82 
83 	return 1;
84 }
85 
86 void
87 nvram_attach(parent, self, aux)
88 	struct device *parent, *self;
89 	void *aux;
90 {
91 	struct nvram_softc *sc = (struct nvram_softc *)self;
92 	struct confargs *ca = aux;
93 	int *reg = ca->ca_reg;
94 
95 	printf("\n");
96 
97 	switch (ca->ca_nreg) {
98 
99 	case 8:						/* untested */
100 		sc->nv_type = NVRAM_IOMEM;
101 		sc->nv_data = mapiodev(ca->ca_baseaddr + reg[0], reg[1]);
102 		break;
103 
104 	case 16:
105 		sc->nv_type = NVRAM_PORT;
106 		sc->nv_port = mapiodev(ca->ca_baseaddr + reg[0], reg[1]);
107 		sc->nv_data = mapiodev(ca->ca_baseaddr + reg[2], reg[3]);
108 		break;
109 
110 	case 0:
111 	default:
112 		sc->nv_type = NVRAM_NONE;
113 		return;
114 	}
115 }
116 
117 int
118 nvramopen(dev, flag, mode, p)
119 	dev_t dev;
120 	int flag, mode;
121 	struct proc *p;
122 {
123 	return 0;
124 }
125 
126 int
127 nvramclose(dev, flag, mode, p)
128 	dev_t dev;
129 	int flag, mode;
130 	struct proc *p;
131 {
132 	return 0;
133 }
134 
135 int
136 nvramread(dev, uio, flag)
137 	dev_t dev;
138 	struct uio *uio;
139 	int flag;
140 {
141 	struct nvram_softc *sc;
142 	u_int off, cnt;
143 	int i;
144 	int error = 0;
145 	char *buf;
146 
147 	sc = nvram_cd.cd_devs[0];
148 
149 	off = uio->uio_offset;
150 	cnt = uio->uio_resid;
151 
152 	if (off > NVRAM_SIZE || cnt > NVRAM_SIZE)
153 		return EFAULT;
154 
155 	if (off + cnt > NVRAM_SIZE)
156 		cnt = NVRAM_SIZE - off;
157 
158 	buf = malloc(NVRAM_SIZE, M_DEVBUF, M_WAITOK);
159 	if (buf == NULL) {
160 		error = EAGAIN;
161 		goto out;
162 	}
163 
164 	switch (sc->nv_type) {
165 
166 	case NVRAM_IOMEM:
167 		for (i = 0; i < NVRAM_SIZE; i++)
168 			buf[i] = sc->nv_data[i * 16];
169 
170 		break;
171 
172 	case NVRAM_PORT:
173 		for (i = 0; i < NVRAM_SIZE; i += 32) {
174 			int j;
175 
176 			out8(sc->nv_port, i / 32);
177 			for (j = 0; j < 32; j++) {
178 				buf[i + j] = sc->nv_data[j * 16];
179 			}
180 		}
181 		break;
182 
183 	default:
184 		goto out;
185 	}
186 
187 	error = uiomove(buf + off, cnt, uio);
188 
189 out:
190 	if (buf)
191 		free(buf, M_DEVBUF);
192 
193 	return error;
194 }
195 
196 int
197 nvramwrite(dev, uio, flag)
198 	dev_t dev;
199 	struct uio *uio;
200 	int flag;
201 {
202 	return ENXIO;
203 }
204 
205 int
206 nvramioctl(dev, cmd, data, flag, p)
207 	dev_t dev;
208 	u_long cmd;
209 	caddr_t data;
210 	int flag;
211 	struct proc *p;
212 {
213 	return ENOTTY;
214 }
215 
216 paddr_t
217 nvrammmap(dev, off, prot)
218         dev_t dev;
219         off_t off;
220 	int prot;
221 {
222 	return -1;
223 }
224