xref: /netbsd-src/sys/arch/hpcmips/dev/plum.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: plum.c,v 1.9 2003/11/07 23:04:15 he Exp $ */
2 
3 /*-
4  * Copyright (c) 1999 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: plum.c,v 1.9 2003/11/07 23:04:15 he Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/malloc.h>
46 
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49 
50 #include <hpcmips/tx/tx39var.h>
51 #include <hpcmips/tx/txcsbusvar.h>
52 #include <hpcmips/dev/plumvar.h>
53 #include <hpcmips/dev/plumreg.h>
54 
55 int plum_match(struct device *, struct cfdata *, void *);
56 void plum_attach(struct device *, struct device *, void *);
57 int plum_print(void *, const char *);
58 int plum_search(struct device *, struct cfdata *, void *);
59 
60 struct plum_softc {
61 	struct	device		sc_dev;
62 	plum_chipset_tag_t	sc_pc;
63 	bus_space_tag_t		sc_csregt;
64 	bus_space_tag_t		sc_csiot;
65 	bus_space_tag_t		sc_csmemt;
66 	int			sc_irq;
67 	int			sc_pri;
68 };
69 
70 CFATTACH_DECL(plum, sizeof(struct plum_softc),
71     plum_match, plum_attach, NULL, NULL);
72 
73 plumreg_t plum_idcheck(bus_space_tag_t);
74 
75 int
76 plum_match(struct device *parent, struct cfdata *cf, void *aux)
77 {
78 	struct cs_attach_args *ca = aux;
79 
80 	switch (plum_idcheck(ca->ca_csreg.cstag)) {
81 	default:
82 		return (0);
83 	case PLUM2_1:
84 	case PLUM2_2:
85 		/* nothing */;
86 	}
87 
88 	return (1);
89 }
90 
91 void
92 plum_attach(struct device *parent, struct device *self, void *aux)
93 {
94 	struct cs_attach_args *ca = aux;
95 	struct plum_softc *sc = (void*)self;
96 	plumreg_t reg;
97 
98 	sc->sc_csregt	= ca->ca_csreg.cstag;
99 	sc->sc_csiot	= ca->ca_csio.cstag;
100 	sc->sc_csmemt	= ca->ca_csmem.cstag;
101 	sc->sc_irq	= ca->ca_irq1;
102 
103 	switch (reg = plum_idcheck(sc->sc_csregt)) {
104 	default:
105 		printf(": unknown revision %#x\n", reg);
106 		return;
107 	case PLUM2_1:
108 		printf(": Plum2 #1\n");
109 		break;
110 	case PLUM2_2:
111 		printf(": Plum2 #2\n");
112 		break;
113 	}
114 	if (!(sc->sc_pc = malloc(sizeof(struct plum_chipset_tag),
115 	    M_DEVBUF, M_NOWAIT))) {
116 		panic("no memory");
117 	}
118 	memset(sc->sc_pc, 0, sizeof(struct plum_chipset_tag));
119 	sc->sc_pc->pc_tc = ca->ca_tc;
120 
121 	/* Attach Plum devices */
122 	/*
123 	 * interrupt, power/clock module is used by other plum module.
124 	 * attach first.
125 	 */
126 	sc->sc_pri = 2;
127 	config_search(plum_search, self, plum_print);
128 	/*
129 	 * Other plum module.
130 	 */
131 	sc->sc_pri = 1;
132 	config_search(plum_search, self, plum_print);
133 }
134 
135 plumreg_t
136 plum_idcheck(bus_space_tag_t regt)
137 {
138 	bus_space_handle_t regh;
139 	plumreg_t reg;
140 
141 	if (bus_space_map(regt, PLUM_ID_REGBASE,
142 	    PLUM_ID_REGSIZE, 0, &regh)) {
143 		printf("ID register map failed\n");
144 		return (0);
145 	}
146 	reg = plum_conf_read(regt, regh, PLUM_ID_REG);
147 	bus_space_unmap(regt, regh, PLUM_ID_REGSIZE);
148 
149 	return (reg);
150 }
151 
152 int
153 plum_print(void *aux, const char *pnp)
154 {
155 
156 	return (pnp ? QUIET : UNCONF);
157 }
158 
159 int
160 plum_search(struct device *parent, struct cfdata *cf, void *aux)
161 {
162 	struct plum_softc *sc = (void*)parent;
163 	struct plum_attach_args pa;
164 
165 	pa.pa_pc	= sc->sc_pc;
166 	pa.pa_regt	= sc->sc_csregt;
167 	pa.pa_iot	= sc->sc_csiot;
168 	pa.pa_memt	= sc->sc_csmemt;
169 	pa.pa_irq	= sc->sc_irq;
170 
171 	if (config_match(parent, cf, &pa) == sc->sc_pri) {
172 		config_attach(parent, cf, &pa, plum_print);
173 	}
174 
175 	return 0;
176 }
177 
178 plumreg_t
179 plum_conf_read(bus_space_tag_t t, bus_space_handle_t h, int o)
180 {
181 	plumreg_t reg;
182 
183 	reg = bus_space_read_4(t, h, o);
184 
185 	return (reg);
186 }
187 
188 void
189 plum_conf_write(bus_space_tag_t t, bus_space_handle_t h, int o, plumreg_t v)
190 {
191 
192 	bus_space_write_4(t, h, o, v);
193 }
194 
195 void
196 plum_conf_register_intr(plum_chipset_tag_t t, void *intrt)
197 {
198 
199 	if (t->pc_intrt) {
200 		panic("duplicate intrt");
201 	}
202 
203 	t->pc_intrt = intrt;
204 }
205 
206 void
207 plum_conf_register_power(plum_chipset_tag_t t, void *powert)
208 {
209 
210 	if (t->pc_powert) {
211 		panic("duplicate powert");
212 	}
213 
214 	t->pc_powert = powert;
215 }
216