xref: /netbsd-src/sys/arch/x68k/dev/sram.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: sram.c,v 1.18 2008/12/21 09:01:19 isaki 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.18 2008/12/21 09:01:19 isaki Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/ioctl.h>
40 #include <sys/file.h>
41 #include <sys/conf.h>
42 #include <sys/bus.h>
43 #include <sys/device.h>
44 
45 #include <machine/sram.h>
46 #include <x68k/dev/intiovar.h>
47 #include <x68k/dev/sramvar.h>
48 
49 #define SRAM_ADDR	(0xed0000)
50 
51 #ifdef DEBUG
52 #define SRAM_DEBUG_OPEN		0x01
53 #define SRAM_DEBUG_CLOSE	0x02
54 #define SRAM_DEBUG_IOCTL	0x04
55 #define SRAM_DEBUG_DONTDOIT	0x08
56 int sramdebug = SRAM_DEBUG_IOCTL;
57 #define DPRINTF(flag, msg)	do {	\
58 	if ((sramdebug & (flag)))	\
59 		printf msg;		\
60 } while (0)
61 #else
62 #define DPRINTF(flag, msg)	/* nothing */
63 #endif
64 
65 int  srammatch(device_t, cfdata_t, void *);
66 void sramattach(device_t, device_t, void *);
67 
68 extern struct cfdriver sram_cd;
69 
70 dev_type_open(sramopen);
71 dev_type_close(sramclose);
72 dev_type_ioctl(sramioctl);
73 
74 CFATTACH_DECL_NEW(sram, sizeof(struct sram_softc),
75 	srammatch, sramattach, NULL, NULL);
76 
77 const struct cdevsw sram_cdevsw = {
78 	sramopen, sramclose, noread, nowrite, sramioctl,
79 	nostop, notty, nopoll, nommap, nokqfilter,
80 };
81 
82 static int sram_attached;
83 
84 /*
85  *  functions for probeing.
86  */
87 int
88 srammatch(device_t parent, cfdata_t cf, void *aux)
89 {
90 	struct intio_attach_args *ia = aux;
91 
92 	if (sram_attached)
93 		return 0;
94 
95 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
96 		ia->ia_addr = SRAM_ADDR;
97 
98 	/* Fixed parameter */
99 	if (ia->ia_addr != SRAM_ADDR)
100 		return 0;
101 
102 	return 1;
103 }
104 
105 void
106 sramattach(device_t parent, device_t self, void *aux)
107 {
108 	struct sram_softc *sc = device_private(self);
109 	struct intio_attach_args *ia = aux;
110 	bus_space_tag_t iot;
111 	bus_space_handle_t ioh;
112 
113 	/* Map I/O space */
114 	iot = ia->ia_bst;
115 	if (bus_space_map(iot, ia->ia_addr, SRAM_SIZE, 0, &ioh))
116 		goto out;
117 
118 	/* Initialize sc */
119 	sc->sc_iot = iot;
120 	sc->sc_ioh = ioh;
121 
122 	sc->sc_flags = 0;
123 	aprint_normal(": 16k bytes accessible\n");
124 	sram_attached = 1;
125 	return;
126 
127  out:
128 	aprint_normal(": not accessible\n");
129 }
130 
131 
132 /*ARGSUSED*/
133 int
134 sramopen(dev_t dev, int flags, int mode, struct lwp *l)
135 {
136 	struct sram_softc *sc;
137 
138 	DPRINTF(SRAM_DEBUG_OPEN, ("Sram open\n"));
139 
140 	sc = device_lookup_private(&sram_cd, minor(dev));
141 	if (sc == NULL)
142 		return ENXIO;
143 
144 	if (sc->sc_flags & SRF_OPEN)
145 		return EBUSY;
146 
147 	sc->sc_flags |= SRF_OPEN;
148 	if (flags & FREAD)
149 		sc->sc_flags |= SRF_READ;
150 	if (flags & FWRITE)
151 		sc->sc_flags |= SRF_WRITE;
152 
153 	return 0;
154 }
155 
156 /*ARGSUSED*/
157 int
158 sramclose(dev_t dev, int flags, int mode, struct lwp *l)
159 {
160 	struct sram_softc *sc;
161 
162 	DPRINTF(SRAM_DEBUG_CLOSE, ("Sram close\n"));
163 
164 	sc = device_lookup_private(&sram_cd, minor(dev));
165 	if (sc == NULL)
166 		return ENXIO;
167 
168 	if (sc->sc_flags & SRF_OPEN) {
169 		sc->sc_flags = 0;
170 	}
171 	sc->sc_flags &= ~(SRF_READ|SRF_WRITE);
172 
173 	return 0;
174 }
175 
176 /*ARGSUSED*/
177 int
178 sramioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
179 {
180 	int error = 0;
181 	struct sram_io *sram_io;
182 	struct sram_softc *sc;
183 
184 	DPRINTF(SRAM_DEBUG_IOCTL, ("Sram ioctl cmd=%lx\n", cmd));
185 
186 	sc = device_lookup_private(&sram_cd, minor(dev));
187 	if (sc == NULL)
188 		return ENXIO;
189 
190 	sram_io = (struct sram_io *)data;
191 	if (sram_io == NULL)
192 		return EFAULT;
193 
194 	switch (cmd) {
195 	case SIOGSRAM:
196 		if ((sc->sc_flags & SRF_READ) == 0)
197 			return EPERM;
198 		DPRINTF(SRAM_DEBUG_IOCTL,
199 			("Sram ioctl SIOGSRAM address=%p\n", data));
200 		DPRINTF(SRAM_DEBUG_IOCTL,
201 			("Sram ioctl SIOGSRAM offset=%x\n", sram_io->offset));
202 		if (sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
203 			return EFAULT;
204 		bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, sram_io->offset,
205 			(uint8_t *)&sram_io->sram, SRAM_IO_SIZE);
206 		break;
207 	case SIOPSRAM:
208 		if ((sc->sc_flags & SRF_WRITE) == 0)
209 			return EPERM;
210 		DPRINTF(SRAM_DEBUG_IOCTL,
211     			("Sram ioctl SIOPSRAM address=%p\n", data));
212 		DPRINTF(SRAM_DEBUG_IOCTL,
213     			("Sram ioctl SIOPSRAM offset=%x\n", sram_io->offset));
214 		if (sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
215 			return EFAULT;
216 #ifdef DEBUG
217 		if (sramdebug & SRAM_DEBUG_DONTDOIT) {
218 			printf("Sram ioctl SIOPSRAM: skipping actual write\n");
219 			break;
220 		}
221 #endif
222 		intio_set_sysport_sramwp(0x31);
223 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh,
224 			sram_io->offset, (uint8_t *)&sram_io->sram,
225 			SRAM_IO_SIZE);
226 		intio_set_sysport_sramwp(0x00);
227 		break;
228 	default:
229 		error = EINVAL;
230 		break;
231 	}
232 	return error;
233 }
234