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