pxa2x0.c revision 1.5 1 /* $NetBSD: pxa2x0.c,v 1.5 2003/12/12 16:42:44 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2002 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 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the NetBSD
57 * Foundation, Inc. and its contributors.
58 * 4. Neither the name of The NetBSD Foundation nor the names of its
59 * contributors may be used to endorse or promote products derived
60 * from this software without specific prior written permission.
61 */
62 /*-
63 * Copyright (c) 1999
64 * Shin Takemura and PocketBSD Project. All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. All advertising materials mentioning features or use of this software
75 * must display the following acknowledgement:
76 * This product includes software developed by the PocketBSD project
77 * and its contributors.
78 * 4. Neither the name of the project nor the names of its contributors
79 * may be used to endorse or promote products derived from this software
80 * without specific prior written permission.
81 *
82 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 * SUCH DAMAGE.
93 *
94 */
95
96 #include <sys/cdefs.h>
97 __KERNEL_RCSID(0, "$NetBSD: pxa2x0.c,v 1.5 2003/12/12 16:42:44 thorpej Exp $");
98
99 #include "pxaintc.h"
100 #include "pxagpio.h"
101 #if 0
102 #include "pxadmac.h" /* Not yet */
103 #endif
104
105 #include "locators.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 <machine/bus.h>
115
116 #include <arm/cpufunc.h>
117 #include <arm/mainbus/mainbus.h>
118 #include <arm/xscale/pxa2x0reg.h>
119 #include <arm/xscale/pxa2x0var.h>
120
121 struct pxaip_softc {
122 struct device sc_dev;
123 bus_space_tag_t sc_bust;
124 bus_dma_tag_t sc_dmat;
125 bus_space_handle_t sc_bush_clk;
126 };
127
128 /* prototypes */
129 static int pxaip_match(struct device *, struct cfdata *, void *);
130 static void pxaip_attach(struct device *, struct device *, void *);
131 static int pxaip_search(struct device *, struct cfdata *, void *);
132 static void pxaip_attach_critical(struct pxaip_softc *);
133 static int pxaip_print(void *, const char *);
134
135 static int pxaip_measure_cpuclock(struct pxaip_softc *);
136
137 /* attach structures */
138 CFATTACH_DECL(pxaip, sizeof(struct pxaip_softc),
139 pxaip_match, pxaip_attach, NULL, NULL);
140
141 static struct pxaip_softc *pxaip_sc;
142
143 static int
144 pxaip_match(struct device *parent, struct cfdata *match, void *aux)
145 {
146
147 return 1;
148 }
149
150 static void
151 pxaip_attach(struct device *parent, struct device *self, void *aux)
152 {
153 struct pxaip_softc *sc = (struct pxaip_softc *)self;
154 int cpuclock;
155
156 pxaip_sc = sc;
157 sc->sc_bust = &pxa2x0_bs_tag;
158 sc->sc_dmat = &pxa2x0_bus_dma_tag;
159
160 aprint_normal(": PXA2x0 Onchip Peripheral Bus\n");
161
162 if (bus_space_map(sc->sc_bust, PXA2X0_CLKMAN_BASE, PXA2X0_CLKMAN_SIZE,
163 0, &sc->sc_bush_clk))
164 panic("pxaip_attach: failed to map CLKMAN");
165
166 /*
167 * Calculate clock speed
168 * This takes 2 secs at most.
169 */
170 cpuclock = pxaip_measure_cpuclock(sc) / 1000;
171 printf("%s: CPU clock = %d.%03d MHz\n", self->dv_xname,
172 cpuclock/1000, cpuclock%1000 );
173
174 /*
175 * Attach critical devices
176 */
177 pxaip_attach_critical(sc);
178
179 /*
180 * Attach all other devices
181 */
182 config_search(pxaip_search, self, sc);
183 }
184
185 static int
186 pxaip_search(struct device *parent, struct cfdata *cf, void *aux)
187 {
188 struct pxaip_softc *sc = aux;
189 struct pxaip_attach_args aa;
190
191 aa.pxa_iot = sc->sc_bust;
192 aa.pxa_dmat = sc->sc_dmat;
193 aa.pxa_addr = cf->cf_loc[PXAIPCF_ADDR];
194 aa.pxa_size = cf->cf_loc[PXAIPCF_SIZE];
195 aa.pxa_index = cf->cf_loc[PXAIPCF_INDEX];
196 aa.pxa_intr = cf->cf_loc[PXAIPCF_INTR];
197
198 if (config_match(parent, cf, &aa))
199 config_attach(parent, cf, &aa, pxaip_print);
200
201 return 0;
202 }
203
204 static void
205 pxaip_attach_critical(struct pxaip_softc *sc)
206 {
207 struct pxaip_attach_args aa;
208
209 aa.pxa_iot = sc->sc_bust;
210 aa.pxa_dmat = sc->sc_dmat;
211 aa.pxa_addr = PXA2X0_INTCTL_BASE;
212 aa.pxa_size = PXA2X0_INTCTL_SIZE;
213 aa.pxa_intr = PXAIPCF_INTR_DEFAULT;
214 if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
215 panic("pxaip_attach_critical: failed to attach INTC!");
216
217 #if NPXAGPIO > 0
218 aa.pxa_iot = sc->sc_bust;
219 aa.pxa_dmat = sc->sc_dmat;
220 aa.pxa_addr = PXA2X0_GPIO_BASE;
221 aa.pxa_size = PXA2X0_GPIO_SIZE;
222 aa.pxa_intr = PXAIPCF_INTR_DEFAULT;
223 if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
224 panic("pxaip_attach_critical: failed to attach GPIO!");
225 #endif
226
227 #if NPXADMAC > 0
228 aa.pxa_iot = sc->sc_bust;
229 aa.pxa_dmat = sc->sc_dmat;
230 aa.pxa_addr = PXA2X0_DMAC_BASE;
231 aa.pxa_size = PXA2X0_DMAC_SIZE;
232 aa.pxa_intr = PXA2X0_INT_DMA;
233 if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
234 panic("pxaip_attach_critical: failed to attach DMAC!");
235 #endif
236 }
237
238 static int
239 pxaip_print(void *aux, const char *name)
240 {
241 struct pxaip_attach_args *sa = (struct pxaip_attach_args*)aux;
242
243 if (sa->pxa_addr != PXAIPCF_ADDR_DEFAULT) {
244 aprint_normal(" addr 0x%lx", sa->pxa_addr);
245 if (sa->pxa_size > PXAIPCF_SIZE_DEFAULT)
246 aprint_normal("-0x%lx", sa->pxa_addr + sa->pxa_size-1);
247 }
248 if (sa->pxa_intr != PXAIPCF_INTR_DEFAULT)
249 aprint_normal(" intr %d", sa->pxa_intr);
250
251 return (UNCONF);
252 }
253
254 static inline uint32_t
255 read_clock_counter(void)
256 {
257 uint32_t x;
258 __asm __volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (x) );
259
260 return x;
261 }
262
263 static int
264 pxaip_measure_cpuclock(struct pxaip_softc *sc)
265 {
266 uint32_t rtc0, rtc1, start, end;
267 uint32_t pmcr_save;
268 bus_space_handle_t ioh;
269 int irq;
270
271 if (bus_space_map(sc->sc_bust, PXA2X0_RTC_BASE, PXA2X0_RTC_SIZE, 0,
272 &ioh))
273 panic("pxaip_measure_cpuclock: can't map RTC");
274
275 irq = disable_interrupts(I32_bit|F32_bit);
276
277 __asm __volatile( "mrc p14, 0, %0, c0, c0, 0" : "=r" (pmcr_save) );
278 /* Enable clock counter */
279 __asm __volatile( "mcr p14, 0, %0, c0, c0, 0" : : "r" (0x0001) );
280
281 rtc0 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR);
282 /* Wait for next second starts */
283 while ((rtc1 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR)) == rtc0)
284 ;
285 start = read_clock_counter();
286 while(rtc1 == bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR))
287 ; /* Wait for 1sec */
288 end = read_clock_counter();
289
290 __asm __volatile( "mcr p14, 0, %0, c0, c0, 0" : : "r" (pmcr_save) );
291 restore_interrupts(irq);
292
293 bus_space_unmap(sc->sc_bust, ioh, PXA2X0_RTC_SIZE);
294
295 return end - start;
296 }
297
298 void
299 pxa2x0_turbo_mode( int f )
300 {
301 __asm __volatile("mcr p14, 0, %0, c6, c0, 0" : : "r" (f));
302 }
303
304 void
305 pxa2x0_probe_sdram(vaddr_t memctl_va, paddr_t *start, paddr_t *size)
306 {
307 u_int32_t mdcnfg, dwid, dcac, drac, dnb;
308 int i;
309
310 mdcnfg = *((volatile u_int32_t *)(memctl_va + MEMCTL_MDCNFG));
311
312 /*
313 * Scan all 4 SDRAM banks
314 */
315 for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
316 start[i] = 0;
317 size[i] = 0;
318
319 switch (i) {
320 case 0:
321 case 1:
322 if ((i == 0 && (mdcnfg & MDCNFG_DE0) == 0) ||
323 (i == 1 && (mdcnfg & MDCNFG_DE1) == 0))
324 continue;
325 dwid = mdcnfg >> MDCNFD_DWID01_SHIFT;
326 dcac = mdcnfg >> MDCNFD_DCAC01_SHIFT;
327 drac = mdcnfg >> MDCNFD_DRAC01_SHIFT;
328 dnb = mdcnfg >> MDCNFD_DNB01_SHIFT;
329 break;
330
331 case 2:
332 case 3:
333 if ((i == 2 && (mdcnfg & MDCNFG_DE2) == 0) ||
334 (i == 3 && (mdcnfg & MDCNFG_DE3) == 0))
335 continue;
336 dwid = mdcnfg >> MDCNFD_DWID23_SHIFT;
337 dcac = mdcnfg >> MDCNFD_DCAC23_SHIFT;
338 drac = mdcnfg >> MDCNFD_DRAC23_SHIFT;
339 dnb = mdcnfg >> MDCNFD_DNB23_SHIFT;
340 break;
341 default:
342 panic("pxa2x0_probe_sdram: impossible");
343 }
344
345 dwid = 2 << (1 - (dwid & MDCNFD_DWID_MASK)); /* 16/32 width */
346 dcac = 1 << ((dcac & MDCNFD_DCAC_MASK) + 8); /* 8-11 columns */
347 drac = 1 << ((drac & MDCNFD_DRAC_MASK) + 11); /* 11-13 rows */
348 dnb = 2 << (dnb & MDCNFD_DNB_MASK); /* # of banks */
349
350 size[i] = (paddr_t)(dwid * dcac * drac * dnb);
351 start[i] = PXA2X0_SDRAM0_START + (i * PXA2X0_SDRAM_BANK_SIZE);
352 }
353 }
354
355 void
356 pxa2x0_clkman_config(u_int clk, boolean_t enable)
357 {
358 struct pxaip_softc *sc;
359 u_int32_t rv;
360
361 KDASSERT(pxaip_sc != NULL);
362 sc = pxaip_sc;
363
364 rv = bus_space_read_4(sc->sc_bust, sc->sc_bush_clk, CLKMAN_CKEN);
365 rv &= ~clk;
366
367 if (enable)
368 rv |= clk;
369
370 bus_space_write_4(sc->sc_bust, sc->sc_bush_clk, CLKMAN_CKEN, rv);
371 }
372