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