Home | History | Annotate | Line # | Download | only in libsa
      1  1.7  christos /*	$NetBSD: promdev.c,v 1.7 2017/02/01 18:24:22 christos Exp $ */
      2  1.1  fredette 
      3  1.1  fredette /*
      4  1.1  fredette  * Copyright (c) 1995 Gordon W. Ross
      5  1.1  fredette  * Copyright (c) 1993 Paul Kranenburg
      6  1.1  fredette  * All rights reserved.
      7  1.1  fredette  *
      8  1.1  fredette  * Redistribution and use in source and binary forms, with or without
      9  1.1  fredette  * modification, are permitted provided that the following conditions
     10  1.1  fredette  * are met:
     11  1.1  fredette  * 1. Redistributions of source code must retain the above copyright
     12  1.1  fredette  *    notice, this list of conditions and the following disclaimer.
     13  1.1  fredette  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  fredette  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  fredette  *    documentation and/or other materials provided with the distribution.
     16  1.1  fredette  * 3. All advertising materials mentioning features or use of this software
     17  1.1  fredette  *    must display the following acknowledgement:
     18  1.1  fredette  *      This product includes software developed by Paul Kranenburg.
     19  1.1  fredette  * 4. The name of the author may not be used to endorse or promote products
     20  1.1  fredette  *    derived from this software without specific prior written permission
     21  1.1  fredette  *
     22  1.1  fredette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1  fredette  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  fredette  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  fredette  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1  fredette  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.1  fredette  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.1  fredette  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.1  fredette  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.1  fredette  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.1  fredette  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1  fredette  */
     33  1.1  fredette 
     34  1.1  fredette #include <sys/types.h>
     35  1.1  fredette #include <machine/mon.h>
     36  1.1  fredette 
     37  1.1  fredette #include <stand.h>
     38  1.1  fredette 
     39  1.1  fredette #include "libsa.h"
     40  1.1  fredette #include "dvma.h"
     41  1.1  fredette #include "saio.h"
     42  1.1  fredette 
     43  1.1  fredette int promdev_inuse;
     44  1.1  fredette 
     45  1.7  christos #ifdef DEBUG_PROM
     46  1.7  christos # define DPRINTF(fmt, ...) \
     47  1.7  christos 	do { \
     48  1.7  christos 		if (debug) \
     49  1.7  christos 			printf("%s: " fmt "\n", __func__, __VA_ARGS__); \
     50  1.7  christos 	} while (/*CONSTCOND*/0)
     51  1.7  christos #else
     52  1.7  christos # define DPRINTF(fmt, ...)
     53  1.7  christos #endif
     54  1.7  christos 
     55  1.1  fredette /*
     56  1.1  fredette  * Note: caller sets the fields:
     57  1.1  fredette  *	si->si_boottab
     58  1.1  fredette  *	si->si_ctlr
     59  1.1  fredette  *	si->si_unit
     60  1.1  fredette  *	si->si_boff
     61  1.1  fredette  */
     62  1.1  fredette 
     63  1.3       chs int
     64  1.3       chs prom_iopen(struct saioreq *si)
     65  1.1  fredette {
     66  1.1  fredette 	struct boottab *ops;
     67  1.1  fredette 	struct devinfo *dip;
     68  1.5   tsutsui 	int	ctlr, error;
     69  1.1  fredette 
     70  1.1  fredette 	if (promdev_inuse)
     71  1.7  christos 		return EMFILE;
     72  1.1  fredette 
     73  1.1  fredette 	ops = si->si_boottab;
     74  1.1  fredette 	dip = ops->b_devinfo;
     75  1.1  fredette 	ctlr = si->si_ctlr;
     76  1.1  fredette 
     77  1.7  christos 
     78  1.7  christos 	DPRINTF("Boot device type: %s", ops->b_desc);
     79  1.1  fredette 
     80  1.1  fredette 	if (!_is2) {
     81  1.1  fredette #ifdef DEBUG_PROM
     82  1.1  fredette 		if (debug) {
     83  1.7  christos 			printf("d_devbytes=%d\n", dip->d_devbytes);
     84  1.7  christos 			printf("d_dmabytes=%d\n", dip->d_dmabytes);
     85  1.7  christos 			printf("d_localbytes=%d\n", dip->d_localbytes);
     86  1.7  christos 			printf("d_devtype=%d\n", dip->d_devtype);
     87  1.7  christos 			printf("d_maxiobytes=%d\n", dip->d_maxiobytes);
     88  1.7  christos 			printf("d_stdcount=%d\n", dip->d_stdcount);
     89  1.7  christos 			for (int i = 0; i < dip->d_stdcount; i++)
     90  1.7  christos 				printf("d_stdaddrs[%d]=%#x\n",
     91  1.7  christos 				    i, dip->d_stdaddrs[0]);
     92  1.7  christos 		}
     93  1.1  fredette #endif
     94  1.1  fredette 
     95  1.7  christos 		if (dip->d_devbytes && dip->d_stdcount) {
     96  1.7  christos 			if (ctlr >= dip->d_stdcount) {
     97  1.7  christos 				putstr("Invalid controller number\n");
     98  1.7  christos 				return ENXIO;
     99  1.7  christos 			}
    100  1.7  christos 			si->si_devaddr = dev_mapin(dip->d_devtype,
    101  1.7  christos 			    dip->d_stdaddrs[ctlr], dip->d_devbytes);
    102  1.7  christos 			DPRINTF("devaddr=%#x", si->si_devaddr);
    103  1.1  fredette 		}
    104  1.1  fredette 
    105  1.7  christos 		if (dip->d_dmabytes) {
    106  1.7  christos 			si->si_dmaaddr = dvma_alloc(dip->d_dmabytes);
    107  1.7  christos 			DPRINTF("dmaaddr=%#x", si->si_dmaaddr);
    108  1.7  christos 		}
    109  1.1  fredette 
    110  1.7  christos 		if (dip->d_localbytes) {
    111  1.7  christos 			si->si_devdata = alloc(dip->d_localbytes);
    112  1.7  christos 			DPRINTF("devdata=%#x", si->si_devdata);
    113  1.1  fredette 		}
    114  1.1  fredette 	}
    115  1.1  fredette 
    116  1.1  fredette 	/* OK, call the PROM device open routine. */
    117  1.7  christos 	DPRINTF("calling prom open... %p", si);
    118  1.1  fredette 	error = (*ops->b_open)(si);
    119  1.7  christos 	DPRINTF("prom open returned %d", error);
    120  1.1  fredette 	if (error != 0) {
    121  1.7  christos #if 0		/* XXX: printf is too big for bootxx */
    122  1.7  christos 		printf("%s: \"%s\" error=%d\n", __func__,
    123  1.7  christos 		    ops->b_desc, error);
    124  1.2     lukem #else
    125  1.2     lukem 		putstr("prom_iopen: prom open failed");
    126  1.2     lukem #endif
    127  1.7  christos 		return ENXIO;
    128  1.1  fredette 	}
    129  1.1  fredette 
    130  1.1  fredette 	promdev_inuse++;
    131  1.7  christos 	return 0;
    132  1.1  fredette }
    133  1.1  fredette 
    134  1.3       chs void
    135  1.3       chs prom_iclose(struct saioreq *si)
    136  1.1  fredette {
    137  1.1  fredette 	struct boottab *ops;
    138  1.1  fredette 
    139  1.1  fredette 	if (promdev_inuse == 0)
    140  1.1  fredette 		return;
    141  1.1  fredette 
    142  1.1  fredette 	ops = si->si_boottab;
    143  1.1  fredette 
    144  1.7  christos 	DPRINTF("calling prom close... %p", si);
    145  1.1  fredette 	(*ops->b_close)(si);
    146  1.1  fredette 
    147  1.1  fredette 	promdev_inuse = 0;
    148  1.1  fredette }
    149