Annotation of sys/arch/arm/xscale/pxa2x0.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: pxa2x0.c,v 1.11 2005/11/11 23:59:58 deraadt Exp $ */
2: /* $NetBSD: pxa2x0.c,v 1.5 2003/12/12 16:42:44 thorpej Exp $ */
3:
4: /*
5: * Copyright (c) 2002 Genetec Corporation. All rights reserved.
6: * Written by Hiroyuki Bessho for Genetec Corporation.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed for the NetBSD Project by
19: * Genetec Corporation.
20: * 4. The name of Genetec Corporation may not be used to endorse or
21: * promote products derived from this software without specific prior
22: * written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
28: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34: * POSSIBILITY OF SUCH DAMAGE.
35: *
36: *
37: * Autoconfiguration support for the Intel PXA2[15]0 application
38: * processor. This code is derived from arm/sa11x0/sa11x0.c
39: */
40:
41: /*-
42: * Copyright (c) 2001, The NetBSD Foundation, Inc. All rights reserved.
43: *
44: * This code is derived from software contributed to The NetBSD Foundation
45: * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
46: *
47: * Redistribution and use in source and binary forms, with or without
48: * modification, are permitted provided that the following conditions
49: * are met:
50: * 1. Redistributions of source code must retain the above copyright
51: * notice, this list of conditions and the following disclaimer.
52: * 2. Redistributions in binary form must reproduce the above copyright
53: * notice, this list of conditions and the following disclaimer in the
54: * documentation and/or other materials provided with the distribution.
55: * 3. All advertising materials mentioning features or use of this software
56: * must display the following acknowledgement:
57: * This product includes software developed by the NetBSD
58: * Foundation, Inc. and its contributors.
59: * 4. Neither the name of The NetBSD Foundation nor the names of its
60: * contributors may be used to endorse or promote products derived
61: * from this software without specific prior written permission.
62: */
63: /*-
64: * Copyright (c) 1999
65: * Shin Takemura and PocketBSD Project. All rights reserved.
66: *
67: * Redistribution and use in source and binary forms, with or without
68: * modification, are permitted provided that the following conditions
69: * are met:
70: * 1. Redistributions of source code must retain the above copyright
71: * notice, this list of conditions and the following disclaimer.
72: * 2. Redistributions in binary form must reproduce the above copyright
73: * notice, this list of conditions and the following disclaimer in the
74: * documentation and/or other materials provided with the distribution.
75: * 3. All advertising materials mentioning features or use of this software
76: * must display the following acknowledgement:
77: * This product includes software developed by the PocketBSD project
78: * and its contributors.
79: * 4. Neither the name of the project nor the names of its contributors
80: * may be used to endorse or promote products derived from this software
81: * without specific prior written permission.
82: *
83: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
84: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
85: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
86: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
87: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
88: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
89: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
90: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
91: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
92: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93: * SUCH DAMAGE.
94: *
95: */
96:
97: #include <sys/cdefs.h>
98: /*
99: __KERNEL_RCSID(0, "$NetBSD: pxa2x0.c,v 1.5 2003/12/12 16:42:44 thorpej Exp $");
100: */
101:
102: #include "pxaintc.h"
103: #include "pxagpio.h"
104: #include "pxadmac.h"
105:
106: #include <sys/param.h>
107: #include <sys/systm.h>
108: #include <sys/device.h>
109: #include <sys/kernel.h>
110: #include <sys/reboot.h>
111:
112: #include <machine/cpu.h>
113: #include <machine/bus.h>
114:
115: #include <arm/cpufunc.h>
116: #include <arm/mainbus/mainbus.h>
117: #include <arm/xscale/pxa2x0reg.h>
118: #include <arm/xscale/pxa2x0var.h>
119:
120: struct pxaip_softc {
121: struct device sc_dev;
122: bus_space_tag_t sc_bust;
123: bus_dma_tag_t sc_dmat;
124: bus_space_handle_t sc_bush_clk;
125: bus_space_handle_t sc_bush_rtc;
126: };
127:
128: /* prototypes */
129: int pxaip_match(struct device *, void *, void *);
130: void pxaip_attach(struct device *, struct device *, void *);
131: int pxaip_search(struct device *, void *, void *);
132: void pxaip_attach_critical(struct pxaip_softc *);
133: int pxaip_print(void *, const char *);
134:
135: int pxaip_measure_cpuclock(struct pxaip_softc *);
136:
137: /* attach structures */
138: #ifdef __NetBSD__
139: CFATTACH_DECL(pxaip, sizeof(struct pxaip_softc),
140: pxaip_match, pxaip_attach, NULL, NULL);
141: #else
142: struct cfattach pxaip_ca = {
143: sizeof(struct pxaip_softc), pxaip_match, pxaip_attach
144: };
145:
146: struct cfdriver pxaip_cd = {
147: NULL, "pxaip", DV_DULL
148: };
149: #endif
150:
151: struct pxaip_softc *pxaip_sc;
152:
153: int
154: pxaip_match(struct device *parent, void *match, void *aux)
155: {
156:
157: return 1;
158: }
159:
160: void
161: pxaip_attach(struct device *parent, struct device *self, void *aux)
162: {
163: struct pxaip_softc *sc = (struct pxaip_softc *)self;
164: extern int freq;
165: int cpuclock;
166:
167: pxaip_sc = sc;
168: sc->sc_bust = &pxa2x0_bs_tag;
169: sc->sc_dmat = &pxa2x0_bus_dma_tag;
170:
171: if (bus_space_map(sc->sc_bust, PXA2X0_CLKMAN_BASE, PXA2X0_CLKMAN_SIZE,
172: 0, &sc->sc_bush_clk))
173: panic("pxaip_attach: failed to map CLKMAN");
174:
175: if (bus_space_map(sc->sc_bust, PXA2X0_RTC_BASE, PXA2X0_RTC_SIZE,
176: 0, &sc->sc_bush_rtc))
177: panic("pxaip_attach: failed to map RTC");
178:
179: /*
180: * Calculate clock speed
181: * This takes 2 secs at most.
182: */
183: cpuclock = pxaip_measure_cpuclock(sc) / 1000;
184: if (cpuclock % 1000 > 500)
185: cpuclock = cpuclock + 1000 - cpuclock % 1000;
186: freq = cpuclock / 1000;
187:
188: printf(": CPU clock = %d.%03d MHz\n", cpuclock/1000, cpuclock%1000);
189:
190: /*
191: * Attach critical devices
192: */
193: pxaip_attach_critical(sc);
194:
195: /*
196: * Attach all other devices
197: */
198: config_search(pxaip_search, self, sc);
199:
200: }
201:
202: int
203: pxaip_search(struct device *parent, void *c, void *aux)
204: {
205: struct pxaip_softc *sc = aux;
206: struct pxaip_attach_args aa;
207: struct cfdata *cf = c;
208:
209: aa.pxa_iot = sc->sc_bust;
210: aa.pxa_dmat = sc->sc_dmat;
211: #if 0
212: aa.pxa_addr = cf->cf_addr;
213: aa.pxa_size = cf->cf_size;
214: aa.pxa_intr = cf->cf_intr;
215: aa.pxa_index = cf->cf_index;
216: #else
217: aa.pxa_addr = (cf->cf_loc)[0];
218: aa.pxa_size = (cf->cf_loc)[1];
219: aa.pxa_intr = (cf->cf_loc)[2];
220: aa.pxa_index = (cf->cf_loc)[3];
221: #endif
222:
223: config_found(parent, &aa, pxaip_print);
224:
225: return 0;
226: }
227:
228: void
229: pxaip_attach_critical(struct pxaip_softc *sc)
230: {
231: struct pxaip_attach_args aa;
232:
233: aa.pxa_iot = sc->sc_bust;
234: aa.pxa_dmat = sc->sc_dmat;
235: aa.pxa_addr = PXA2X0_INTCTL_BASE;
236: aa.pxa_size = PXA2X0_INTCTL_SIZE;
237: aa.pxa_intr = -1;
238: if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
239: panic("pxaip_attach_critical: failed to attach INTC!");
240:
241: #if NPXAGPIO > 0
242: aa.pxa_iot = sc->sc_bust;
243: aa.pxa_dmat = sc->sc_dmat;
244: aa.pxa_addr = PXA2X0_GPIO_BASE;
245: aa.pxa_size = PXA2X0_GPIO_SIZE;
246: aa.pxa_intr = -1;
247: if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
248: panic("pxaip_attach_critical: failed to attach GPIO!");
249: #endif
250:
251: #if NPXADMAC > 0
252: aa.pxa_iot = sc->sc_bust;
253: aa.pxa_dmat = sc->sc_dmat;
254: aa.pxa_addr = PXA2X0_DMAC_BASE;
255: aa.pxa_size = PXA2X0_DMAC_SIZE;
256: aa.pxa_intr = PXA2X0_INT_DMA;
257: if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
258: panic("pxaip_attach_critical: failed to attach DMAC!");
259: #endif
260: }
261:
262: int
263: pxaip_print(void *aux, const char *name)
264: {
265: struct pxaip_attach_args *sa = (struct pxaip_attach_args*)aux;
266:
267: if (sa->pxa_addr != -1) {
268: printf(" addr 0x%lx", sa->pxa_addr);
269: if (sa->pxa_size > -1)
270: printf("-0x%lx", sa->pxa_addr + sa->pxa_size-1);
271: }
272: if (sa->pxa_intr != -1)
273: printf(" intr %d", sa->pxa_intr);
274:
275: return (UNCONF);
276: }
277:
278: static inline uint32_t
279: read_clock_counter(void)
280: {
281: uint32_t x;
282: __asm __volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (x));
283:
284: return x;
285: }
286:
287: int
288: pxaip_measure_cpuclock(struct pxaip_softc *sc)
289: {
290: uint32_t rtc0, rtc1, start, end;
291: uint32_t pmcr_save;
292: bus_space_handle_t ioh;
293: int irq;
294:
295: ioh = sc->sc_bush_rtc;
296: irq = disable_interrupts(I32_bit|F32_bit);
297:
298: __asm __volatile( "mrc p14, 0, %0, c0, c1, 0" : "=r" (pmcr_save));
299: /* Enable clock counter */
300: __asm __volatile( "mcr p14, 0, %0, c0, c1, 0" : : "r" (0x0001));
301:
302: rtc0 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR);
303: /* Wait for next second starts */
304: while ((rtc1 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR)) == rtc0)
305: ;
306: start = read_clock_counter();
307: while(rtc1 == bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR))
308: ; /* Wait for 1sec */
309: end = read_clock_counter();
310:
311: __asm __volatile( "mcr p14, 0, %0, c0, c1, 0" : : "r" (pmcr_save));
312: restore_interrupts(irq);
313:
314: return end - start;
315: }
316:
317: void
318: pxa2x0_turbo_mode(int f)
319: {
320: __asm __volatile("mcr p14, 0, %0, c6, c0, 0" : : "r" (f));
321: }
322:
323: void
324: pxa2x0_probe_sdram(vaddr_t memctl_va, paddr_t *start, paddr_t *size)
325: {
326: u_int32_t mdcnfg, dwid, dcac, drac, dnb;
327: int i;
328:
329: mdcnfg = *((volatile u_int32_t *)(memctl_va + MEMCTL_MDCNFG));
330:
331: /*
332: * Scan all 4 SDRAM banks
333: */
334: for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
335: start[i] = 0;
336: size[i] = 0;
337:
338: switch (i) {
339: case 0:
340: case 1:
341: if ((i == 0 && (mdcnfg & MDCNFG_DE0) == 0) ||
342: (i == 1 && (mdcnfg & MDCNFG_DE1) == 0))
343: continue;
344: dwid = mdcnfg >> MDCNFD_DWID01_SHIFT;
345: dcac = mdcnfg >> MDCNFD_DCAC01_SHIFT;
346: drac = mdcnfg >> MDCNFD_DRAC01_SHIFT;
347: dnb = mdcnfg >> MDCNFD_DNB01_SHIFT;
348: break;
349:
350: case 2:
351: case 3:
352: if ((i == 2 && (mdcnfg & MDCNFG_DE2) == 0) ||
353: (i == 3 && (mdcnfg & MDCNFG_DE3) == 0))
354: continue;
355: dwid = mdcnfg >> MDCNFD_DWID23_SHIFT;
356: dcac = mdcnfg >> MDCNFD_DCAC23_SHIFT;
357: drac = mdcnfg >> MDCNFD_DRAC23_SHIFT;
358: dnb = mdcnfg >> MDCNFD_DNB23_SHIFT;
359: break;
360: default:
361: panic("pxa2x0_probe_sdram: impossible");
362: }
363:
364: dwid = 2 << (1 - (dwid & MDCNFD_DWID_MASK)); /* 16/32 width */
365: dcac = 1 << ((dcac & MDCNFD_DCAC_MASK) + 8); /* 8-11 columns */
366: drac = 1 << ((drac & MDCNFD_DRAC_MASK) + 11); /* 11-13 rows */
367: dnb = 2 << (dnb & MDCNFD_DNB_MASK); /* # of banks */
368:
369: size[i] = (paddr_t)(dwid * dcac * drac * dnb);
370: start[i] = PXA2X0_SDRAM0_START + (i * PXA2X0_SDRAM_BANK_SIZE);
371: }
372: }
373:
374: void
375: pxa2x0_clkman_config(u_int clk, int enable)
376: {
377: struct pxaip_softc *sc;
378: u_int32_t rv;
379:
380: KDASSERT(pxaip_sc != NULL);
381: sc = pxaip_sc;
382:
383: rv = bus_space_read_4(sc->sc_bust, sc->sc_bush_clk, CLKMAN_CKEN);
384: rv &= ~clk;
385:
386: if (enable)
387: rv |= clk;
388:
389: bus_space_write_4(sc->sc_bust, sc->sc_bush_clk, CLKMAN_CKEN, rv);
390: }
391:
392: void
393: pxa2x0_rtc_setalarm(u_int32_t secs)
394: {
395: struct pxaip_softc *sc;
396: u_int32_t rv;
397: int s;
398:
399: KDASSERT(pxaip_sc != NULL);
400: sc = pxaip_sc;
401:
402: s = splhigh();
403: bus_space_write_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTAR, secs);
404: rv = bus_space_read_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTSR);
405: if (secs == 0)
406: bus_space_write_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTSR,
407: (rv | RTSR_AL) & ~RTSR_ALE);
408: else
409: bus_space_write_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTSR,
410: (rv | RTSR_AL | RTSR_ALE));
411: splx(s);
412: }
413:
414: u_int32_t
415: pxa2x0_rtc_getalarm(void)
416: {
417: struct pxaip_softc *sc;
418:
419: KDASSERT(pxaip_sc != NULL);
420: sc = pxaip_sc;
421:
422: return (bus_space_read_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RTAR));
423: }
424:
425: u_int32_t
426: pxa2x0_rtc_getsecs(void)
427: {
428: struct pxaip_softc *sc;
429:
430: KDASSERT(pxaip_sc != NULL);
431: sc = pxaip_sc;
432:
433: return (bus_space_read_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RCNR));
434: }
435:
436: void
437: resettodr(void)
438: {
439: struct pxaip_softc *sc = pxaip_sc;
440:
441: bus_space_write_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RCNR,
442: (u_int32_t)time.tv_sec);
443: }
444:
445: void
446: inittodr(time_t base)
447: {
448: struct pxaip_softc *sc = pxaip_sc;
449: u_int32_t rcnr;
450:
451: /* XXX decide if RCNR can be valid, based on the last reset
452: * XXX reason, i.e. RCSR. */
453: rcnr = bus_space_read_4(sc->sc_bust, sc->sc_bush_rtc, RTC_RCNR);
454:
455: /* XXX check how much RCNR differs from the filesystem date. */
456: if (rcnr > base)
457: time.tv_sec = rcnr;
458: else {
459: printf("WARNING: using filesystem date -- CHECK AND RESET THE DATE!\n");
460: time.tv_sec = base;
461: }
462:
463: time.tv_usec = 0;
464: }
CVSweb