xref: /netbsd-src/sys/arch/arm/xscale/pxa2x0.c (revision f9967d10c997b30f59931b55be2ff010c84270ef)
1 /*	$NetBSD: pxa2x0.c,v 1.6 2005/06/30 17:03:52 drochner 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.6 2005/06/30 17:03:52 drochner 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 *,
132 			     const locdesc_t *, void *);
133 static void	pxaip_attach_critical(struct pxaip_softc *);
134 static int	pxaip_print(void *, const char *);
135 
136 static int	pxaip_measure_cpuclock(struct pxaip_softc *);
137 
138 /* attach structures */
139 CFATTACH_DECL(pxaip, sizeof(struct pxaip_softc),
140     pxaip_match, pxaip_attach, NULL, NULL);
141 
142 static struct pxaip_softc *pxaip_sc;
143 
144 static int
145 pxaip_match(struct device *parent, struct cfdata *match, void *aux)
146 {
147 
148 	return 1;
149 }
150 
151 static void
152 pxaip_attach(struct device *parent, struct device *self, void *aux)
153 {
154 	struct pxaip_softc *sc = (struct pxaip_softc *)self;
155 	int cpuclock;
156 
157 	pxaip_sc = sc;
158 	sc->sc_bust = &pxa2x0_bs_tag;
159 	sc->sc_dmat = &pxa2x0_bus_dma_tag;
160 
161 	aprint_normal(": PXA2x0 Onchip Peripheral Bus\n");
162 
163 	if (bus_space_map(sc->sc_bust, PXA2X0_CLKMAN_BASE, PXA2X0_CLKMAN_SIZE,
164 	    0, &sc->sc_bush_clk))
165 		panic("pxaip_attach: failed to map CLKMAN");
166 
167 	/*
168 	 * Calculate clock speed
169 	 * This takes 2 secs at most.
170 	 */
171 	cpuclock = pxaip_measure_cpuclock(sc) / 1000;
172 	printf("%s: CPU clock = %d.%03d MHz\n", self->dv_xname,
173 	    cpuclock/1000, cpuclock%1000 );
174 
175 	/*
176 	 * Attach critical devices
177 	 */
178 	pxaip_attach_critical(sc);
179 
180 	/*
181 	 * Attach all other devices
182 	 */
183 	config_search_ia(pxaip_search, self, "pxaip", sc);
184 }
185 
186 static int
187 pxaip_search(struct device *parent, struct cfdata *cf,
188 	     const locdesc_t *ldesc, void *aux)
189 {
190 	struct pxaip_softc *sc = aux;
191 	struct pxaip_attach_args aa;
192 
193         aa.pxa_iot = sc->sc_bust;
194         aa.pxa_dmat = sc->sc_dmat;
195         aa.pxa_addr = cf->cf_loc[PXAIPCF_ADDR];
196         aa.pxa_size = cf->cf_loc[PXAIPCF_SIZE];
197 	aa.pxa_index = cf->cf_loc[PXAIPCF_INDEX];
198         aa.pxa_intr = cf->cf_loc[PXAIPCF_INTR];
199 
200         if (config_match(parent, cf, &aa))
201                 config_attach(parent, cf, &aa, pxaip_print);
202 
203         return 0;
204 }
205 
206 static void
207 pxaip_attach_critical(struct pxaip_softc *sc)
208 {
209 	struct pxaip_attach_args aa;
210 
211         aa.pxa_iot = sc->sc_bust;
212         aa.pxa_dmat = sc->sc_dmat;
213         aa.pxa_addr = PXA2X0_INTCTL_BASE;
214         aa.pxa_size = PXA2X0_INTCTL_SIZE;
215         aa.pxa_intr = PXAIPCF_INTR_DEFAULT;
216 	if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
217 		panic("pxaip_attach_critical: failed to attach INTC!");
218 
219 #if NPXAGPIO > 0
220         aa.pxa_iot = sc->sc_bust;
221         aa.pxa_dmat = sc->sc_dmat;
222         aa.pxa_addr = PXA2X0_GPIO_BASE;
223         aa.pxa_size = PXA2X0_GPIO_SIZE;
224         aa.pxa_intr = PXAIPCF_INTR_DEFAULT;
225 	if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
226 		panic("pxaip_attach_critical: failed to attach GPIO!");
227 #endif
228 
229 #if NPXADMAC > 0
230         aa.pxa_iot = sc->sc_bust;
231         aa.pxa_dmat = sc->sc_dmat;
232         aa.pxa_addr = PXA2X0_DMAC_BASE;
233         aa.pxa_size = PXA2X0_DMAC_SIZE;
234         aa.pxa_intr = PXA2X0_INT_DMA;
235 	if (config_found(&sc->sc_dev, &aa, pxaip_print) == NULL)
236 		panic("pxaip_attach_critical: failed to attach DMAC!");
237 #endif
238 }
239 
240 static int
241 pxaip_print(void *aux, const char *name)
242 {
243 	struct pxaip_attach_args *sa = (struct pxaip_attach_args*)aux;
244 
245 	if (sa->pxa_addr != PXAIPCF_ADDR_DEFAULT) {
246                 aprint_normal(" addr 0x%lx", sa->pxa_addr);
247 	        if (sa->pxa_size > PXAIPCF_SIZE_DEFAULT)
248 	                aprint_normal("-0x%lx", sa->pxa_addr + sa->pxa_size-1);
249 	}
250         if (sa->pxa_intr != PXAIPCF_INTR_DEFAULT)
251                 aprint_normal(" intr %d", sa->pxa_intr);
252 
253         return (UNCONF);
254 }
255 
256 static inline uint32_t
257 read_clock_counter(void)
258 {
259   uint32_t x;
260   __asm __volatile("mrc	p14, 0, %0, c1, c0, 0" : "=r" (x) );
261 
262   return x;
263 }
264 
265 static int
266 pxaip_measure_cpuclock(struct pxaip_softc *sc)
267 {
268 	uint32_t rtc0, rtc1, start, end;
269 	uint32_t pmcr_save;
270 	bus_space_handle_t ioh;
271 	int irq;
272 
273 	if (bus_space_map(sc->sc_bust, PXA2X0_RTC_BASE, PXA2X0_RTC_SIZE, 0,
274 	    &ioh))
275 		panic("pxaip_measure_cpuclock: can't map RTC");
276 
277 	irq = disable_interrupts(I32_bit|F32_bit);
278 
279 	__asm __volatile( "mrc p14, 0, %0, c0, c0, 0" : "=r" (pmcr_save) );
280 	/* Enable clock counter */
281 	__asm __volatile( "mcr p14, 0, %0, c0, c0, 0" : : "r" (0x0001) );
282 
283 	rtc0 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR);
284 	/* Wait for next second starts */
285 	while ((rtc1 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR)) == rtc0)
286 		;
287 	start = read_clock_counter();
288 	while(rtc1 == bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR))
289 		;		/* Wait for 1sec */
290 	end = read_clock_counter();
291 
292 	__asm __volatile( "mcr p14, 0, %0, c0, c0, 0" : : "r" (pmcr_save) );
293 	restore_interrupts(irq);
294 
295 	bus_space_unmap(sc->sc_bust, ioh, PXA2X0_RTC_SIZE);
296 
297 	return end - start;
298 }
299 
300 void
301 pxa2x0_turbo_mode( int f )
302 {
303   __asm __volatile("mcr p14, 0, %0, c6, c0, 0" : : "r" (f));
304 }
305 
306 void
307 pxa2x0_probe_sdram(vaddr_t memctl_va, paddr_t *start, paddr_t *size)
308 {
309 	u_int32_t mdcnfg, dwid, dcac, drac, dnb;
310 	int i;
311 
312 	mdcnfg = *((volatile u_int32_t *)(memctl_va + MEMCTL_MDCNFG));
313 
314 	/*
315 	 * Scan all 4 SDRAM banks
316 	 */
317 	for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
318 		start[i] = 0;
319 		size[i] = 0;
320 
321 		switch (i) {
322 		case 0:
323 		case 1:
324 			if ((i == 0 && (mdcnfg & MDCNFG_DE0) == 0) ||
325 			    (i == 1 && (mdcnfg & MDCNFG_DE1) == 0))
326 				continue;
327 			dwid = mdcnfg >> MDCNFD_DWID01_SHIFT;
328 			dcac = mdcnfg >> MDCNFD_DCAC01_SHIFT;
329 			drac = mdcnfg >> MDCNFD_DRAC01_SHIFT;
330 			dnb = mdcnfg >> MDCNFD_DNB01_SHIFT;
331 			break;
332 
333 		case 2:
334 		case 3:
335 			if ((i == 2 && (mdcnfg & MDCNFG_DE2) == 0) ||
336 			    (i == 3 && (mdcnfg & MDCNFG_DE3) == 0))
337 				continue;
338 			dwid = mdcnfg >> MDCNFD_DWID23_SHIFT;
339 			dcac = mdcnfg >> MDCNFD_DCAC23_SHIFT;
340 			drac = mdcnfg >> MDCNFD_DRAC23_SHIFT;
341 			dnb = mdcnfg >> MDCNFD_DNB23_SHIFT;
342 			break;
343 		default:
344 			panic("pxa2x0_probe_sdram: impossible");
345 		}
346 
347 		dwid = 2 << (1 - (dwid & MDCNFD_DWID_MASK));  /* 16/32 width */
348 		dcac = 1 << ((dcac & MDCNFD_DCAC_MASK) + 8);  /* 8-11 columns */
349 		drac = 1 << ((drac & MDCNFD_DRAC_MASK) + 11); /* 11-13 rows */
350 		dnb = 2 << (dnb & MDCNFD_DNB_MASK);	      /* # of banks */
351 
352 		size[i] = (paddr_t)(dwid * dcac * drac * dnb);
353 		start[i] = PXA2X0_SDRAM0_START + (i * PXA2X0_SDRAM_BANK_SIZE);
354 	}
355 }
356 
357 void
358 pxa2x0_clkman_config(u_int clk, boolean_t enable)
359 {
360 	struct pxaip_softc *sc;
361 	u_int32_t rv;
362 
363 	KDASSERT(pxaip_sc != NULL);
364 	sc = pxaip_sc;
365 
366 	rv = bus_space_read_4(sc->sc_bust, sc->sc_bush_clk, CLKMAN_CKEN);
367 	rv &= ~clk;
368 
369 	if (enable)
370 		rv |= clk;
371 
372 	bus_space_write_4(sc->sc_bust, sc->sc_bush_clk, CLKMAN_CKEN, rv);
373 }
374