sram.c revision 1.17 1 1.17 isaki /* $NetBSD: sram.c,v 1.17 2008/12/14 02:16:51 isaki Exp $ */
2 1.1 oki
3 1.1 oki /*
4 1.1 oki * Copyright (c) 1994 Kazuhisa Shimizu.
5 1.1 oki * All rights reserved.
6 1.1 oki *
7 1.1 oki * Redistribution and use in source and binary forms, with or without
8 1.1 oki * modification, are permitted provided that the following conditions
9 1.1 oki * are met:
10 1.1 oki * 1. Redistributions of source code must retain the above copyright
11 1.1 oki * notice, this list of conditions and the following disclaimer.
12 1.1 oki * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 oki * notice, this list of conditions and the following disclaimer in the
14 1.1 oki * documentation and/or other materials provided with the distribution.
15 1.1 oki * 3. All advertising materials mentioning features or use of this software
16 1.1 oki * must display the following acknowledgement:
17 1.1 oki * This product includes software developed by Kazuhisa Shimizu.
18 1.1 oki * 4. The name of the author may not be used to endorse or promote products
19 1.1 oki * derived from this software without specific prior written permission
20 1.1 oki *
21 1.1 oki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 oki * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 oki * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 oki * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 oki * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 oki * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 oki * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 oki * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 oki * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 oki * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 oki */
32 1.10 lukem
33 1.10 lukem #include <sys/cdefs.h>
34 1.17 isaki __KERNEL_RCSID(0, "$NetBSD: sram.c,v 1.17 2008/12/14 02:16:51 isaki Exp $");
35 1.1 oki
36 1.1 oki #include <sys/param.h>
37 1.1 oki #include <sys/proc.h>
38 1.1 oki #include <sys/ioctl.h>
39 1.1 oki #include <sys/file.h>
40 1.1 oki #include <sys/malloc.h>
41 1.1 oki #include <sys/systm.h>
42 1.8 gehenna #include <sys/conf.h>
43 1.17 isaki #include <sys/bus.h>
44 1.17 isaki #include <sys/device.h>
45 1.1 oki
46 1.1 oki #include <machine/sram.h>
47 1.17 isaki #include <x68k/dev/intiovar.h>
48 1.1 oki #include <x68k/dev/sramvar.h>
49 1.1 oki #include <x68k/x68k/iodevice.h>
50 1.1 oki
51 1.1 oki struct sram_softc sram_softc;
52 1.1 oki
53 1.1 oki #ifdef DEBUG
54 1.1 oki #define SRAM_DEBUG_OPEN 0x01
55 1.1 oki #define SRAM_DEBUG_CLOSE 0x02
56 1.6 minoura #define SRAM_DEBUG_IOCTL 0x04
57 1.6 minoura #define SRAM_DEBUG_DONTDOIT 0x08
58 1.1 oki int sramdebug = SRAM_DEBUG_IOCTL;
59 1.1 oki #endif
60 1.5 oki
61 1.11 chs void sramattach(int);
62 1.8 gehenna
63 1.8 gehenna dev_type_open(sramopen);
64 1.8 gehenna dev_type_close(sramclose);
65 1.8 gehenna dev_type_ioctl(sramioctl);
66 1.8 gehenna
67 1.8 gehenna const struct cdevsw sram_cdevsw = {
68 1.8 gehenna sramopen, sramclose, noread, nowrite, sramioctl,
69 1.9 jdolecek nostop, notty, nopoll, nommap, nokqfilter,
70 1.8 gehenna };
71 1.1 oki
72 1.16 isaki /*
73 1.1 oki * functions for probeing.
74 1.1 oki */
75 1.1 oki /* ARGSUSED */
76 1.16 isaki void
77 1.11 chs sramattach(int num)
78 1.1 oki {
79 1.1 oki sram_softc.flags = 0;
80 1.4 christos printf("sram0: 16k bytes accessible\n");
81 1.1 oki }
82 1.1 oki
83 1.1 oki
84 1.1 oki /*
85 1.1 oki * functions made available by conf.c
86 1.1 oki */
87 1.1 oki
88 1.1 oki /*ARGSUSED*/
89 1.15 isaki int
90 1.12 christos sramopen(dev_t dev, int flags, int mode, struct lwp *l)
91 1.1 oki {
92 1.1 oki struct sram_softc *su = &sram_softc;
93 1.1 oki
94 1.1 oki #ifdef DEBUG
95 1.1 oki if (sramdebug & SRAM_DEBUG_OPEN)
96 1.4 christos printf ("Sram open\n");
97 1.1 oki #endif
98 1.1 oki
99 1.1 oki if (minor(dev) >= 1)
100 1.1 oki return EXDEV;
101 1.1 oki
102 1.1 oki if (su->flags & SRF_OPEN) {
103 1.1 oki return (EBUSY);
104 1.1 oki }
105 1.1 oki
106 1.1 oki su->flags |= SRF_OPEN;
107 1.6 minoura if (flags & FREAD)
108 1.6 minoura su->flags |= SRF_READ;
109 1.6 minoura if (flags & FWRITE)
110 1.6 minoura su->flags |= SRF_WRITE;
111 1.6 minoura
112 1.1 oki return (0);
113 1.1 oki }
114 1.1 oki
115 1.1 oki /*ARGSUSED*/
116 1.15 isaki int
117 1.12 christos sramclose(dev_t dev, int flags, int mode, struct lwp *l)
118 1.1 oki {
119 1.1 oki struct sram_softc *su = &sram_softc;
120 1.1 oki
121 1.1 oki #ifdef DEBUG
122 1.1 oki if (sramdebug & SRAM_DEBUG_CLOSE)
123 1.4 christos printf ("Sram close\n");
124 1.1 oki #endif
125 1.1 oki
126 1.1 oki if (su->flags & SRF_OPEN) {
127 1.1 oki su->flags = 0;
128 1.1 oki }
129 1.6 minoura su->flags &= ~(SRF_READ|SRF_WRITE);
130 1.8 gehenna
131 1.8 gehenna return (0);
132 1.1 oki }
133 1.6 minoura
134 1.1 oki /*ARGSUSED*/
135 1.15 isaki int
136 1.14 christos sramioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
137 1.1 oki {
138 1.1 oki int error = 0;
139 1.1 oki struct sram_io *sram_io;
140 1.13 he char *sramtop = __UNVOLATILE(IODEVbase->io_sram);
141 1.6 minoura struct sram_softc *su = &sram_softc;
142 1.1 oki
143 1.1 oki #ifdef DEBUG
144 1.1 oki if (sramdebug & SRAM_DEBUG_IOCTL)
145 1.4 christos printf("Sram ioctl cmd=%lx\n", cmd);
146 1.1 oki #endif
147 1.1 oki sram_io = (struct sram_io *)data;
148 1.1 oki
149 1.1 oki switch (cmd) {
150 1.1 oki case SIOGSRAM:
151 1.6 minoura if ((su->flags & SRF_READ) == 0)
152 1.6 minoura return(EPERM);
153 1.1 oki #ifdef DEBUG
154 1.2 oki if (sramdebug & SRAM_DEBUG_IOCTL) {
155 1.16 isaki printf("Sram ioctl SIOGSRAM address=%p\n", data);
156 1.16 isaki printf("Sram ioctl SIOGSRAM offset=%x\n", sram_io->offset);
157 1.2 oki }
158 1.1 oki #endif
159 1.1 oki if (sram_io == NULL ||
160 1.1 oki sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
161 1.1 oki return(EFAULT);
162 1.7 wiz memcpy(&(sram_io->sram), sramtop + sram_io->offset,
163 1.7 wiz SRAM_IO_SIZE);
164 1.1 oki break;
165 1.1 oki case SIOPSRAM:
166 1.6 minoura if ((su->flags & SRF_WRITE) == 0)
167 1.6 minoura return(EPERM);
168 1.1 oki #ifdef DEBUG
169 1.2 oki if (sramdebug & SRAM_DEBUG_IOCTL) {
170 1.16 isaki printf("Sram ioctl SIOPSRAM address=%p\n", data);
171 1.16 isaki printf("Sram ioctl SIOPSRAM offset=%x\n", sram_io->offset);
172 1.2 oki }
173 1.1 oki #endif
174 1.1 oki if (sram_io == NULL ||
175 1.1 oki sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
176 1.1 oki return(EFAULT);
177 1.6 minoura #ifdef DEBUG
178 1.6 minoura if (sramdebug & SRAM_DEBUG_DONTDOIT) {
179 1.16 isaki printf("Sram ioctl SIOPSRAM: skipping actual write\n");
180 1.6 minoura break;
181 1.6 minoura }
182 1.6 minoura #endif
183 1.17 isaki intio_set_sysport_sramwp(0x31);
184 1.7 wiz memcpy(sramtop + sram_io->offset, &(sram_io->sram),
185 1.7 wiz SRAM_IO_SIZE);
186 1.17 isaki intio_set_sysport_sramwp(0x00);
187 1.1 oki break;
188 1.1 oki default:
189 1.1 oki error = EINVAL;
190 1.1 oki break;
191 1.1 oki }
192 1.2 oki return (error);
193 1.1 oki }
194