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