Home | History | Annotate | Line # | Download | only in dev
sram.c revision 1.19
      1  1.19  dholland /*	$NetBSD: sram.c,v 1.19 2014/03/16 05:20:26 dholland 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.19  dholland __KERNEL_RCSID(0, "$NetBSD: sram.c,v 1.19 2014/03/16 05:20:26 dholland 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.19  dholland 	.d_open = sramopen,
     79  1.19  dholland 	.d_close = sramclose,
     80  1.19  dholland 	.d_read = noread,
     81  1.19  dholland 	.d_write = nowrite,
     82  1.19  dholland 	.d_ioctl = sramioctl,
     83  1.19  dholland 	.d_stop = nostop,
     84  1.19  dholland 	.d_tty = notty,
     85  1.19  dholland 	.d_poll = nopoll,
     86  1.19  dholland 	.d_mmap = nommap,
     87  1.19  dholland 	.d_kqfilter = nokqfilter,
     88  1.19  dholland 	.d_flag = 0
     89   1.8   gehenna };
     90   1.1       oki 
     91  1.18     isaki static int sram_attached;
     92  1.18     isaki 
     93  1.16     isaki /*
     94   1.1       oki  *  functions for probeing.
     95   1.1       oki  */
     96  1.18     isaki int
     97  1.18     isaki srammatch(device_t parent, cfdata_t cf, void *aux)
     98  1.18     isaki {
     99  1.18     isaki 	struct intio_attach_args *ia = aux;
    100  1.18     isaki 
    101  1.18     isaki 	if (sram_attached)
    102  1.18     isaki 		return 0;
    103  1.18     isaki 
    104  1.18     isaki 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    105  1.18     isaki 		ia->ia_addr = SRAM_ADDR;
    106  1.18     isaki 
    107  1.18     isaki 	/* Fixed parameter */
    108  1.18     isaki 	if (ia->ia_addr != SRAM_ADDR)
    109  1.18     isaki 		return 0;
    110  1.18     isaki 
    111  1.18     isaki 	return 1;
    112  1.18     isaki }
    113  1.18     isaki 
    114  1.16     isaki void
    115  1.18     isaki sramattach(device_t parent, device_t self, void *aux)
    116   1.1       oki {
    117  1.18     isaki 	struct sram_softc *sc = device_private(self);
    118  1.18     isaki 	struct intio_attach_args *ia = aux;
    119  1.18     isaki 	bus_space_tag_t iot;
    120  1.18     isaki 	bus_space_handle_t ioh;
    121  1.18     isaki 
    122  1.18     isaki 	/* Map I/O space */
    123  1.18     isaki 	iot = ia->ia_bst;
    124  1.18     isaki 	if (bus_space_map(iot, ia->ia_addr, SRAM_SIZE, 0, &ioh))
    125  1.18     isaki 		goto out;
    126  1.18     isaki 
    127  1.18     isaki 	/* Initialize sc */
    128  1.18     isaki 	sc->sc_iot = iot;
    129  1.18     isaki 	sc->sc_ioh = ioh;
    130  1.18     isaki 
    131  1.18     isaki 	sc->sc_flags = 0;
    132  1.18     isaki 	aprint_normal(": 16k bytes accessible\n");
    133  1.18     isaki 	sram_attached = 1;
    134  1.18     isaki 	return;
    135  1.18     isaki 
    136  1.18     isaki  out:
    137  1.18     isaki 	aprint_normal(": not accessible\n");
    138   1.1       oki }
    139   1.1       oki 
    140   1.1       oki 
    141   1.1       oki /*ARGSUSED*/
    142  1.15     isaki int
    143  1.12  christos sramopen(dev_t dev, int flags, int mode, struct lwp *l)
    144   1.1       oki {
    145  1.18     isaki 	struct sram_softc *sc;
    146   1.1       oki 
    147  1.18     isaki 	DPRINTF(SRAM_DEBUG_OPEN, ("Sram open\n"));
    148   1.1       oki 
    149  1.18     isaki 	sc = device_lookup_private(&sram_cd, minor(dev));
    150  1.18     isaki 	if (sc == NULL)
    151  1.18     isaki 		return ENXIO;
    152   1.1       oki 
    153  1.18     isaki 	if (sc->sc_flags & SRF_OPEN)
    154  1.18     isaki 		return EBUSY;
    155   1.1       oki 
    156  1.18     isaki 	sc->sc_flags |= SRF_OPEN;
    157   1.6   minoura 	if (flags & FREAD)
    158  1.18     isaki 		sc->sc_flags |= SRF_READ;
    159   1.6   minoura 	if (flags & FWRITE)
    160  1.18     isaki 		sc->sc_flags |= SRF_WRITE;
    161   1.6   minoura 
    162  1.18     isaki 	return 0;
    163   1.1       oki }
    164   1.1       oki 
    165   1.1       oki /*ARGSUSED*/
    166  1.15     isaki int
    167  1.12  christos sramclose(dev_t dev, int flags, int mode, struct lwp *l)
    168   1.1       oki {
    169  1.18     isaki 	struct sram_softc *sc;
    170  1.18     isaki 
    171  1.18     isaki 	DPRINTF(SRAM_DEBUG_CLOSE, ("Sram close\n"));
    172   1.1       oki 
    173  1.18     isaki 	sc = device_lookup_private(&sram_cd, minor(dev));
    174  1.18     isaki 	if (sc == NULL)
    175  1.18     isaki 		return ENXIO;
    176   1.1       oki 
    177  1.18     isaki 	if (sc->sc_flags & SRF_OPEN) {
    178  1.18     isaki 		sc->sc_flags = 0;
    179   1.1       oki 	}
    180  1.18     isaki 	sc->sc_flags &= ~(SRF_READ|SRF_WRITE);
    181   1.8   gehenna 
    182  1.18     isaki 	return 0;
    183   1.1       oki }
    184   1.6   minoura 
    185   1.1       oki /*ARGSUSED*/
    186  1.15     isaki int
    187  1.14  christos sramioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    188   1.1       oki {
    189   1.1       oki 	int error = 0;
    190   1.1       oki 	struct sram_io *sram_io;
    191  1.18     isaki 	struct sram_softc *sc;
    192  1.18     isaki 
    193  1.18     isaki 	DPRINTF(SRAM_DEBUG_IOCTL, ("Sram ioctl cmd=%lx\n", cmd));
    194  1.18     isaki 
    195  1.18     isaki 	sc = device_lookup_private(&sram_cd, minor(dev));
    196  1.18     isaki 	if (sc == NULL)
    197  1.18     isaki 		return ENXIO;
    198   1.1       oki 
    199   1.1       oki 	sram_io = (struct sram_io *)data;
    200  1.18     isaki 	if (sram_io == NULL)
    201  1.18     isaki 		return EFAULT;
    202   1.1       oki 
    203   1.1       oki 	switch (cmd) {
    204   1.1       oki 	case SIOGSRAM:
    205  1.18     isaki 		if ((sc->sc_flags & SRF_READ) == 0)
    206  1.18     isaki 			return EPERM;
    207  1.18     isaki 		DPRINTF(SRAM_DEBUG_IOCTL,
    208  1.18     isaki 			("Sram ioctl SIOGSRAM address=%p\n", data));
    209  1.18     isaki 		DPRINTF(SRAM_DEBUG_IOCTL,
    210  1.18     isaki 			("Sram ioctl SIOGSRAM offset=%x\n", sram_io->offset));
    211  1.18     isaki 		if (sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
    212  1.18     isaki 			return EFAULT;
    213  1.18     isaki 		bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, sram_io->offset,
    214  1.18     isaki 			(uint8_t *)&sram_io->sram, SRAM_IO_SIZE);
    215   1.1       oki 		break;
    216   1.1       oki 	case SIOPSRAM:
    217  1.18     isaki 		if ((sc->sc_flags & SRF_WRITE) == 0)
    218  1.18     isaki 			return EPERM;
    219  1.18     isaki 		DPRINTF(SRAM_DEBUG_IOCTL,
    220  1.18     isaki     			("Sram ioctl SIOPSRAM address=%p\n", data));
    221  1.18     isaki 		DPRINTF(SRAM_DEBUG_IOCTL,
    222  1.18     isaki     			("Sram ioctl SIOPSRAM offset=%x\n", sram_io->offset));
    223  1.18     isaki 		if (sram_io->offset + SRAM_IO_SIZE > SRAM_SIZE)
    224  1.18     isaki 			return EFAULT;
    225   1.6   minoura #ifdef DEBUG
    226   1.6   minoura 		if (sramdebug & SRAM_DEBUG_DONTDOIT) {
    227  1.16     isaki 			printf("Sram ioctl SIOPSRAM: skipping actual write\n");
    228   1.6   minoura 			break;
    229   1.6   minoura 		}
    230   1.6   minoura #endif
    231  1.17     isaki 		intio_set_sysport_sramwp(0x31);
    232  1.18     isaki 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh,
    233  1.18     isaki 			sram_io->offset, (uint8_t *)&sram_io->sram,
    234  1.18     isaki 			SRAM_IO_SIZE);
    235  1.17     isaki 		intio_set_sysport_sramwp(0x00);
    236   1.1       oki 		break;
    237   1.1       oki 	default:
    238   1.1       oki 		error = EINVAL;
    239   1.1       oki 		break;
    240   1.1       oki 	}
    241  1.18     isaki 	return error;
    242   1.1       oki }
    243