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