Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / x68k / dev / sram.c
blobd1f7a591ae845f78ca0be13f7f9328281c02dc0f
1 /* $NetBSD: sram.c,v 1.17 2008/12/14 02:16:51 isaki Exp $ */
3 /*
4 * Copyright (c) 1994 Kazuhisa Shimizu.
5 * All rights reserved.
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
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: sram.c,v 1.17 2008/12/14 02:16:51 isaki Exp $");
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>
45 #include <machine/sram.h>
46 #include <x68k/dev/intiovar.h>
47 #include <x68k/dev/sramvar.h>
49 #define SRAM_ADDR (0xed0000)
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
65 int srammatch(device_t, cfdata_t, void *);
66 void sramattach(device_t, device_t, void *);
68 extern struct cfdriver sram_cd;
70 dev_type_open(sramopen);
71 dev_type_close(sramclose);
72 dev_type_ioctl(sramioctl);
74 CFATTACH_DECL_NEW(sram, sizeof(struct sram_softc),
75 srammatch, sramattach, NULL, NULL);
77 const struct cdevsw sram_cdevsw = {
78 sramopen, sramclose, noread, nowrite, sramioctl,
79 nostop, notty, nopoll, nommap, nokqfilter,
82 static int sram_attached;
85 * functions for probeing.
87 int
88 srammatch(device_t parent, cfdata_t cf, void *aux)
90 struct intio_attach_args *ia = aux;
92 if (sram_attached)
93 return 0;
95 if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
96 ia->ia_addr = SRAM_ADDR;
98 /* Fixed parameter */
99 if (ia->ia_addr != SRAM_ADDR)
100 return 0;
102 return 1;
105 void
106 sramattach(device_t parent, device_t self, void *aux)
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;
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;
118 /* Initialize sc */
119 sc->sc_iot = iot;
120 sc->sc_ioh = ioh;
122 sc->sc_flags = 0;
123 aprint_normal(": 16k bytes accessible\n");
124 sram_attached = 1;
125 return;
127 out:
128 aprint_normal(": not accessible\n");
132 /*ARGSUSED*/
134 sramopen(dev_t dev, int flags, int mode, struct lwp *l)
136 struct sram_softc *sc;
138 DPRINTF(SRAM_DEBUG_OPEN, ("Sram open\n"));
140 sc = device_lookup_private(&sram_cd, minor(dev));
141 if (sc == NULL)
142 return ENXIO;
144 if (sc->sc_flags & SRF_OPEN)
145 return EBUSY;
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;
153 return 0;
156 /*ARGSUSED*/
158 sramclose(dev_t dev, int flags, int mode, struct lwp *l)
160 struct sram_softc *sc;
162 DPRINTF(SRAM_DEBUG_CLOSE, ("Sram close\n"));
164 sc = device_lookup_private(&sram_cd, minor(dev));
165 if (sc == NULL)
166 return ENXIO;
168 if (sc->sc_flags & SRF_OPEN) {
169 sc->sc_flags = 0;
171 sc->sc_flags &= ~(SRF_READ|SRF_WRITE);
173 return 0;
176 /*ARGSUSED*/
178 sramioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
180 int error = 0;
181 struct sram_io *sram_io;
182 struct sram_softc *sc;
184 DPRINTF(SRAM_DEBUG_IOCTL, ("Sram ioctl cmd=%lx\n", cmd));
186 sc = device_lookup_private(&sram_cd, minor(dev));
187 if (sc == NULL)
188 return ENXIO;
190 sram_io = (struct sram_io *)data;
191 if (sram_io == NULL)
192 return EFAULT;
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;
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;
232 return error;