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