xref: /netbsd-src/sys/arch/macppc/dev/nvram.c (revision f9228f42259a421502f04b1ddb2df91c2ecbc519)
1*f9228f42Sdholland /*	$NetBSD: nvram.c,v 1.20 2014/07/25 08:10:34 dholland Exp $	*/
221f84e91Stsubai 
321f84e91Stsubai /*-
421f84e91Stsubai  * Copyright (C) 1998	Internet Research Institute, Inc.
521f84e91Stsubai  * All rights reserved.
621f84e91Stsubai  *
721f84e91Stsubai  * Redistribution and use in source and binary forms, with or without
821f84e91Stsubai  * modification, are permitted provided that the following conditions
921f84e91Stsubai  * are met:
1021f84e91Stsubai  * 1. Redistributions of source code must retain the above copyright
1121f84e91Stsubai  *    notice, this list of conditions and the following disclaimer.
1221f84e91Stsubai  * 2. Redistributions in binary form must reproduce the above copyright
1321f84e91Stsubai  *    notice, this list of conditions and the following disclaimer in the
1421f84e91Stsubai  *    documentation and/or other materials provided with the distribution.
1521f84e91Stsubai  * 3. All advertising materials mentioning features or use of this software
1621f84e91Stsubai  *    must display the following acknowledgement:
1721f84e91Stsubai  *	This product includes software developed by
1821f84e91Stsubai  *	Internet Research Institute, Inc.
1921f84e91Stsubai  * 4. The name of the author may not be used to endorse or promote products
2021f84e91Stsubai  *    derived from this software without specific prior written permission.
2121f84e91Stsubai  *
2221f84e91Stsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2321f84e91Stsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2421f84e91Stsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2521f84e91Stsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2621f84e91Stsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2721f84e91Stsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2821f84e91Stsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2921f84e91Stsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3021f84e91Stsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3121f84e91Stsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3221f84e91Stsubai  */
3321f84e91Stsubai 
344b2744bfSlukem #include <sys/cdefs.h>
35*f9228f42Sdholland __KERNEL_RCSID(0, "$NetBSD: nvram.c,v 1.20 2014/07/25 08:10:34 dholland Exp $");
364b2744bfSlukem 
3721f84e91Stsubai #include <sys/types.h>
3821f84e91Stsubai #include <sys/param.h>
3921f84e91Stsubai #include <sys/systm.h>
40d2f0c68dSmatt #include <sys/conf.h>
4121f84e91Stsubai #include <sys/kernel.h>
4221f84e91Stsubai #include <sys/device.h>
4321f84e91Stsubai #include <sys/malloc.h>
44e0cc03a0Sjdolecek #include <sys/event.h>
4521f84e91Stsubai 
4621f84e91Stsubai #include <machine/autoconf.h>
4721f84e91Stsubai #include <machine/pio.h>
4821f84e91Stsubai 
4921f84e91Stsubai #define NVRAM_NONE  0
5021f84e91Stsubai #define NVRAM_IOMEM 1
5121f84e91Stsubai #define NVRAM_PORT  2
5221f84e91Stsubai 
5321f84e91Stsubai #define NVRAM_SIZE 0x2000
5421f84e91Stsubai 
5505b09539Smatt static void nvram_attach(device_t, device_t, void *);
5605b09539Smatt static int nvram_match(device_t, cfdata_t, void *);
5721f84e91Stsubai 
5821f84e91Stsubai struct nvram_softc {
5921f84e91Stsubai 	int nv_type;
6021f84e91Stsubai 	char *nv_port;
6121f84e91Stsubai 	char *nv_data;
6221f84e91Stsubai };
6321f84e91Stsubai 
64cbab9cadSchs CFATTACH_DECL_NEW(nvram, sizeof(struct nvram_softc),
65c5e91d44Sthorpej     nvram_match, nvram_attach, NULL, NULL);
6621f84e91Stsubai 
6721f84e91Stsubai extern struct cfdriver nvram_cd;
6821f84e91Stsubai 
6977a6b82bSgehenna dev_type_read(nvramread);
7077a6b82bSgehenna dev_type_write(nvramwrite);
7177a6b82bSgehenna dev_type_mmap(nvrammmap);
7277a6b82bSgehenna 
7377a6b82bSgehenna const struct cdevsw nvram_cdevsw = {
74a68f9396Sdholland 	.d_open = nullopen,
75a68f9396Sdholland 	.d_close = nullclose,
76a68f9396Sdholland 	.d_read = nvramread,
77a68f9396Sdholland 	.d_write = nvramwrite,
78a68f9396Sdholland 	.d_ioctl = noioctl,
79a68f9396Sdholland 	.d_stop = nostop,
80a68f9396Sdholland 	.d_tty = notty,
81a68f9396Sdholland 	.d_poll = nopoll,
82a68f9396Sdholland 	.d_mmap = nvrammmap,
83a68f9396Sdholland 	.d_kqfilter = nokqfilter,
84*f9228f42Sdholland 	.d_discard = nodiscard,
85a68f9396Sdholland 	.d_flag = 0
8677a6b82bSgehenna };
8777a6b82bSgehenna 
8821f84e91Stsubai int
nvram_match(device_t parent,cfdata_t cf,void * aux)8905b09539Smatt nvram_match(device_t parent, cfdata_t cf, void *aux)
9021f84e91Stsubai {
9121f84e91Stsubai 	struct confargs *ca = aux;
9221f84e91Stsubai 
9321f84e91Stsubai 	if (strcmp(ca->ca_name, "nvram") != 0)
9421f84e91Stsubai 		return 0;
9521f84e91Stsubai 
9621f84e91Stsubai 	if (ca->ca_nreg == 0)
9721f84e91Stsubai 		return 0;
9821f84e91Stsubai 
9921f84e91Stsubai 	return 1;
10021f84e91Stsubai }
10121f84e91Stsubai 
10221f84e91Stsubai void
nvram_attach(device_t parent,device_t self,void * aux)10305b09539Smatt nvram_attach(device_t parent, device_t self, void *aux)
10421f84e91Stsubai {
10505b09539Smatt 	struct nvram_softc *sc = device_private(self);
10621f84e91Stsubai 	struct confargs *ca = aux;
10721f84e91Stsubai 	int *reg = ca->ca_reg;
10821f84e91Stsubai 
10921f84e91Stsubai 	printf("\n");
11021f84e91Stsubai 
11121f84e91Stsubai 	switch (ca->ca_nreg) {
11221f84e91Stsubai 
11321f84e91Stsubai 	case 8:						/* untested */
11421f84e91Stsubai 		sc->nv_type = NVRAM_IOMEM;
1154a40b014Smatt 		sc->nv_data = mapiodev(ca->ca_baseaddr + reg[0], reg[1], false);
11621f84e91Stsubai 		break;
11721f84e91Stsubai 
11821f84e91Stsubai 	case 16:
11921f84e91Stsubai 		sc->nv_type = NVRAM_PORT;
1204a40b014Smatt 		sc->nv_port = mapiodev(ca->ca_baseaddr + reg[0], reg[1], false);
1214a40b014Smatt 		sc->nv_data = mapiodev(ca->ca_baseaddr + reg[2], reg[3], false);
12221f84e91Stsubai 		break;
12321f84e91Stsubai 
12421f84e91Stsubai 	case 0:
12521f84e91Stsubai 	default:
12621f84e91Stsubai 		sc->nv_type = NVRAM_NONE;
12721f84e91Stsubai 		return;
12821f84e91Stsubai 	}
12921f84e91Stsubai }
13021f84e91Stsubai 
13121f84e91Stsubai int
nvramread(dev_t dev,struct uio * uio,int flag)1322dace402Scegger nvramread(dev_t dev, struct uio *uio, int flag)
13321f84e91Stsubai {
13421f84e91Stsubai 	struct nvram_softc *sc;
13521f84e91Stsubai 	u_int off, cnt;
13621f84e91Stsubai 	int i;
13721f84e91Stsubai 	int error = 0;
13821f84e91Stsubai 	char *buf;
13921f84e91Stsubai 
1402dace402Scegger 	sc = device_lookup_private(&nvram_cd, 0);
14121f84e91Stsubai 
14221f84e91Stsubai 	off = uio->uio_offset;
14321f84e91Stsubai 	cnt = uio->uio_resid;
14421f84e91Stsubai 
14521f84e91Stsubai 	if (off > NVRAM_SIZE || cnt > NVRAM_SIZE)
14621f84e91Stsubai 		return EFAULT;
14721f84e91Stsubai 
14821f84e91Stsubai 	if (off + cnt > NVRAM_SIZE)
14921f84e91Stsubai 		cnt = NVRAM_SIZE - off;
15021f84e91Stsubai 
15121f84e91Stsubai 	buf = malloc(NVRAM_SIZE, M_DEVBUF, M_WAITOK);
15221f84e91Stsubai 	if (buf == NULL) {
15321f84e91Stsubai 		error = EAGAIN;
15421f84e91Stsubai 		goto out;
15521f84e91Stsubai 	}
15621f84e91Stsubai 
15721f84e91Stsubai 	switch (sc->nv_type) {
15821f84e91Stsubai 
15921f84e91Stsubai 	case NVRAM_IOMEM:
16021f84e91Stsubai 		for (i = 0; i < NVRAM_SIZE; i++)
16121f84e91Stsubai 			buf[i] = sc->nv_data[i * 16];
16221f84e91Stsubai 
16321f84e91Stsubai 		break;
16421f84e91Stsubai 
16521f84e91Stsubai 	case NVRAM_PORT:
16621f84e91Stsubai 		for (i = 0; i < NVRAM_SIZE; i += 32) {
16721f84e91Stsubai 			int j;
16821f84e91Stsubai 
16921f84e91Stsubai 			out8(sc->nv_port, i / 32);
17021f84e91Stsubai 			for (j = 0; j < 32; j++) {
17121f84e91Stsubai 				buf[i + j] = sc->nv_data[j * 16];
17221f84e91Stsubai 			}
17321f84e91Stsubai 		}
17421f84e91Stsubai 		break;
17521f84e91Stsubai 
17621f84e91Stsubai 	default:
17721f84e91Stsubai 		goto out;
17821f84e91Stsubai 	}
17921f84e91Stsubai 
18021f84e91Stsubai 	error = uiomove(buf + off, cnt, uio);
18121f84e91Stsubai 
18221f84e91Stsubai out:
18321f84e91Stsubai 	if (buf)
18421f84e91Stsubai 		free(buf, M_DEVBUF);
18521f84e91Stsubai 
18621f84e91Stsubai 	return error;
18721f84e91Stsubai }
18821f84e91Stsubai 
18921f84e91Stsubai int
nvramwrite(dev_t dev,struct uio * uio,int flag)190454af1c0Sdsl nvramwrite(dev_t dev, struct uio *uio, int flag)
19121f84e91Stsubai {
19221f84e91Stsubai 	return ENXIO;
19321f84e91Stsubai }
19421f84e91Stsubai 
195889c658bSsimonb paddr_t
nvrammmap(dev_t dev,off_t off,int prot)196454af1c0Sdsl nvrammmap(dev_t dev, off_t off, int prot)
19721f84e91Stsubai {
198db3051d7Smrg 	return -1;
19921f84e91Stsubai }
200