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