xref: /netbsd-src/sys/arch/arm/imx/imx31_ahb.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: imx31_ahb.c,v 1.9 2021/08/07 16:18:44 thorpej 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.9 2021/08/07 16:18:44 thorpej Exp $");
103 
104 #include "locators.h"
105 #include "avic.h"
106 
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/device.h>
110 #include <sys/kernel.h>
111 #include <sys/reboot.h>
112 
113 #include <machine/cpu.h>
114 #include <sys/bus.h>
115 
116 #include <arm/cpufunc.h>
117 #include <arm/mainbus/mainbus.h>
118 
119 #include <machine/intr.h>
120 
121 #include <arm/imx/imx31reg.h>
122 #include <arm/imx/imx31var.h>
123 
124 struct ahb_softc {
125 	device_t sc_dev;
126 	bus_dma_tag_t sc_dmat;
127 	bus_space_tag_t sc_memt;
128 	bus_space_handle_t sc_memh;
129 };
130 
131 /* prototypes */
132 static int	ahb_match(device_t, cfdata_t, void *);
133 static void	ahb_attach(device_t, device_t, void *);
134 static int 	ahb_search(device_t, cfdata_t, const int *, void *);
135 static void	ahb_attach_critical(struct ahb_softc *);
136 static int	ahbbus_print(void *, const char *);
137 
138 /* attach structures */
139 CFATTACH_DECL_NEW(ahb, sizeof(struct ahb_softc),
140     ahb_match, ahb_attach, NULL, NULL);
141 
142 static struct ahb_softc *ahb_sc;
143 
144 static int
ahb_match(device_t parent,cfdata_t match,void * aux)145 ahb_match(device_t parent, cfdata_t match, void *aux)
146 {
147 	return 1;
148 }
149 
150 static void
ahb_attach(device_t parent,device_t self,void * aux)151 ahb_attach(device_t parent, device_t self, void *aux)
152 {
153 	struct ahb_softc *sc = device_private(self);
154 	struct ahb_attach_args ahba;
155 
156 	ahb_sc = sc;
157 	sc->sc_dev = self;
158 	sc->sc_memt = &imx_bs_tag;
159 #if NBUS_DMA_GENERIC > 0
160 	sc->sc_dmat = &imx_bus_dma_tag;
161 #else
162 	sc->sc_dmat = 0;
163 #endif
164 
165 	aprint_normal(": AHB-Lite 2.v6 bus interface\n");
166 
167 	/*
168 	 * Attach critical devices
169 	 */
170 	ahb_attach_critical(sc);
171 
172 	/*
173 	 * Attach all other devices
174 	 */
175 	ahba.ahba_name = "ahb";
176 	ahba.ahba_memt = sc->sc_memt;
177 	ahba.ahba_dmat = sc->sc_dmat;
178 	config_search(self, &ahba,
179 	    CFARGS(.search = ahb_search));
180 }
181 
182 static int
ahb_search(device_t parent,cfdata_t cf,const int * ldesc,void * aux)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_probe(parent, cf, ahba))
193 		config_attach(parent, cf, ahba, ahbbus_print, CFARGS_NONE);
194 
195 	return 0;
196 }
197 
198 static int
ahb_find(device_t parent,cfdata_t cf,const int * ldesc,void * aux)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
ahb_attach_critical(struct ahb_softc * sc)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(sc->sc_dev, &ahba,
264 		    CFARGS(.submatch = ahb_find));
265 		if (cf == NULL && critical_devs[i].required)
266 			panic("ahb_attach_critical: failed to find %s!",
267 			    critical_devs[i].name);
268 
269 		ahba.ahba_addr = cf->cf_loc[AHBCF_ADDR];
270 		ahba.ahba_size = cf->cf_loc[AHBCF_SIZE];
271 		ahba.ahba_intr = cf->cf_loc[AHBCF_INTR];
272 		ahba.ahba_irqbase = cf->cf_loc[AHBCF_IRQBASE];
273 		config_attach(sc->sc_dev, cf, &ahba, ahbbus_print, CFARGS_NONE);
274 	}
275 }
276 
277 static int
ahbbus_print(void * aux,const char * name)278 ahbbus_print(void *aux, const char *name)
279 {
280 	struct ahb_attach_args *ahba = aux;
281 
282 	if (ahba->ahba_addr != AHBCF_ADDR_DEFAULT) {
283 		aprint_normal(" addr 0x%lx", ahba->ahba_addr);
284 		if (ahba->ahba_size > AHBCF_SIZE_DEFAULT)
285 			aprint_normal("-0x%lx",
286 			    ahba->ahba_addr + ahba->ahba_size-1);
287 	}
288 	if (ahba->ahba_intr != AHBCF_INTR_DEFAULT)
289 		aprint_normal(" intr %d", ahba->ahba_intr);
290 	if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT)
291 		aprint_normal(" irqbase %d", ahba->ahba_irqbase);
292 
293 	return (UNCONF);
294 }
295 
296 static inline uint32_t
read_clock_counter_xsc1(void)297 read_clock_counter_xsc1(void)
298 {
299 	uint32_t x;
300 	__asm volatile("mrc	p14, 0, %0, c1, c0, 0" : "=r" (x) );
301 
302 	return x;
303 }
304 
305 static inline uint32_t
read_clock_counter_xsc2(void)306 read_clock_counter_xsc2(void)
307 {
308 	uint32_t x;
309 	__asm volatile("mrc	p14, 0, %0, c1, c1, 0" : "=r" (x) );
310 
311 	return x;
312 }
313 
314