xref: /netbsd-src/sys/dev/hpc/hpcioman.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: hpcioman.c,v 1.11 2004/09/13 12:55:47 drochner Exp $ */
2 
3 /*-
4  * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: hpcioman.c,v 1.11 2004/09/13 12:55:47 drochner Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 
46 #include <machine/config_hook.h>
47 #include <machine/platid.h>
48 #include <machine/platid_mask.h>
49 
50 #include <machine/bus.h>
51 #include <dev/hpc/hpciovar.h>
52 #include <dev/hpc/hpciomanvar.h>
53 
54 #include "locators.h"
55 
56 int	hpcioman_match(struct device *, struct cfdata *, void *);
57 void	hpcioman_attach(struct device *, struct device *, void *);
58 int	hpcioman_print(void *, const char *);
59 int	hpcioman_search(struct device *, struct cfdata *,
60 			const locdesc_t *, void *);
61 
62 struct hpcioman_softc {
63 	struct device sc_dev;
64 };
65 
66 CFATTACH_DECL(hpcioman, sizeof(struct hpcioman_softc),
67     hpcioman_match, hpcioman_attach, NULL, NULL);
68 
69 int
70 hpcioman_match(struct device *parent, struct cfdata *cf, void *aux)
71 {
72 	struct hpcio_attach_args *haa = aux;
73 	platid_mask_t mask;
74 
75 	if (strcmp(haa->haa_busname, HPCIO_BUSNAME))
76 		return (0);
77 	/* select platform */
78 	mask = PLATID_DEREF(cf->cf_loc[HPCIOIFCF_PLATFORM]);
79 
80 	return (platid_match(&platid, &mask));
81 }
82 
83 void
84 hpcioman_attach(struct device *parent, struct device *self, void *aux)
85 {
86 	printf("\n");
87 
88 	config_search_ia(hpcioman_search, self, "hpcioman", aux);
89 }
90 
91 int
92 hpcioman_search(struct device *parent, struct cfdata *cf,
93 		const locdesc_t *ldesc, void *aux)
94 {
95 	//struct hpcioman_softc *sc = (struct hpcioman_softc *)parent;
96 	struct hpcio_attach_args *haa = aux;
97 	struct hpcioman_attach_args hma;
98 
99 	/* get io chip */
100 	hma.hma_hc = (*haa->haa_getchip)(haa->haa_sc, cf->cf_iochip);
101 
102 	/* interrupt mode */
103 	if (cf->cf_level != HPCIOMANCF_LEVEL_DEFAULT) {
104 		switch (cf->cf_hold) {
105 		case 1:
106 			hma.hma_intr_mode = HPCIO_INTR_HOLD;
107 			break;
108 		case 0:
109 		case HPCIOMANCF_HOLD_DEFAULT:
110 		default:
111 			hma.hma_intr_mode = HPCIO_INTR_THROUGH;
112 			break;
113 		}
114 		hma.hma_intr_mode |= HPCIO_INTR_LEVEL;
115 		if (cf->cf_level == 0)
116 			hma.hma_intr_mode |= HPCIO_INTR_LOW;
117 		else
118 			hma.hma_intr_mode |= HPCIO_INTR_HIGH;
119 	} else {
120 		switch (cf->cf_hold) {
121 		case 0:
122 			hma.hma_intr_mode = HPCIO_INTR_THROUGH;
123 			break;
124 		case 1:
125 		case HPCIOMANCF_HOLD_DEFAULT:
126 		default:
127 			hma.hma_intr_mode = HPCIO_INTR_HOLD;
128 			break;
129 		}
130 		hma.hma_intr_mode |= HPCIO_INTR_EDGE;
131 		switch (cf->cf_edge) {
132 		case 1:
133 			hma.hma_intr_mode |= HPCIO_INTR_POSEDGE;
134 			break;
135 		case 2:
136 			hma.hma_intr_mode |= HPCIO_INTR_NEGEDGE;
137 			break;
138 		case 0:
139 		case 3:
140 		case HPCIOMANCF_EDGE_DEFAULT:
141 			hma.hma_intr_mode |= HPCIO_INTR_POSEDGE;
142 			hma.hma_intr_mode |= HPCIO_INTR_NEGEDGE;
143 			break;
144 		default:
145 			printf("%s(%d): invalid configuration, edge=%d",
146 			    __FILE__, __LINE__, cf->cf_edge);
147 			break;
148 		}
149 	}
150 
151 	hma.hma_type = cf->cf_type;
152 	hma.hma_id = cf->cf_id;
153 	hma.hma_port = cf->cf_port;
154 	if (cf->cf_active == 0) {
155 		hma.hma_on = 0;
156 		hma.hma_off = 1;
157 	} else {
158 		hma.hma_on = 1;
159 		hma.hma_off = 0;
160 	}
161 	hma.hma_initvalue = -1; /* none */
162 	if (cf->cf_initvalue != HPCIOMANCF_INITVALUE_DEFAULT) {
163 		if (cf->cf_initvalue == 0)
164 			hma.hma_initvalue = hma.hma_off;
165 		else
166 			hma.hma_initvalue = hma.hma_on;
167 	}
168 	hma.hma_connect = cf->cf_connect;
169 
170 	config_attach(parent, cf, &hma, hpcioman_print);
171 
172 	return (0);
173 }
174 
175 int
176 hpcioman_print(void *aux, const char *pnp)
177 {
178 	struct hpcioman_attach_args *hma = aux;
179 	int type = hma->hma_type;
180 
181 	if (!pnp)  {
182 		aprint_normal(" iochip %s, port %d, type %d, id %d",
183 		    hma->hma_hc ? hma->hma_hc->hc_name : "not found",
184 		    hma->hma_port, type, hma->hma_id);
185 		if (type == CONFIG_HOOK_BUTTONEVENT ||
186 		    type == CONFIG_HOOK_PMEVENT ||
187 		    type == CONFIG_HOOK_EVENT ||
188 		    type == CONFIG_HOOK_PCIINTR) {
189 			if (hma->hma_intr_mode & HPCIO_INTR_EDGE)
190 				aprint_normal (", interrupt edge [%s%s]",
191 				    (hma->hma_intr_mode&HPCIO_INTR_POSEDGE)
192 				    ? "p" : "",
193 				    (hma->hma_intr_mode&HPCIO_INTR_NEGEDGE)
194 				    ? "n" : "");
195 			else
196 				aprint_normal (", interrupt level %s",
197 				    (hma->hma_intr_mode&HPCIO_INTR_HIGH) ?
198 				    "high" : "low");
199 		}
200 		if (hma->hma_initvalue != -1)
201 			aprint_normal(", initial value %d", hma->hma_initvalue);
202 		if (hma->hma_on == 0)
203 			aprint_normal(", active low");
204 	}
205 
206 	return (QUIET);
207 }
208