Home | History | Annotate | Line # | Download | only in dev
sram.c revision 1.16.50.1
      1  1.16.50.1     skrll /*	$NetBSD: sram.c,v 1.16.50.1 2009/01/19 13:17:03 skrll 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.16.50.1     skrll __KERNEL_RCSID(0, "$NetBSD: sram.c,v 1.16.50.1 2009/01/19 13:17:03 skrll Exp $");
     35        1.1       oki 
     36        1.1       oki #include <sys/param.h>
     37  1.16.50.1     skrll #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.16.50.1     skrll #include <sys/bus.h>
     43  1.16.50.1     skrll #include <sys/device.h>
     44        1.1       oki 
     45        1.1       oki #include <machine/sram.h>
     46  1.16.50.1     skrll #include <x68k/dev/intiovar.h>
     47        1.1       oki #include <x68k/dev/sramvar.h>
     48        1.1       oki 
     49  1.16.50.1     skrll #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.16.50.1     skrll #define DPRINTF(flag, msg)	do {	\
     58  1.16.50.1     skrll 	if ((sramdebug & (flag)))	\
     59  1.16.50.1     skrll 		printf msg;		\
     60  1.16.50.1     skrll } while (0)
     61  1.16.50.1     skrll #else
     62  1.16.50.1     skrll #define DPRINTF(flag, msg)	/* nothing */
     63        1.1       oki #endif
     64        1.5       oki 
     65  1.16.50.1     skrll int  srammatch(device_t, cfdata_t, void *);
     66  1.16.50.1     skrll void sramattach(device_t, device_t, void *);
     67  1.16.50.1     skrll 
     68  1.16.50.1     skrll 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.16.50.1     skrll CFATTACH_DECL_NEW(sram, sizeof(struct sram_softc),
     75  1.16.50.1     skrll 	srammatch, sramattach, NULL, NULL);
     76  1.16.50.1     skrll 
     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.16.50.1     skrll static int sram_attached;
     83  1.16.50.1     skrll 
     84       1.16     isaki /*
     85        1.1       oki  *  functions for probeing.
     86        1.1       oki  */
     87  1.16.50.1     skrll int
     88  1.16.50.1     skrll srammatch(device_t parent, cfdata_t cf, void *aux)
     89        1.1       oki {
     90  1.16.50.1     skrll 	struct intio_attach_args *ia = aux;
     91  1.16.50.1     skrll 
     92  1.16.50.1     skrll 	if (sram_attached)
     93  1.16.50.1     skrll 		return 0;
     94  1.16.50.1     skrll 
     95  1.16.50.1     skrll 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
     96  1.16.50.1     skrll 		ia->ia_addr = SRAM_ADDR;
     97  1.16.50.1     skrll 
     98  1.16.50.1     skrll 	/* Fixed parameter */
     99  1.16.50.1     skrll 	if (ia->ia_addr != SRAM_ADDR)
    100  1.16.50.1     skrll 		return 0;
    101  1.16.50.1     skrll 
    102  1.16.50.1     skrll 	return 1;
    103        1.1       oki }
    104        1.1       oki 
    105  1.16.50.1     skrll void
    106  1.16.50.1     skrll sramattach(device_t parent, device_t self, void *aux)
    107  1.16.50.1     skrll {
    108  1.16.50.1     skrll 	struct sram_softc *sc = device_private(self);
    109  1.16.50.1     skrll 	struct intio_attach_args *ia = aux;
    110  1.16.50.1     skrll 	bus_space_tag_t iot;
    111  1.16.50.1     skrll 	bus_space_handle_t ioh;
    112  1.16.50.1     skrll 
    113  1.16.50.1     skrll 	/* Map I/O space */
    114  1.16.50.1     skrll 	iot = ia->ia_bst;
    115  1.16.50.1     skrll 	if (bus_space_map(iot, ia->ia_addr, SRAM_SIZE, 0, &ioh))
    116  1.16.50.1     skrll 		goto out;
    117  1.16.50.1     skrll 
    118  1.16.50.1     skrll 	/* Initialize sc */
    119  1.16.50.1     skrll 	sc->sc_iot = iot;
    120  1.16.50.1     skrll 	sc->sc_ioh = ioh;
    121  1.16.50.1     skrll 
    122  1.16.50.1     skrll 	sc->sc_flags = 0;
    123  1.16.50.1     skrll 	aprint_normal(": 16k bytes accessible\n");
    124  1.16.50.1     skrll 	sram_attached = 1;
    125  1.16.50.1     skrll 	return;
    126  1.16.50.1     skrll 
    127  1.16.50.1     skrll  out:
    128  1.16.50.1     skrll 	aprint_normal(": not accessible\n");
    129  1.16.50.1     skrll }
    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.16.50.1     skrll 	struct sram_softc *sc;
    137        1.1       oki 
    138  1.16.50.1     skrll 	DPRINTF(SRAM_DEBUG_OPEN, ("Sram open\n"));
    139        1.1       oki 
    140  1.16.50.1     skrll 	sc = device_lookup_private(&sram_cd, minor(dev));
    141  1.16.50.1     skrll 	if (sc == NULL)
    142  1.16.50.1     skrll 		return ENXIO;
    143        1.1       oki 
    144  1.16.50.1     skrll 	if (sc->sc_flags & SRF_OPEN)
    145  1.16.50.1     skrll 		return EBUSY;
    146        1.1       oki 
    147  1.16.50.1     skrll 	sc->sc_flags |= SRF_OPEN;
    148        1.6   minoura 	if (flags & FREAD)
    149  1.16.50.1     skrll 		sc->sc_flags |= SRF_READ;
    150        1.6   minoura 	if (flags & FWRITE)
    151  1.16.50.1     skrll 		sc->sc_flags |= SRF_WRITE;
    152        1.6   minoura 
    153  1.16.50.1     skrll 	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.16.50.1     skrll 	struct sram_softc *sc;
    161        1.1       oki 
    162  1.16.50.1     skrll 	DPRINTF(SRAM_DEBUG_CLOSE, ("Sram close\n"));
    163  1.16.50.1     skrll 
    164  1.16.50.1     skrll 	sc = device_lookup_private(&sram_cd, minor(dev));
    165  1.16.50.1     skrll 	if (sc == NULL)
    166  1.16.50.1     skrll 		return ENXIO;
    167        1.1       oki 
    168  1.16.50.1     skrll 	if (sc->sc_flags & SRF_OPEN) {
    169  1.16.50.1     skrll 		sc->sc_flags = 0;
    170        1.1       oki 	}
    171  1.16.50.1     skrll 	sc->sc_flags &= ~(SRF_READ|SRF_WRITE);
    172        1.8   gehenna 
    173  1.16.50.1     skrll 	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.16.50.1     skrll 	struct sram_softc *sc;
    183  1.16.50.1     skrll 
    184  1.16.50.1     skrll 	DPRINTF(SRAM_DEBUG_IOCTL, ("Sram ioctl cmd=%lx\n", cmd));
    185  1.16.50.1     skrll 
    186  1.16.50.1     skrll 	sc = device_lookup_private(&sram_cd, minor(dev));
    187  1.16.50.1     skrll 	if (sc == NULL)
    188  1.16.50.1     skrll 		return ENXIO;
    189        1.1       oki 
    190        1.1       oki 	sram_io = (struct sram_io *)data;
    191  1.16.50.1     skrll 	if (sram_io == NULL)
    192  1.16.50.1     skrll 		return EFAULT;
    193        1.1       oki 
    194        1.1       oki 	switch (cmd) {
    195        1.1       oki 	case SIOGSRAM:
    196  1.16.50.1     skrll 		if ((sc->sc_flags & SRF_READ) == 0)
    197  1.16.50.1     skrll 			return EPERM;
    198  1.16.50.1     skrll 		DPRINTF(SRAM_DEBUG_IOCTL,
    199  1.16.50.1     skrll 			("Sram ioctl SIOGSRAM address=%p\n", data));
    200  1.16.50.1     skrll 		DPRINTF(SRAM_DEBUG_IOCTL,
    201  1.16.50.1     skrll 			("Sram ioctl SIOGSRAM offset=%x\n", sram_io->offset));
    202  1.16.50.1     skrll 		if (sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
    203  1.16.50.1     skrll 			return EFAULT;
    204  1.16.50.1     skrll 		bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, sram_io->offset,
    205  1.16.50.1     skrll 			(uint8_t *)&sram_io->sram, SRAM_IO_SIZE);
    206        1.1       oki 		break;
    207        1.1       oki 	case SIOPSRAM:
    208  1.16.50.1     skrll 		if ((sc->sc_flags & SRF_WRITE) == 0)
    209  1.16.50.1     skrll 			return EPERM;
    210  1.16.50.1     skrll 		DPRINTF(SRAM_DEBUG_IOCTL,
    211  1.16.50.1     skrll     			("Sram ioctl SIOPSRAM address=%p\n", data));
    212  1.16.50.1     skrll 		DPRINTF(SRAM_DEBUG_IOCTL,
    213  1.16.50.1     skrll     			("Sram ioctl SIOPSRAM offset=%x\n", sram_io->offset));
    214  1.16.50.1     skrll 		if (sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
    215  1.16.50.1     skrll 			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.16.50.1     skrll 		intio_set_sysport_sramwp(0x31);
    223  1.16.50.1     skrll 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh,
    224  1.16.50.1     skrll 			sram_io->offset, (uint8_t *)&sram_io->sram,
    225  1.16.50.1     skrll 			SRAM_IO_SIZE);
    226  1.16.50.1     skrll 		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.16.50.1     skrll 	return error;
    233        1.1       oki }
    234