xref: /netbsd/sys/arch/x68k/dev/sram.c (revision c4a72b64)
1 /*	$NetBSD: sram.c,v 1.9 2002/10/23 09:12:48 jdolecek 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/param.h>
34 #include <sys/proc.h>
35 #include <sys/ioctl.h>
36 #include <sys/file.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 
41 #include <machine/sram.h>
42 #include <x68k/dev/sramvar.h>
43 #include <x68k/x68k/iodevice.h>
44 
45 struct sram_softc sram_softc;
46 
47 #ifdef DEBUG
48 #define SRAM_DEBUG_OPEN		0x01
49 #define SRAM_DEBUG_CLOSE	0x02
50 #define SRAM_DEBUG_IOCTL	0x04
51 #define SRAM_DEBUG_DONTDOIT	0x08
52 int sramdebug = SRAM_DEBUG_IOCTL;
53 #endif
54 
55 void sramattach __P((int));
56 
57 dev_type_open(sramopen);
58 dev_type_close(sramclose);
59 dev_type_ioctl(sramioctl);
60 
61 const struct cdevsw sram_cdevsw = {
62 	sramopen, sramclose, noread, nowrite, sramioctl,
63 	nostop, notty, nopoll, nommap, nokqfilter,
64 };
65 
66 /*
67  *  functions for probeing.
68  */
69 /* ARGSUSED */
70 void
71 sramattach(num)
72 	int num;
73 {
74 	sram_softc.flags = 0;
75 	printf("sram0: 16k bytes accessible\n");
76 }
77 
78 
79 /*
80  *  functions made available by conf.c
81  */
82 
83 /*ARGSUSED*/
84 int
85 sramopen(dev, flags, mode, p)
86 	dev_t dev;
87 	int flags, mode;
88 	struct proc *p;
89 {
90 	struct sram_softc *su = &sram_softc;
91 
92 #ifdef DEBUG
93 	if (sramdebug & SRAM_DEBUG_OPEN)
94 		printf ("Sram open\n");
95 #endif
96 
97 	if (minor(dev) >= 1)
98 		return EXDEV;
99 
100 	if (su->flags & SRF_OPEN) {
101 		return (EBUSY);
102 	}
103 
104 	su->flags |= SRF_OPEN;
105 	if (flags & FREAD)
106 		su->flags |= SRF_READ;
107 	if (flags & FWRITE)
108 		su->flags |= SRF_WRITE;
109 
110 	return (0);
111 }
112 
113 /*ARGSUSED*/
114 int
115 sramclose(dev, flags, mode, p)
116 	dev_t dev;
117 	int flags, mode;
118 	struct proc *p;
119 {
120 	struct sram_softc *su = &sram_softc;
121 
122 #ifdef DEBUG
123 	if (sramdebug & SRAM_DEBUG_CLOSE)
124 		printf ("Sram close\n");
125 #endif
126 
127 	if (su->flags & SRF_OPEN) {
128 		su->flags = 0;
129 	}
130 	su->flags &= ~(SRF_READ|SRF_WRITE);
131 
132 	return (0);
133 }
134 
135 /*ARGSUSED*/
136 int
137 sramioctl (dev, cmd, data, flag, p)
138 	dev_t dev;
139 	u_long cmd;
140 	caddr_t data;
141 	int flag;
142 	struct proc *p;
143 {
144 	int error = 0;
145 	struct sram_io *sram_io;
146 	register char *sramtop = IODEVbase->io_sram;
147 	struct sram_softc *su = &sram_softc;
148 
149 #ifdef DEBUG
150 	if (sramdebug & SRAM_DEBUG_IOCTL)
151 		printf("Sram ioctl cmd=%lx\n", cmd);
152 #endif
153 	sram_io = (struct sram_io *)data;
154 
155 	switch (cmd) {
156 	case SIOGSRAM:
157 		if ((su->flags & SRF_READ) == 0)
158 			return(EPERM);
159 #ifdef DEBUG
160 		if (sramdebug & SRAM_DEBUG_IOCTL) {
161     			printf("Sram ioctl SIOGSRAM address=%p\n", data);
162     			printf("Sram ioctl SIOGSRAM offset=%x\n", sram_io->offset);
163 		}
164 #endif
165 		if (sram_io == NULL ||
166 		    sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
167 			return(EFAULT);
168 		memcpy(&(sram_io->sram), sramtop + sram_io->offset,
169 		    SRAM_IO_SIZE);
170 		break;
171 	case SIOPSRAM:
172 		if ((su->flags & SRF_WRITE) == 0)
173 			return(EPERM);
174 #ifdef DEBUG
175 		if (sramdebug & SRAM_DEBUG_IOCTL) {
176     			printf("Sram ioctl SIOPSRAM address=%p\n", data);
177     			printf("Sram ioctl SIOPSRAM offset=%x\n", sram_io->offset);
178 		}
179 #endif
180 		if (sram_io == NULL ||
181 		    sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
182 			return(EFAULT);
183 #ifdef DEBUG
184 		if (sramdebug & SRAM_DEBUG_DONTDOIT) {
185 			printf ("Sram ioctl SIOPSRAM: skipping actual write\n");
186 			break;
187 		}
188 #endif
189 		sysport.sramwp = 0x31;
190 		memcpy(sramtop + sram_io->offset, &(sram_io->sram),
191 		    SRAM_IO_SIZE);
192 		sysport.sramwp = 0x00;
193 		break;
194 	default:
195 		error = EINVAL;
196 		break;
197 	}
198 	return (error);
199 }
200