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