pxa2x0.c revision 1.1 1 1.1 bsh /* $NetBSD: pxa2x0.c,v 1.1 2002/10/19 19:31:38 bsh Exp $ */
2 1.1 bsh
3 1.1 bsh /*
4 1.1 bsh * Copyright (c) 2002 Genetec Corporation. All rights reserved.
5 1.1 bsh * Written by Hiroyuki Bessho for Genetec Corporation.
6 1.1 bsh *
7 1.1 bsh * Redistribution and use in source and binary forms, with or without
8 1.1 bsh * modification, are permitted provided that the following conditions
9 1.1 bsh * are met:
10 1.1 bsh * 1. Redistributions of source code must retain the above copyright
11 1.1 bsh * notice, this list of conditions and the following disclaimer.
12 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bsh * notice, this list of conditions and the following disclaimer in the
14 1.1 bsh * documentation and/or other materials provided with the distribution.
15 1.1 bsh * 3. All advertising materials mentioning features or use of this software
16 1.1 bsh * must display the following acknowledgement:
17 1.1 bsh * This product includes software developed for the NetBSD Project by
18 1.1 bsh * Genetec Corporation.
19 1.1 bsh * 4. The name of Genetec Corporation may not be used to endorse or
20 1.1 bsh * promote products derived from this software without specific prior
21 1.1 bsh * written permission.
22 1.1 bsh *
23 1.1 bsh * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
24 1.1 bsh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 bsh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 bsh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
27 1.1 bsh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 bsh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 bsh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 bsh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 bsh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 bsh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 bsh * POSSIBILITY OF SUCH DAMAGE.
34 1.1 bsh *
35 1.1 bsh *
36 1.1 bsh * Autoconfiguration support for the Intel PXA2[15]0 application
37 1.1 bsh * processor. This code is derived from arm/sa11x0/sa11x0.c
38 1.1 bsh */
39 1.1 bsh
40 1.1 bsh /*-
41 1.1 bsh * Copyright (c) 2001, The NetBSD Foundation, Inc. All rights reserved.
42 1.1 bsh *
43 1.1 bsh * This code is derived from software contributed to The NetBSD Foundation
44 1.1 bsh * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
45 1.1 bsh *
46 1.1 bsh * Redistribution and use in source and binary forms, with or without
47 1.1 bsh * modification, are permitted provided that the following conditions
48 1.1 bsh * are met:
49 1.1 bsh * 1. Redistributions of source code must retain the above copyright
50 1.1 bsh * notice, this list of conditions and the following disclaimer.
51 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 bsh * notice, this list of conditions and the following disclaimer in the
53 1.1 bsh * documentation and/or other materials provided with the distribution.
54 1.1 bsh * 3. All advertising materials mentioning features or use of this software
55 1.1 bsh * must display the following acknowledgement:
56 1.1 bsh * This product includes software developed by the NetBSD
57 1.1 bsh * Foundation, Inc. and its contributors.
58 1.1 bsh * 4. Neither the name of The NetBSD Foundation nor the names of its
59 1.1 bsh * contributors may be used to endorse or promote products derived
60 1.1 bsh * from this software without specific prior written permission.
61 1.1 bsh */
62 1.1 bsh /*-
63 1.1 bsh * Copyright (c) 1999
64 1.1 bsh * Shin Takemura and PocketBSD Project. All rights reserved.
65 1.1 bsh *
66 1.1 bsh * Redistribution and use in source and binary forms, with or without
67 1.1 bsh * modification, are permitted provided that the following conditions
68 1.1 bsh * are met:
69 1.1 bsh * 1. Redistributions of source code must retain the above copyright
70 1.1 bsh * notice, this list of conditions and the following disclaimer.
71 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright
72 1.1 bsh * notice, this list of conditions and the following disclaimer in the
73 1.1 bsh * documentation and/or other materials provided with the distribution.
74 1.1 bsh * 3. All advertising materials mentioning features or use of this software
75 1.1 bsh * must display the following acknowledgement:
76 1.1 bsh * This product includes software developed by the PocketBSD project
77 1.1 bsh * and its contributors.
78 1.1 bsh * 4. Neither the name of the project nor the names of its contributors
79 1.1 bsh * may be used to endorse or promote products derived from this software
80 1.1 bsh * without specific prior written permission.
81 1.1 bsh *
82 1.1 bsh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 1.1 bsh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 1.1 bsh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 1.1 bsh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 1.1 bsh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 1.1 bsh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 1.1 bsh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 1.1 bsh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 1.1 bsh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 1.1 bsh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 1.1 bsh * SUCH DAMAGE.
93 1.1 bsh *
94 1.1 bsh */
95 1.1 bsh
96 1.1 bsh #include <sys/param.h>
97 1.1 bsh #include <sys/systm.h>
98 1.1 bsh #include <sys/device.h>
99 1.1 bsh #include <sys/kernel.h>
100 1.1 bsh #include <sys/reboot.h>
101 1.1 bsh
102 1.1 bsh #include <machine/cpu.h>
103 1.1 bsh #include <machine/bus.h>
104 1.1 bsh
105 1.1 bsh #include <arm/cpufunc.h>
106 1.1 bsh #include <arm/mainbus/mainbus.h>
107 1.1 bsh #include <arm/xscale/pxa2x0reg.h>
108 1.1 bsh #include <arm/xscale/pxa2x0var.h>
109 1.1 bsh
110 1.1 bsh #include "locators.h"
111 1.1 bsh
112 1.1 bsh /* prototypes */
113 1.1 bsh static int pxa2x0_match(struct device *, struct cfdata *, void *);
114 1.1 bsh static void pxa2x0_attach(struct device *, struct device *, void *);
115 1.1 bsh static int pxa2x0_search(struct device *, struct cfdata *, void *);
116 1.1 bsh
117 1.1 bsh /* attach structures */
118 1.1 bsh CFATTACH_DECL(pxaip, sizeof(struct pxa2x0_softc), pxa2x0_match, pxa2x0_attach,
119 1.1 bsh NULL, NULL);
120 1.1 bsh
121 1.1 bsh extern struct bus_space pxa2x0_bs_tag;
122 1.1 bsh
123 1.1 bsh struct pxa2x0_softc *pxa2x0_softc;
124 1.1 bsh
125 1.1 bsh static int
126 1.1 bsh pxa2x0_print(void *aux, const char *name)
127 1.1 bsh {
128 1.1 bsh struct pxa2x0_attach_args *sa = (struct pxa2x0_attach_args*)aux;
129 1.1 bsh
130 1.1 bsh if (sa->pxa_size)
131 1.1 bsh printf(" addr 0x%lx", sa->pxa_addr);
132 1.1 bsh if (sa->pxa_size > 1)
133 1.1 bsh printf("-0x%lx", sa->pxa_addr + sa->pxa_size - 1);
134 1.1 bsh if (sa->pxa_intr > 1)
135 1.1 bsh printf(" intr %d", sa->pxa_intr);
136 1.1 bsh if (sa->pxa_gpio != -1)
137 1.1 bsh printf(" gpio %d", sa->pxa_gpio);
138 1.1 bsh
139 1.1 bsh return (UNCONF);
140 1.1 bsh }
141 1.1 bsh
142 1.1 bsh int
143 1.1 bsh pxa2x0_match(struct device *parent, struct cfdata *match, void *aux)
144 1.1 bsh {
145 1.1 bsh return 1;
146 1.1 bsh }
147 1.1 bsh
148 1.1 bsh void
149 1.1 bsh pxa2x0_attach(struct device *parent, struct device *self, void *aux)
150 1.1 bsh {
151 1.1 bsh struct pxa2x0_softc *sc = (struct pxa2x0_softc*)self;
152 1.1 bsh bus_space_tag_t iot;
153 1.1 bsh const char *which_registers; /* for panic message */
154 1.1 bsh
155 1.1 bsh #define FAIL(which) do { \
156 1.1 bsh which_registers=(which); goto abort; }while(/*CONSTCOND*/0)
157 1.1 bsh
158 1.1 bsh pxa2x0_softc = sc;
159 1.1 bsh sc->saip.sc_iot = iot = &pxa2x0_bs_tag;
160 1.1 bsh
161 1.1 bsh if (bus_space_map(iot,
162 1.1 bsh PXA2X0_INTCTL_BASE, PXA2X0_INTCTL_SIZE,
163 1.1 bsh 0, &sc->saip.sc_ioh))
164 1.1 bsh FAIL("intc");
165 1.1 bsh
166 1.1 bsh /* Map the GPIO registers */
167 1.1 bsh if (bus_space_map(iot, PXA2X0_GPIO_BASE, PXA2X0_GPIO_SIZE,
168 1.1 bsh 0, &sc->saip.sc_gpioh))
169 1.1 bsh FAIL("GPIO");
170 1.1 bsh
171 1.1 bsh /* Map the DMA controller registers */
172 1.1 bsh if (bus_space_map(iot, PXA2X0_DMAC_BASE, PXA2X0_DMAC_SIZE,
173 1.1 bsh 0, &sc->saip.sc_dmach))
174 1.1 bsh FAIL("DMAC");
175 1.1 bsh
176 1.1 bsh /* Memory controller */
177 1.1 bsh if( bus_space_map(iot, PXA2X0_MEMCTL_BASE,
178 1.1 bsh PXA2X0_MEMCTL_SIZE, 0, &sc->sc_memctl_ioh) )
179 1.1 bsh FAIL("MEMC");
180 1.1 bsh /* Clock manager */
181 1.1 bsh if( bus_space_map(iot, PXA2X0_CLKMAN_BASE,
182 1.1 bsh PXA2X0_CLKMAN_SIZE, 0, &sc->sc_clkman_ioh) )
183 1.1 bsh FAIL("CLK");
184 1.1 bsh
185 1.1 bsh /* Real time clock */
186 1.1 bsh if( bus_space_map(iot, PXA2X0_RTC_BASE,
187 1.1 bsh PXA2X0_RTC_SIZE, 0, &sc->sc_rtc_ioh) )
188 1.1 bsh FAIL("RTC");
189 1.1 bsh
190 1.1 bsh #if 1
191 1.1 bsh /* This takes 2 secs at most. */
192 1.1 bsh {
193 1.1 bsh int cpuclock;
194 1.1 bsh
195 1.1 bsh cpuclock = pxa2x0_measure_cpuclock( sc ) / 1000;
196 1.1 bsh printf( " CPU clock = %d.%03d MHz", cpuclock/1000, cpuclock%1000 );
197 1.1 bsh }
198 1.1 bsh #endif
199 1.1 bsh printf("\n");
200 1.1 bsh
201 1.1 bsh pxa2x0_set_intcbase(sc->saip.sc_ioh);
202 1.1 bsh pxa2x0_intr_init();
203 1.1 bsh
204 1.1 bsh /*
205 1.1 bsh * Attach devices.
206 1.1 bsh */
207 1.1 bsh config_search(pxa2x0_search, self, NULL);
208 1.1 bsh return;
209 1.1 bsh
210 1.1 bsh abort:
211 1.1 bsh panic("%s: unable to map %s registers\n",
212 1.1 bsh self->dv_xname, which_registers);
213 1.1 bsh
214 1.1 bsh #undef FAIL
215 1.1 bsh }
216 1.1 bsh
217 1.1 bsh int
218 1.1 bsh pxa2x0_search(struct device *parent, struct cfdata *cf, void *aux)
219 1.1 bsh {
220 1.1 bsh struct pxa2x0_softc *sc = (struct pxa2x0_softc *)parent;
221 1.1 bsh struct pxa2x0_attach_args aa;
222 1.1 bsh
223 1.1 bsh aa.pxa_sc = sc;
224 1.1 bsh aa.pxa_iot = sc->saip.sc_iot;
225 1.1 bsh aa.pxa_addr = cf->cf_loc[PXAIPCF_ADDR];
226 1.1 bsh aa.pxa_size = cf->cf_loc[PXAIPCF_SIZE];
227 1.1 bsh aa.pxa_index = cf->cf_loc[PXAIPCF_INDEX];
228 1.1 bsh aa.pxa_sa.sa_membase = 0;
229 1.1 bsh aa.pxa_sa.sa_memsize = 0;
230 1.1 bsh aa.pxa_intr = cf->cf_loc[PXAIPCF_INTR];
231 1.1 bsh aa.pxa_gpio = cf->cf_loc[PXAIPCF_GPIO];
232 1.1 bsh
233 1.1 bsh if (config_match(parent, cf, &aa))
234 1.1 bsh config_attach(parent, cf, &aa, pxa2x0_print);
235 1.1 bsh
236 1.1 bsh return 0;
237 1.1 bsh }
238 1.1 bsh
239 1.1 bsh static inline uint32_t
240 1.1 bsh read_clock_counter(void)
241 1.1 bsh {
242 1.1 bsh uint32_t x;
243 1.1 bsh __asm __volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (x) );
244 1.1 bsh
245 1.1 bsh return x;
246 1.1 bsh }
247 1.1 bsh
248 1.1 bsh int
249 1.1 bsh pxa2x0_measure_cpuclock( struct pxa2x0_softc *sc )
250 1.1 bsh {
251 1.1 bsh uint32_t rtc0, rtc1, start, end;
252 1.1 bsh uint32_t pmcr_save;
253 1.1 bsh bus_space_tag_t iot = sc->saip.sc_iot;
254 1.1 bsh bus_space_handle_t rtc_ioh = sc->sc_rtc_ioh;
255 1.1 bsh int irq = disable_interrupts(I32_bit|F32_bit);
256 1.1 bsh
257 1.1 bsh __asm __volatile( "mrc p14, 0, %0, c0, c0, 0" : "=r" (pmcr_save) );
258 1.1 bsh /* Enable clock counter */
259 1.1 bsh __asm __volatile( "mcr p14, 0, %0, c0, c0, 0" : : "r" (0x0001) );
260 1.1 bsh
261 1.1 bsh rtc0 = bus_space_read_4( iot, rtc_ioh, RTC_RCNR );
262 1.1 bsh /* Wait for next second starts */
263 1.1 bsh while( (rtc1 = bus_space_read_4( iot, rtc_ioh, RTC_RCNR )) == rtc0 )
264 1.1 bsh ;
265 1.1 bsh start = read_clock_counter();
266 1.1 bsh while( rtc1 == bus_space_read_4( iot, rtc_ioh, RTC_RCNR ) )
267 1.1 bsh ; /* Wait for 1sec */
268 1.1 bsh end = read_clock_counter();
269 1.1 bsh
270 1.1 bsh __asm __volatile( "mcr p14, 0, %0, c0, c0, 0" : : "r" (pmcr_save) );
271 1.1 bsh restore_interrupts(irq);
272 1.1 bsh
273 1.1 bsh return end - start;
274 1.1 bsh }
275 1.1 bsh
276 1.1 bsh void
277 1.1 bsh pxa2x0_turbo_mode( int f )
278 1.1 bsh {
279 1.1 bsh __asm __volatile("mcr p14, 0, %0, c6, c0, 0" : : "r" (f));
280 1.1 bsh }
281