Home | History | Annotate | Line # | Download | only in dev
sram.c revision 1.18
      1  1.18     isaki /*	$NetBSD: sram.c,v 1.18 2008/12/21 09:01:19 isaki 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.10     lukem 
     33  1.10     lukem #include <sys/cdefs.h>
     34  1.18     isaki __KERNEL_RCSID(0, "$NetBSD: sram.c,v 1.18 2008/12/21 09:01:19 isaki Exp $");
     35   1.1       oki 
     36   1.1       oki #include <sys/param.h>
     37  1.18     isaki #include <sys/systm.h>
     38   1.1       oki #include <sys/proc.h>
     39   1.1       oki #include <sys/ioctl.h>
     40   1.1       oki #include <sys/file.h>
     41   1.8   gehenna #include <sys/conf.h>
     42  1.17     isaki #include <sys/bus.h>
     43  1.17     isaki #include <sys/device.h>
     44   1.1       oki 
     45   1.1       oki #include <machine/sram.h>
     46  1.17     isaki #include <x68k/dev/intiovar.h>
     47   1.1       oki #include <x68k/dev/sramvar.h>
     48   1.1       oki 
     49  1.18     isaki #define SRAM_ADDR	(0xed0000)
     50   1.1       oki 
     51   1.1       oki #ifdef DEBUG
     52   1.1       oki #define SRAM_DEBUG_OPEN		0x01
     53   1.1       oki #define SRAM_DEBUG_CLOSE	0x02
     54   1.6   minoura #define SRAM_DEBUG_IOCTL	0x04
     55   1.6   minoura #define SRAM_DEBUG_DONTDOIT	0x08
     56   1.1       oki int sramdebug = SRAM_DEBUG_IOCTL;
     57  1.18     isaki #define DPRINTF(flag, msg)	do {	\
     58  1.18     isaki 	if ((sramdebug & (flag)))	\
     59  1.18     isaki 		printf msg;		\
     60  1.18     isaki } while (0)
     61  1.18     isaki #else
     62  1.18     isaki #define DPRINTF(flag, msg)	/* nothing */
     63   1.1       oki #endif
     64   1.5       oki 
     65  1.18     isaki int  srammatch(device_t, cfdata_t, void *);
     66  1.18     isaki void sramattach(device_t, device_t, void *);
     67  1.18     isaki 
     68  1.18     isaki extern struct cfdriver sram_cd;
     69   1.8   gehenna 
     70   1.8   gehenna dev_type_open(sramopen);
     71   1.8   gehenna dev_type_close(sramclose);
     72   1.8   gehenna dev_type_ioctl(sramioctl);
     73   1.8   gehenna 
     74  1.18     isaki CFATTACH_DECL_NEW(sram, sizeof(struct sram_softc),
     75  1.18     isaki 	srammatch, sramattach, NULL, NULL);
     76  1.18     isaki 
     77   1.8   gehenna const struct cdevsw sram_cdevsw = {
     78   1.8   gehenna 	sramopen, sramclose, noread, nowrite, sramioctl,
     79   1.9  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
     80   1.8   gehenna };
     81   1.1       oki 
     82  1.18     isaki static int sram_attached;
     83  1.18     isaki 
     84  1.16     isaki /*
     85   1.1       oki  *  functions for probeing.
     86   1.1       oki  */
     87  1.18     isaki int
     88  1.18     isaki srammatch(device_t parent, cfdata_t cf, void *aux)
     89  1.18     isaki {
     90  1.18     isaki 	struct intio_attach_args *ia = aux;
     91  1.18     isaki 
     92  1.18     isaki 	if (sram_attached)
     93  1.18     isaki 		return 0;
     94  1.18     isaki 
     95  1.18     isaki 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
     96  1.18     isaki 		ia->ia_addr = SRAM_ADDR;
     97  1.18     isaki 
     98  1.18     isaki 	/* Fixed parameter */
     99  1.18     isaki 	if (ia->ia_addr != SRAM_ADDR)
    100  1.18     isaki 		return 0;
    101  1.18     isaki 
    102  1.18     isaki 	return 1;
    103  1.18     isaki }
    104  1.18     isaki 
    105  1.16     isaki void
    106  1.18     isaki sramattach(device_t parent, device_t self, void *aux)
    107   1.1       oki {
    108  1.18     isaki 	struct sram_softc *sc = device_private(self);
    109  1.18     isaki 	struct intio_attach_args *ia = aux;
    110  1.18     isaki 	bus_space_tag_t iot;
    111  1.18     isaki 	bus_space_handle_t ioh;
    112  1.18     isaki 
    113  1.18     isaki 	/* Map I/O space */
    114  1.18     isaki 	iot = ia->ia_bst;
    115  1.18     isaki 	if (bus_space_map(iot, ia->ia_addr, SRAM_SIZE, 0, &ioh))
    116  1.18     isaki 		goto out;
    117  1.18     isaki 
    118  1.18     isaki 	/* Initialize sc */
    119  1.18     isaki 	sc->sc_iot = iot;
    120  1.18     isaki 	sc->sc_ioh = ioh;
    121  1.18     isaki 
    122  1.18     isaki 	sc->sc_flags = 0;
    123  1.18     isaki 	aprint_normal(": 16k bytes accessible\n");
    124  1.18     isaki 	sram_attached = 1;
    125  1.18     isaki 	return;
    126  1.18     isaki 
    127  1.18     isaki  out:
    128  1.18     isaki 	aprint_normal(": not accessible\n");
    129   1.1       oki }
    130   1.1       oki 
    131   1.1       oki 
    132   1.1       oki /*ARGSUSED*/
    133  1.15     isaki int
    134  1.12  christos sramopen(dev_t dev, int flags, int mode, struct lwp *l)
    135   1.1       oki {
    136  1.18     isaki 	struct sram_softc *sc;
    137   1.1       oki 
    138  1.18     isaki 	DPRINTF(SRAM_DEBUG_OPEN, ("Sram open\n"));
    139   1.1       oki 
    140  1.18     isaki 	sc = device_lookup_private(&sram_cd, minor(dev));
    141  1.18     isaki 	if (sc == NULL)
    142  1.18     isaki 		return ENXIO;
    143   1.1       oki 
    144  1.18     isaki 	if (sc->sc_flags & SRF_OPEN)
    145  1.18     isaki 		return EBUSY;
    146   1.1       oki 
    147  1.18     isaki 	sc->sc_flags |= SRF_OPEN;
    148   1.6   minoura 	if (flags & FREAD)
    149  1.18     isaki 		sc->sc_flags |= SRF_READ;
    150   1.6   minoura 	if (flags & FWRITE)
    151  1.18     isaki 		sc->sc_flags |= SRF_WRITE;
    152   1.6   minoura 
    153  1.18     isaki 	return 0;
    154   1.1       oki }
    155   1.1       oki 
    156   1.1       oki /*ARGSUSED*/
    157  1.15     isaki int
    158  1.12  christos sramclose(dev_t dev, int flags, int mode, struct lwp *l)
    159   1.1       oki {
    160  1.18     isaki 	struct sram_softc *sc;
    161  1.18     isaki 
    162  1.18     isaki 	DPRINTF(SRAM_DEBUG_CLOSE, ("Sram close\n"));
    163   1.1       oki 
    164  1.18     isaki 	sc = device_lookup_private(&sram_cd, minor(dev));
    165  1.18     isaki 	if (sc == NULL)
    166  1.18     isaki 		return ENXIO;
    167   1.1       oki 
    168  1.18     isaki 	if (sc->sc_flags & SRF_OPEN) {
    169  1.18     isaki 		sc->sc_flags = 0;
    170   1.1       oki 	}
    171  1.18     isaki 	sc->sc_flags &= ~(SRF_READ|SRF_WRITE);
    172   1.8   gehenna 
    173  1.18     isaki 	return 0;
    174   1.1       oki }
    175   1.6   minoura 
    176   1.1       oki /*ARGSUSED*/
    177  1.15     isaki int
    178  1.14  christos sramioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    179   1.1       oki {
    180   1.1       oki 	int error = 0;
    181   1.1       oki 	struct sram_io *sram_io;
    182  1.18     isaki 	struct sram_softc *sc;
    183  1.18     isaki 
    184  1.18     isaki 	DPRINTF(SRAM_DEBUG_IOCTL, ("Sram ioctl cmd=%lx\n", cmd));
    185  1.18     isaki 
    186  1.18     isaki 	sc = device_lookup_private(&sram_cd, minor(dev));
    187  1.18     isaki 	if (sc == NULL)
    188  1.18     isaki 		return ENXIO;
    189   1.1       oki 
    190   1.1       oki 	sram_io = (struct sram_io *)data;
    191  1.18     isaki 	if (sram_io == NULL)
    192  1.18     isaki 		return EFAULT;
    193   1.1       oki 
    194   1.1       oki 	switch (cmd) {
    195   1.1       oki 	case SIOGSRAM:
    196  1.18     isaki 		if ((sc->sc_flags & SRF_READ) == 0)
    197  1.18     isaki 			return EPERM;
    198  1.18     isaki 		DPRINTF(SRAM_DEBUG_IOCTL,
    199  1.18     isaki 			("Sram ioctl SIOGSRAM address=%p\n", data));
    200  1.18     isaki 		DPRINTF(SRAM_DEBUG_IOCTL,
    201  1.18     isaki 			("Sram ioctl SIOGSRAM offset=%x\n", sram_io->offset));
    202  1.18     isaki 		if (sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
    203  1.18     isaki 			return EFAULT;
    204  1.18     isaki 		bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, sram_io->offset,
    205  1.18     isaki 			(uint8_t *)&sram_io->sram, SRAM_IO_SIZE);
    206   1.1       oki 		break;
    207   1.1       oki 	case SIOPSRAM:
    208  1.18     isaki 		if ((sc->sc_flags & SRF_WRITE) == 0)
    209  1.18     isaki 			return EPERM;
    210  1.18     isaki 		DPRINTF(SRAM_DEBUG_IOCTL,
    211  1.18     isaki     			("Sram ioctl SIOPSRAM address=%p\n", data));
    212  1.18     isaki 		DPRINTF(SRAM_DEBUG_IOCTL,
    213  1.18     isaki     			("Sram ioctl SIOPSRAM offset=%x\n", sram_io->offset));
    214  1.18     isaki 		if (sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
    215  1.18     isaki 			return EFAULT;
    216   1.6   minoura #ifdef DEBUG
    217   1.6   minoura 		if (sramdebug & SRAM_DEBUG_DONTDOIT) {
    218  1.16     isaki 			printf("Sram ioctl SIOPSRAM: skipping actual write\n");
    219   1.6   minoura 			break;
    220   1.6   minoura 		}
    221   1.6   minoura #endif
    222  1.17     isaki 		intio_set_sysport_sramwp(0x31);
    223  1.18     isaki 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh,
    224  1.18     isaki 			sram_io->offset, (uint8_t *)&sram_io->sram,
    225  1.18     isaki 			SRAM_IO_SIZE);
    226  1.17     isaki 		intio_set_sysport_sramwp(0x00);
    227   1.1       oki 		break;
    228   1.1       oki 	default:
    229   1.1       oki 		error = EINVAL;
    230   1.1       oki 		break;
    231   1.1       oki 	}
    232  1.18     isaki 	return error;
    233   1.1       oki }
    234