xref: /netbsd-src/sys/arch/x68k/dev/sram.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: sram.c,v 1.10 2003/07/15 01:44:52 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Kazuhisa Shimizu.
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 Kazuhisa Shimizu.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: sram.c,v 1.10 2003/07/15 01:44:52 lukem Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/proc.h>
38 #include <sys/ioctl.h>
39 #include <sys/file.h>
40 #include <sys/malloc.h>
41 #include <sys/systm.h>
42 #include <sys/conf.h>
43 
44 #include <machine/sram.h>
45 #include <x68k/dev/sramvar.h>
46 #include <x68k/x68k/iodevice.h>
47 
48 struct sram_softc sram_softc;
49 
50 #ifdef DEBUG
51 #define SRAM_DEBUG_OPEN		0x01
52 #define SRAM_DEBUG_CLOSE	0x02
53 #define SRAM_DEBUG_IOCTL	0x04
54 #define SRAM_DEBUG_DONTDOIT	0x08
55 int sramdebug = SRAM_DEBUG_IOCTL;
56 #endif
57 
58 void sramattach __P((int));
59 
60 dev_type_open(sramopen);
61 dev_type_close(sramclose);
62 dev_type_ioctl(sramioctl);
63 
64 const struct cdevsw sram_cdevsw = {
65 	sramopen, sramclose, noread, nowrite, sramioctl,
66 	nostop, notty, nopoll, nommap, nokqfilter,
67 };
68 
69 /*
70  *  functions for probeing.
71  */
72 /* ARGSUSED */
73 void
74 sramattach(num)
75 	int num;
76 {
77 	sram_softc.flags = 0;
78 	printf("sram0: 16k bytes accessible\n");
79 }
80 
81 
82 /*
83  *  functions made available by conf.c
84  */
85 
86 /*ARGSUSED*/
87 int
88 sramopen(dev, flags, mode, p)
89 	dev_t dev;
90 	int flags, mode;
91 	struct proc *p;
92 {
93 	struct sram_softc *su = &sram_softc;
94 
95 #ifdef DEBUG
96 	if (sramdebug & SRAM_DEBUG_OPEN)
97 		printf ("Sram open\n");
98 #endif
99 
100 	if (minor(dev) >= 1)
101 		return EXDEV;
102 
103 	if (su->flags & SRF_OPEN) {
104 		return (EBUSY);
105 	}
106 
107 	su->flags |= SRF_OPEN;
108 	if (flags & FREAD)
109 		su->flags |= SRF_READ;
110 	if (flags & FWRITE)
111 		su->flags |= SRF_WRITE;
112 
113 	return (0);
114 }
115 
116 /*ARGSUSED*/
117 int
118 sramclose(dev, flags, mode, p)
119 	dev_t dev;
120 	int flags, mode;
121 	struct proc *p;
122 {
123 	struct sram_softc *su = &sram_softc;
124 
125 #ifdef DEBUG
126 	if (sramdebug & SRAM_DEBUG_CLOSE)
127 		printf ("Sram close\n");
128 #endif
129 
130 	if (su->flags & SRF_OPEN) {
131 		su->flags = 0;
132 	}
133 	su->flags &= ~(SRF_READ|SRF_WRITE);
134 
135 	return (0);
136 }
137 
138 /*ARGSUSED*/
139 int
140 sramioctl (dev, cmd, data, flag, p)
141 	dev_t dev;
142 	u_long cmd;
143 	caddr_t data;
144 	int flag;
145 	struct proc *p;
146 {
147 	int error = 0;
148 	struct sram_io *sram_io;
149 	register char *sramtop = IODEVbase->io_sram;
150 	struct sram_softc *su = &sram_softc;
151 
152 #ifdef DEBUG
153 	if (sramdebug & SRAM_DEBUG_IOCTL)
154 		printf("Sram ioctl cmd=%lx\n", cmd);
155 #endif
156 	sram_io = (struct sram_io *)data;
157 
158 	switch (cmd) {
159 	case SIOGSRAM:
160 		if ((su->flags & SRF_READ) == 0)
161 			return(EPERM);
162 #ifdef DEBUG
163 		if (sramdebug & SRAM_DEBUG_IOCTL) {
164     			printf("Sram ioctl SIOGSRAM address=%p\n", data);
165     			printf("Sram ioctl SIOGSRAM offset=%x\n", sram_io->offset);
166 		}
167 #endif
168 		if (sram_io == NULL ||
169 		    sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
170 			return(EFAULT);
171 		memcpy(&(sram_io->sram), sramtop + sram_io->offset,
172 		    SRAM_IO_SIZE);
173 		break;
174 	case SIOPSRAM:
175 		if ((su->flags & SRF_WRITE) == 0)
176 			return(EPERM);
177 #ifdef DEBUG
178 		if (sramdebug & SRAM_DEBUG_IOCTL) {
179     			printf("Sram ioctl SIOPSRAM address=%p\n", data);
180     			printf("Sram ioctl SIOPSRAM offset=%x\n", sram_io->offset);
181 		}
182 #endif
183 		if (sram_io == NULL ||
184 		    sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
185 			return(EFAULT);
186 #ifdef DEBUG
187 		if (sramdebug & SRAM_DEBUG_DONTDOIT) {
188 			printf ("Sram ioctl SIOPSRAM: skipping actual write\n");
189 			break;
190 		}
191 #endif
192 		sysport.sramwp = 0x31;
193 		memcpy(sramtop + sram_io->offset, &(sram_io->sram),
194 		    SRAM_IO_SIZE);
195 		sysport.sramwp = 0x00;
196 		break;
197 	default:
198 		error = EINVAL;
199 		break;
200 	}
201 	return (error);
202 }
203