xref: /netbsd-src/sys/arch/arm/imx/imx31_ahb.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: imx31_ahb.c,v 1.3 2008/05/02 22:00:29 martin Exp $	*/
2 
3 /*
4  * Copyright (c) 2002, 2005  Genetec Corporation.  All rights reserved.
5  * Written by Hiroyuki Bessho for Genetec Corporation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the NetBSD Project by
18  *	Genetec Corporation.
19  * 4. The name of Genetec Corporation may not be used to endorse or
20  *    promote products derived from this software without specific prior
21  *    written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  *
36  * Autoconfiguration support for the Intel PXA2[15]0 application
37  * processor. This code is derived from arm/sa11x0/sa11x0.c
38  */
39 
40 /*-
41  * Copyright (c) 2001, The NetBSD Foundation, Inc.  All rights reserved.
42  *
43  * This code is derived from software contributed to The NetBSD Foundation
44  * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65  * POSSIBILITY OF SUCH DAMAGE.
66  */
67 /*-
68  * Copyright (c) 1999
69  *         Shin Takemura and PocketBSD Project. All rights reserved.
70  *
71  * Redistribution and use in source and binary forms, with or without
72  * modification, are permitted provided that the following conditions
73  * are met:
74  * 1. Redistributions of source code must retain the above copyright
75  *    notice, this list of conditions and the following disclaimer.
76  * 2. Redistributions in binary form must reproduce the above copyright
77  *    notice, this list of conditions and the following disclaimer in the
78  *    documentation and/or other materials provided with the distribution.
79  * 3. All advertising materials mentioning features or use of this software
80  *    must display the following acknowledgement:
81  *	This product includes software developed by the PocketBSD project
82  *	and its contributors.
83  * 4. Neither the name of the project nor the names of its contributors
84  *    may be used to endorse or promote products derived from this software
85  *    without specific prior written permission.
86  *
87  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
88  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
91  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
93  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
94  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97  * SUCH DAMAGE.
98  *
99  */
100 
101 #include <sys/cdefs.h>
102 __KERNEL_RCSID(0, "$Id: imx31_ahb.c,v 1.3 2008/05/02 22:00:29 martin Exp $");
103 
104 #include "locators.h"
105 #include "avic.h"
106 #include "imxgpio.h"
107 
108 #include <sys/param.h>
109 #include <sys/systm.h>
110 #include <sys/device.h>
111 #include <sys/kernel.h>
112 #include <sys/reboot.h>
113 
114 #include <machine/cpu.h>
115 #include <machine/bus.h>
116 
117 #include <arm/cpufunc.h>
118 #include <arm/mainbus/mainbus.h>
119 
120 #include <machine/intr.h>
121 
122 #include <arm/imx/imx31reg.h>
123 #include <arm/imx/imx31var.h>
124 
125 struct ahb_softc {
126 	struct device sc_dev;
127 	bus_dma_tag_t sc_dmat;
128 	bus_space_tag_t sc_memt;
129 	bus_space_handle_t sc_memh;
130 };
131 
132 /* prototypes */
133 static int	ahb_match(device_t, cfdata_t, void *);
134 static void	ahb_attach(device_t, device_t, void *);
135 static int 	ahb_search(device_t, cfdata_t, const int *, void *);
136 static void	ahb_attach_critical(struct ahb_softc *);
137 static int	ahbbus_print(void *, const char *);
138 
139 /* attach structures */
140 CFATTACH_DECL(ahb, sizeof(struct ahb_softc),
141     ahb_match, ahb_attach, NULL, NULL);
142 
143 static struct ahb_softc *ahb_sc;
144 
145 static int
146 ahb_match(device_t parent, cfdata_t match, void *aux)
147 {
148 	return 1;
149 }
150 
151 static void
152 ahb_attach(device_t parent, device_t self, void *aux)
153 {
154 	struct ahb_softc *sc = (struct ahb_softc *)self;
155 	extern struct bus_space imx31_bs_tag;
156 	struct ahb_attach_args ahba;
157 
158 	ahb_sc = sc;
159 	sc->sc_memt = &imx31_bs_tag;
160 #if NBUS_DMA_GENERIC > 0
161 	sc->sc_dmat = &imx31_bus_dma_tag;
162 #else
163 	sc->sc_dmat = 0;
164 #endif
165 
166 	aprint_normal(": AHB-Lite 2.v6 bus interface\n");
167 
168 	/*
169 	 * Attach critical devices
170 	 */
171 	ahb_attach_critical(sc);
172 
173 	/*
174 	 * Attach all other devices
175 	 */
176 	ahba.ahba_name = "ahb";
177 	ahba.ahba_memt = sc->sc_memt;
178 	ahba.ahba_dmat = sc->sc_dmat;
179 	config_search_ia(ahb_search, self, "ahb", &ahba);
180 }
181 
182 static int
183 ahb_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
184 {
185 	struct ahb_attach_args * const ahba = aux;
186 
187 	ahba->ahba_addr = cf->cf_loc[AHBCF_ADDR];
188 	ahba->ahba_size = cf->cf_loc[AHBCF_SIZE];
189 	ahba->ahba_intr = cf->cf_loc[AHBCF_INTR];
190 	ahba->ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
191 
192 	if (config_match(parent, cf, ahba))
193 		config_attach(parent, cf, ahba, ahbbus_print);
194 
195 	return 0;
196 }
197 
198 static int
199 ahb_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
200 {
201 	struct ahb_attach_args * const ahba = aux;
202 	struct ahb_attach_args ahba0;
203 
204 	if (strcmp(ahba->ahba_name, "ahb"))
205 		return 0;
206 	if (ahba->ahba_addr != AHBCF_ADDR_DEFAULT
207 	    && ahba->ahba_addr != cf->cf_loc[AHBCF_ADDR])
208 		return 0;
209 	if (ahba->ahba_size != AHBCF_SIZE_DEFAULT
210 	    && ahba->ahba_size != cf->cf_loc[AHBCF_SIZE])
211 		return 0;
212 	if (ahba->ahba_intr != AHBCF_INTR_DEFAULT
213 	    && ahba->ahba_intr != cf->cf_loc[AHBCF_INTR])
214 		return 0;
215 	if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT
216 	    && ahba->ahba_irqbase != cf->cf_loc[AHBCF_IRQBASE])
217 		return 0;
218 
219 	ahba0 = *ahba;
220 	ahba0.ahba_addr = cf->cf_loc[AHBCF_ADDR];
221 	ahba0.ahba_size = cf->cf_loc[AHBCF_SIZE];
222 	ahba0.ahba_intr = cf->cf_loc[AHBCF_INTR];
223 	ahba0.ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
224 
225 	return config_match(parent, cf, &ahba0);
226 }
227 
228 #if NAVIC == 0
229 #error no avic present in config file
230 #endif
231 
232 static const struct {
233 	const char *name;
234 	bus_addr_t addr;
235 	bool required;
236 } critical_devs[] = {
237 	{ .name = "avic", .addr = INTC_BASE, .required = true },
238 	{ .name = "gpio1", .addr = GPIO1_BASE, .required = false },
239 	{ .name = "gpio2", .addr = GPIO2_BASE, .required = false },
240 	{ .name = "gpio3", .addr = GPIO3_BASE, .required = false },
241 #if 0
242 	{ .name = "dmac", .addr = DMAC_BASE, .required = true },
243 #endif
244 };
245 
246 static void
247 ahb_attach_critical(struct ahb_softc *sc)
248 {
249 	struct ahb_attach_args ahba;
250 	cfdata_t cf;
251 	size_t i;
252 
253 	for (i = 0; i < __arraycount(critical_devs); i++) {
254 		ahba.ahba_name = "ahb";
255 		ahba.ahba_memt = sc->sc_memt;
256 		ahba.ahba_dmat = sc->sc_dmat;
257 
258 		ahba.ahba_addr = critical_devs[i].addr;
259 		ahba.ahba_size = AHBCF_SIZE_DEFAULT;
260 		ahba.ahba_intr = AHBCF_INTR_DEFAULT;
261 		ahba.ahba_irqbase = AHBCF_IRQBASE_DEFAULT;
262 
263 		cf = config_search_ia(ahb_find, &sc->sc_dev, "ahb", &ahba);
264 		if (cf == NULL && critical_devs[i].required)
265 			panic("ahb_attach_critical: failed to find %s!",
266 			    critical_devs[i].name);
267 
268 		ahba.ahba_addr = cf->cf_loc[AHBCF_ADDR];
269 		ahba.ahba_size = cf->cf_loc[AHBCF_SIZE];
270 		ahba.ahba_intr = cf->cf_loc[AHBCF_INTR];
271 		ahba.ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
272 		config_attach(&sc->sc_dev, cf, &ahba, ahbbus_print);
273 	}
274 }
275 
276 static int
277 ahbbus_print(void *aux, const char *name)
278 {
279 	struct ahb_attach_args *ahba = (struct ahb_attach_args*)aux;
280 
281 	if (ahba->ahba_addr != AHBCF_ADDR_DEFAULT) {
282 		aprint_normal(" addr 0x%lx", ahba->ahba_addr);
283 		if (ahba->ahba_size > AHBCF_SIZE_DEFAULT)
284 			aprint_normal("-0x%lx",
285 			    ahba->ahba_addr + ahba->ahba_size-1);
286 	}
287 	if (ahba->ahba_intr != AHBCF_INTR_DEFAULT)
288 		aprint_normal(" intr %d", ahba->ahba_intr);
289 	if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT)
290 		aprint_normal(" irqbase %d", ahba->ahba_irqbase);
291 
292 	return (UNCONF);
293 }
294 
295 static inline uint32_t
296 read_clock_counter_xsc1(void)
297 {
298 	uint32_t x;
299 	__asm volatile("mrc	p14, 0, %0, c1, c0, 0" : "=r" (x) );
300 
301 	return x;
302 }
303 
304 static inline uint32_t
305 read_clock_counter_xsc2(void)
306 {
307 	uint32_t x;
308 	__asm volatile("mrc	p14, 0, %0, c1, c1, 0" : "=r" (x) );
309 
310 	return x;
311 }
312 
313