nvram_pnpbus.c revision 1.5 1 /* $NetBSD: nvram_pnpbus.c,v 1.5 2006/10/30 17:54:29 garbled Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tim Rightnour
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: nvram_pnpbus.c,v 1.5 2006/10/30 17:54:29 garbled Exp $");
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/ioctl.h>
46 #include <sys/conf.h>
47 #include <sys/kthread.h>
48 #include <sys/device.h>
49 #include <sys/malloc.h>
50 #include <sys/lock.h>
51
52 #include <machine/bus.h>
53 #include <machine/intr.h>
54 #include <machine/isa_machdep.h>
55 /* clock stuff for motorolla machines */
56 #include <dev/clock_subr.h>
57 #include <dev/ic/mk48txxreg.h>
58
59 #include <machine/nvram.h>
60
61 #include <prep/pnpbus/pnpbusvar.h>
62
63 #include "opt_nvram.h"
64
65 static char *nvramData;
66 static NVRAM_MAP *nvram;
67 static char *nvramGEAp; /* pointer to the GE area */
68 static char *nvramCAp; /* pointer to the Config area */
69 static char *nvramOSAp; /* pointer to the OSArea */
70 struct simplelock nvram_slock; /* lock */
71
72 int prep_clock_mk48txx;
73
74 extern char bootpath[256];
75
76 #define NVRAM_STD_DEV 0
77
78 static int nvram_pnpbus_probe(struct device *, struct cfdata *, void *);
79 static void nvram_pnpbus_attach(struct device *, struct device *, void *);
80 uint8_t prep_nvram_read_val(int);
81 char *prep_nvram_next_var(char *);
82 char *prep_nvram_find_var(const char *);
83 char *prep_nvram_get_var(const char *);
84 int prep_nvram_get_var_len(const char *);
85 int prep_nvram_count_vars(void);
86 void prep_nvram_write_val(int, uint8_t);
87 uint8_t mkclock_pnpbus_nvrd(struct mk48txx_softc *, int);
88 void mkclock_pnpbus_nvwr(struct mk48txx_softc *, int, uint8_t);
89
90 CFATTACH_DECL(nvram_pnpbus, sizeof(struct nvram_pnpbus_softc),
91 nvram_pnpbus_probe, nvram_pnpbus_attach, NULL, NULL);
92
93 dev_type_open(prep_nvramopen);
94 dev_type_ioctl(prep_nvramioctl);
95 dev_type_close(prep_nvramclose);
96
97 const struct cdevsw prep_nvram_cdevsw = {
98 prep_nvramopen, prep_nvramclose, noread, nowrite, prep_nvramioctl,
99 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
100 };
101
102 extern struct cfdriver nvram_cd;
103
104 static int
105 nvram_pnpbus_probe(struct device *parent, struct cfdata *match, void *aux)
106 {
107 struct pnpbus_dev_attach_args *pna = aux;
108 int ret = 0;
109
110 if (strcmp(pna->pna_devid, "IBM0008") == 0)
111 ret = 1;
112
113 if (ret)
114 pnpbus_scan(pna, pna->pna_ppc_dev);
115
116 return ret;
117 }
118
119 static void
120 nvram_pnpbus_attach(struct device *parent, struct device *self, void *aux)
121 {
122 struct nvram_pnpbus_softc *sc = (void *)self;
123 struct pnpbus_dev_attach_args *pna = aux;
124 int as_iobase, as_len, data_iobase, data_len, i, nvlen, cur;
125 uint8_t *p;
126 HEADER prep_nvram_header;
127
128 sc->sc_iot = pna->pna_iot;
129
130 pnpbus_getioport(&pna->pna_res, 0, &as_iobase, &as_len);
131 pnpbus_getioport(&pna->pna_res, 1, &data_iobase, &data_len);
132
133 if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_as, &sc->sc_ash) ||
134 pnpbus_io_map(&pna->pna_res, 1, &sc->sc_data, &sc->sc_datah)) {
135 aprint_error("nvram: couldn't map registers\n");
136 return;
137 }
138
139 simple_lock_init(&nvram_slock);
140
141 /* Initialize the nvram header */
142 p = (uint8_t *) &prep_nvram_header;
143 for (i = 0; i < sizeof(HEADER); i++)
144 *p++ = prep_nvram_read_val(i);
145
146 /*
147 * now that we have the header, we know how big the NVRAM part on
148 * this machine really is. Malloc space to save a copy.
149 */
150
151 nvlen = 1024 * prep_nvram_header.Size;
152 nvramData = malloc(nvlen, M_DEVBUF, M_NOWAIT);
153 p = (uint8_t *) nvramData;
154
155 /*
156 * now read the whole nvram in, one chunk at a time, marking down
157 * the main start points as we go.
158 */
159 for (i = 0; i < sizeof(HEADER) && i < nvlen; i++)
160 *p++ = prep_nvram_read_val(i);
161 nvramGEAp = p;
162 cur = i;
163 for (; i < cur + prep_nvram_header.GELength && i < nvlen; i++)
164 *p++ = prep_nvram_read_val(i);
165 nvramOSAp = p;
166 cur = i;
167 for (; i < cur + prep_nvram_header.OSAreaLength && i < nvlen; i++)
168 *p++ = prep_nvram_read_val(i);
169 nvramCAp = p;
170 cur = i;
171 for (; i < cur + prep_nvram_header.ConfigLength && i < nvlen; i++)
172 *p++ = prep_nvram_read_val(i);
173
174 /* we should be done here. umm.. yay? */
175 nvram = (NVRAM_MAP *)&nvramData[0];
176 aprint_normal("\n");
177 aprint_verbose("%s: Read %d bytes from nvram of size %d\n",
178 device_xname(self), i, nvlen);
179
180 #if defined(NVRAM_DUMP)
181 printf("Boot device: %s\n", prep_nvram_get_var("fw-boot-device"));
182 printf("Dumping nvram\n");
183 for (cur=0; cur < i; cur++) {
184 printf("%c", nvramData[cur]);
185 if (cur % 70 == 0)
186 printf("\n");
187 }
188 #endif
189 strncpy(bootpath, prep_nvram_get_var("fw-boot-device"), 256);
190
191
192 if (prep_clock_mk48txx == 0)
193 return;
194 /* otherwise, we have a motorolla clock chip. Set it up. */
195 sc->sc_mksc.sc_model = "mk48t18";
196 sc->sc_mksc.sc_year0 = 1900;
197 sc->sc_mksc.sc_nvrd = mkclock_pnpbus_nvrd;
198 sc->sc_mksc.sc_nvwr = mkclock_pnpbus_nvwr;
199 /* copy down the bus space tags */
200 sc->sc_mksc.sc_bst = sc->sc_as;
201 sc->sc_mksc.sc_bsh = sc->sc_ash;
202 sc->sc_mksc.sc_data = sc->sc_data;
203 sc->sc_mksc.sc_datah = sc->sc_datah;
204
205 aprint_normal("%s: attaching clock", device_xname(self));
206 mk48txx_attach((struct mk48txx_softc *)&sc->sc_mksc);
207 aprint_normal("\n");
208 todr_attach(&sc->sc_mksc.sc_handle);
209 }
210
211 /*
212 * This function should be called at a high spl only, as it interfaces with
213 * real hardware.
214 */
215
216 uint8_t
217 prep_nvram_read_val(int addr)
218 {
219 struct nvram_pnpbus_softc *sc;
220
221 if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
222 || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
223 return 0;
224 }
225
226 sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
227
228 /* tell the NVRAM what we want */
229 bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
230 bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
231
232 return bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
233 }
234
235 /*
236 * This function should be called at a high spl only, as it interfaces with
237 * real hardware.
238 */
239
240 void
241 prep_nvram_write_val(int addr, uint8_t val)
242 {
243 struct nvram_pnpbus_softc *sc;
244
245 if (nvram_cd.cd_devs == NULL || nvram_cd.cd_ndevs == 0
246 || nvram_cd.cd_devs[NVRAM_STD_DEV] == NULL) {
247 return;
248 }
249
250 sc = (struct nvram_pnpbus_softc *) nvram_cd.cd_devs[NVRAM_STD_DEV];
251
252 /* tell the NVRAM what we want */
253 bus_space_write_1(sc->sc_as, sc->sc_ash, 0, addr);
254 bus_space_write_1(sc->sc_as, sc->sc_ash, 1, addr>>8);
255
256 bus_space_write_1(sc->sc_data, sc->sc_datah, 0, val);
257 }
258
259 /* the rest of these should all be called with the lock held */
260
261 char *
262 prep_nvram_next_var(char *name)
263 {
264 char *cp;
265
266 cp = name;
267 /* skip forward to the first null char */
268 while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp != '\0'))
269 cp++;
270 /* skip nulls */
271 while ((cp - nvramGEAp) < nvram->Header.GELength && (*cp == '\0'))
272 cp++;
273 if ((cp - nvramGEAp) < nvram->Header.GELength)
274 return cp;
275 else
276 return NULL;
277 }
278
279 char *
280 prep_nvram_find_var(const char *name)
281 {
282 char *cp = nvramGEAp;
283 size_t len;
284
285 len = strlen(name);
286 while (cp != NULL) {
287 if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
288 return cp;
289 cp = prep_nvram_next_var(cp);
290 }
291 return NULL;
292 }
293
294 char *
295 prep_nvram_get_var(const char *name)
296 {
297 char *cp = nvramGEAp;
298 size_t len;
299
300 len = strlen(name);
301 while (cp != NULL) {
302 if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
303 return cp+len+1;
304 cp = prep_nvram_next_var(cp);
305 }
306 return NULL;
307 }
308
309 int
310 prep_nvram_get_var_len(const char *name)
311 {
312 char *cp = nvramGEAp;
313 char *ep;
314 size_t len;
315
316 len = strlen(name);
317 while (cp != NULL) {
318 if ((strncmp(name, cp, len) == 0) && (cp[len] == '='))
319 goto out;
320 cp = prep_nvram_next_var(cp);
321 }
322 return -1;
323
324 out:
325 ep = cp;
326 while (ep != NULL && *ep != '\0')
327 ep++;
328 return ep-cp;
329 }
330
331 int
332 prep_nvram_count_vars(void)
333 {
334 char *cp = nvramGEAp;
335 int i=0;
336
337 while (cp != NULL) {
338 i++;
339 cp = prep_nvram_next_var(cp);
340 }
341 return i;
342 }
343
344 int
345 prep_nvramioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct lwp *l)
346 {
347 int len, error;
348 struct pnviocdesc *pnv;
349 char *np, *cp;
350
351 pnv = (struct pnviocdesc *)data;
352 error = 0;
353
354 switch (cmd) {
355 case PNVIOCGET:
356 if (pnv->pnv_name == NULL)
357 return EINVAL;
358
359 simple_lock(&nvram_slock);
360 np = prep_nvram_get_var(pnv->pnv_name);
361 simple_unlock(&nvram_slock);
362 if (np == NULL)
363 return EINVAL;
364 simple_lock(&nvram_slock);
365 len = prep_nvram_get_var_len(pnv->pnv_name);
366 simple_unlock(&nvram_slock);
367
368 if (len > pnv->pnv_buflen) {
369 error = ENOMEM;
370 break;
371 }
372 if (len <= 0)
373 break;
374 error = copyout(np, pnv->pnv_buf, len);
375 pnv->pnv_buflen = len;
376 break;
377
378 case PNVIOCGETNEXTNAME:
379 /* if the first one is null, we give them the first name */
380 simple_lock(&nvram_slock);
381 if (pnv->pnv_name == NULL) {
382 np = nvramGEAp;
383 cp = prep_nvram_next_var(np);
384 } else {
385 np = prep_nvram_find_var(pnv->pnv_name);
386 cp = prep_nvram_next_var(np);
387 }
388 simple_unlock(&nvram_slock);
389 np = cp;
390 while (*np != '=')
391 np++;
392 len = np-cp;
393 if (len > pnv->pnv_buflen) {
394 error = ENOMEM;
395 break;
396 }
397 error = copyout(cp, pnv->pnv_buf, len);
398 pnv->pnv_buflen = len;
399 break;
400 default:
401 return (ENOTTY);
402 }
403 return (error);
404 }
405
406 int
407 prep_nvramopen(dev_t dev, int flags, int mode, struct lwp *l)
408 {
409 struct nvram_pnpbus_softc *sc;
410
411 sc = device_lookup(&nvram_cd, dev);
412 if (sc == NULL)
413 return ENODEV;
414
415 if (sc->sc_open)
416 return EBUSY;
417
418 sc->sc_open = 1;
419
420 return 0;
421 }
422
423 int
424 prep_nvramclose(dev_t dev, int flags, int mode, struct lwp *l)
425 {
426 struct nvram_pnpbus_softc *sc;
427
428 sc = device_lookup(&nvram_cd, dev);
429 if (sc == NULL)
430 return ENODEV;
431 sc->sc_open = 0;
432 return 0;
433 }
434
435 /* Motorola mk48txx clock routines */
436 uint8_t
437 mkclock_pnpbus_nvrd(struct mk48txx_softc *osc, int off)
438 {
439 struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
440 uint8_t datum;
441 int s;
442
443 #ifdef DEBUG
444 printf("mkclock_pnpbus_nvrd(%d)", off);
445 #endif
446 s = splclock();
447 bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
448 bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
449 datum = bus_space_read_1(sc->sc_data, sc->sc_datah, 0);
450 splx(s);
451 #ifdef DEBUG
452 printf(" -> %02x\n", datum);
453 #endif
454 return datum;
455 }
456
457 void
458 mkclock_pnpbus_nvwr(struct mk48txx_softc *osc, int off, uint8_t datum)
459 {
460 struct prep_mk48txx_softc *sc = (struct prep_mk48txx_softc *)osc;
461 int s;
462
463 #ifdef DEBUG
464 printf("mkclock_isa_nvwr(%d, %02x)\n", off, datum);
465 #endif
466 s = splclock();
467 bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, off & 0xff);
468 bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, off >> 8);
469 bus_space_write_1(sc->sc_data, sc->sc_datah, 0, datum);
470 splx(s);
471 }
472