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