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