xref: /netbsd-src/sys/arch/arm/xscale/pxa2x0.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: pxa2x0.c,v 1.24 2021/08/07 16:18:46 thorpej Exp $ */
25e33e792Sbsh 
35e33e792Sbsh /*
4d8876765Sbsh  * Copyright (c) 2002, 2005  Genetec Corporation.  All rights reserved.
55e33e792Sbsh  * Written by Hiroyuki Bessho for Genetec Corporation.
65e33e792Sbsh  *
75e33e792Sbsh  * Redistribution and use in source and binary forms, with or without
85e33e792Sbsh  * modification, are permitted provided that the following conditions
95e33e792Sbsh  * are met:
105e33e792Sbsh  * 1. Redistributions of source code must retain the above copyright
115e33e792Sbsh  *    notice, this list of conditions and the following disclaimer.
125e33e792Sbsh  * 2. Redistributions in binary form must reproduce the above copyright
135e33e792Sbsh  *    notice, this list of conditions and the following disclaimer in the
145e33e792Sbsh  *    documentation and/or other materials provided with the distribution.
155e33e792Sbsh  * 3. All advertising materials mentioning features or use of this software
165e33e792Sbsh  *    must display the following acknowledgement:
175e33e792Sbsh  *	This product includes software developed for the NetBSD Project by
185e33e792Sbsh  *	Genetec Corporation.
195e33e792Sbsh  * 4. The name of Genetec Corporation may not be used to endorse or
205e33e792Sbsh  *    promote products derived from this software without specific prior
215e33e792Sbsh  *    written permission.
225e33e792Sbsh  *
235e33e792Sbsh  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
245e33e792Sbsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
255e33e792Sbsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
265e33e792Sbsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
275e33e792Sbsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
285e33e792Sbsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
295e33e792Sbsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
305e33e792Sbsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
315e33e792Sbsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
325e33e792Sbsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
335e33e792Sbsh  * POSSIBILITY OF SUCH DAMAGE.
345e33e792Sbsh  *
355e33e792Sbsh  *
365e33e792Sbsh  * Autoconfiguration support for the Intel PXA2[15]0 application
375e33e792Sbsh  * processor. This code is derived from arm/sa11x0/sa11x0.c
385e33e792Sbsh  */
395e33e792Sbsh 
405e33e792Sbsh /*-
415e33e792Sbsh  * Copyright (c) 2001, The NetBSD Foundation, Inc.  All rights reserved.
425e33e792Sbsh  *
435e33e792Sbsh  * This code is derived from software contributed to The NetBSD Foundation
445e33e792Sbsh  * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
455e33e792Sbsh  *
465e33e792Sbsh  * Redistribution and use in source and binary forms, with or without
475e33e792Sbsh  * modification, are permitted provided that the following conditions
485e33e792Sbsh  * are met:
495e33e792Sbsh  * 1. Redistributions of source code must retain the above copyright
505e33e792Sbsh  *    notice, this list of conditions and the following disclaimer.
515e33e792Sbsh  * 2. Redistributions in binary form must reproduce the above copyright
525e33e792Sbsh  *    notice, this list of conditions and the following disclaimer in the
535e33e792Sbsh  *    documentation and/or other materials provided with the distribution.
542d265edbSmartin  *
552d265edbSmartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
562d265edbSmartin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
572d265edbSmartin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
582d265edbSmartin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
592d265edbSmartin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
602d265edbSmartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
612d265edbSmartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
622d265edbSmartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
632d265edbSmartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
642d265edbSmartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
652d265edbSmartin  * POSSIBILITY OF SUCH DAMAGE.
665e33e792Sbsh  */
675e33e792Sbsh /*-
685e33e792Sbsh  * Copyright (c) 1999
695e33e792Sbsh  *         Shin Takemura and PocketBSD Project. All rights reserved.
705e33e792Sbsh  *
715e33e792Sbsh  * Redistribution and use in source and binary forms, with or without
725e33e792Sbsh  * modification, are permitted provided that the following conditions
735e33e792Sbsh  * are met:
745e33e792Sbsh  * 1. Redistributions of source code must retain the above copyright
755e33e792Sbsh  *    notice, this list of conditions and the following disclaimer.
765e33e792Sbsh  * 2. Redistributions in binary form must reproduce the above copyright
775e33e792Sbsh  *    notice, this list of conditions and the following disclaimer in the
785e33e792Sbsh  *    documentation and/or other materials provided with the distribution.
795e33e792Sbsh  * 3. All advertising materials mentioning features or use of this software
805e33e792Sbsh  *    must display the following acknowledgement:
815e33e792Sbsh  *	This product includes software developed by the PocketBSD project
825e33e792Sbsh  *	and its contributors.
835e33e792Sbsh  * 4. Neither the name of the project nor the names of its contributors
845e33e792Sbsh  *    may be used to endorse or promote products derived from this software
855e33e792Sbsh  *    without specific prior written permission.
865e33e792Sbsh  *
875e33e792Sbsh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
885e33e792Sbsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
895e33e792Sbsh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
905e33e792Sbsh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
915e33e792Sbsh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
925e33e792Sbsh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
935e33e792Sbsh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
945e33e792Sbsh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
955e33e792Sbsh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
965e33e792Sbsh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
975e33e792Sbsh  * SUCH DAMAGE.
985e33e792Sbsh  *
995e33e792Sbsh  */
1005e33e792Sbsh 
10108716eaeSlukem #include <sys/cdefs.h>
102*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: pxa2x0.c,v 1.24 2021/08/07 16:18:46 thorpej Exp $");
10308716eaeSlukem 
104233a67b2Sscw #include "pxaintc.h"
105233a67b2Sscw #include "pxagpio.h"
106233a67b2Sscw #if 0
107233a67b2Sscw #include "pxadmac.h"	/* Not yet */
108233a67b2Sscw #endif
109233a67b2Sscw 
110233a67b2Sscw #include "locators.h"
111233a67b2Sscw 
1125e33e792Sbsh #include <sys/param.h>
1135e33e792Sbsh #include <sys/systm.h>
1145e33e792Sbsh #include <sys/device.h>
1155e33e792Sbsh #include <sys/kernel.h>
1165e33e792Sbsh #include <sys/reboot.h>
1175e33e792Sbsh 
1185e33e792Sbsh #include <machine/cpu.h>
119ed9977b1Sdyoung #include <sys/bus.h>
1205e33e792Sbsh 
1215e33e792Sbsh #include <arm/cpufunc.h>
1225e33e792Sbsh #include <arm/mainbus/mainbus.h>
123c61364bfSbsh #include <arm/xscale/pxa2x0cpu.h>
1245e33e792Sbsh #include <arm/xscale/pxa2x0reg.h>
1255e33e792Sbsh #include <arm/xscale/pxa2x0var.h>
126d8876765Sbsh #include <arm/xscale/xscalereg.h>
1275e33e792Sbsh 
128233a67b2Sscw struct pxaip_softc {
129c09d4990Snonaka 	device_t sc_dev;
130233a67b2Sscw 	bus_space_tag_t sc_bust;
131233a67b2Sscw 	bus_dma_tag_t sc_dmat;
132233a67b2Sscw 	bus_space_handle_t sc_bush_clk;
133ac86e555Speter 	bus_space_handle_t sc_bush_mem;
134233a67b2Sscw };
1355e33e792Sbsh 
1365e33e792Sbsh /* prototypes */
137c09d4990Snonaka static int	pxaip_match(device_t, cfdata_t, void *);
138c09d4990Snonaka static void	pxaip_attach(device_t, device_t, void *);
139c09d4990Snonaka static int 	pxaip_search(device_t, cfdata_t, const int *, void *);
140233a67b2Sscw static void	pxaip_attach_critical(struct pxaip_softc *);
141233a67b2Sscw static int	pxaip_print(void *, const char *);
142233a67b2Sscw 
143233a67b2Sscw static int	pxaip_measure_cpuclock(struct pxaip_softc *);
1445e33e792Sbsh 
145c61364bfSbsh #if defined(CPU_XSCALE_PXA250) && defined(CPU_XSCALE_PXA270)
146c61364bfSbsh # define SUPPORTED_CPU	"PXA250 and PXA270"
147c61364bfSbsh #elif defined(CPU_XSCALE_PXA250)
148c61364bfSbsh # define SUPPORTED_CPU	"PXA250"
149c61364bfSbsh #elif defined(CPU_XSCALE_PXA270)
150c61364bfSbsh # define SUPPORTED_CPU	"PXA270"
151c61364bfSbsh #else
152c61364bfSbsh # define SUPPORTED_CPU	"none of PXA2xx"
153c61364bfSbsh #endif
154c61364bfSbsh 
1555e33e792Sbsh /* attach structures */
156c09d4990Snonaka CFATTACH_DECL_NEW(pxaip, sizeof(struct pxaip_softc),
157233a67b2Sscw     pxaip_match, pxaip_attach, NULL, NULL);
1585e33e792Sbsh 
159233a67b2Sscw static struct pxaip_softc *pxaip_sc;
160ac86e555Speter static vaddr_t pxamemctl_regs;
161ac86e555Speter #define MEMCTL_BOOTSTRAP_REG(reg) \
162ac86e555Speter 	(*((volatile uint32_t *)(pxamemctl_regs + (reg))))
163dbc112baSober static vaddr_t pxaclkman_regs;
164dbc112baSober #define CLKMAN_BOOTSTRAP_REG(reg) \
165dbc112baSober 	(*((volatile uint32_t *)(pxaclkman_regs + (reg))))
1665e33e792Sbsh 
1675e33e792Sbsh static int
pxaip_match(device_t parent,cfdata_t match,void * aux)168c09d4990Snonaka pxaip_match(device_t parent, cfdata_t match, void *aux)
1695e33e792Sbsh {
1705e33e792Sbsh 
171c61364bfSbsh #if	!defined(CPU_XSCALE_PXA270)
172c61364bfSbsh 	if (__CPU_IS_PXA270)
173c61364bfSbsh 		goto bad_config;
174c61364bfSbsh #endif
175c61364bfSbsh 
176c61364bfSbsh #if	!defined(CPU_XSCALE_PXA250)
177c61364bfSbsh 	if (__CPU_IS_PXA250)
178c61364bfSbsh 		goto bad_config;
179c61364bfSbsh #endif
180c61364bfSbsh 
1815e33e792Sbsh 	return 1;
182c61364bfSbsh 
183c61364bfSbsh #if	defined(CPU_XSCALE_PXA250) + defined(CPU_XSCALE_PXA270) != 2
184c61364bfSbsh  bad_config:
185c61364bfSbsh 	aprint_error("Kernel is configured for %s, but CPU is %s\n",
186c61364bfSbsh 		     SUPPORTED_CPU, __CPU_IS_PXA270 ? "PXA270" : "PXA250");
187c61364bfSbsh 	return 0;
188c61364bfSbsh #endif
1895e33e792Sbsh }
1905e33e792Sbsh 
191233a67b2Sscw static void
pxaip_attach(device_t parent,device_t self,void * aux)192c09d4990Snonaka pxaip_attach(device_t parent, device_t self, void *aux)
1935e33e792Sbsh {
194c09d4990Snonaka 	struct pxaip_softc *sc = device_private(self);
1955e33e792Sbsh 	int cpuclock;
1965e33e792Sbsh 
197233a67b2Sscw 	pxaip_sc = sc;
198c09d4990Snonaka 	sc->sc_dev = self;
199233a67b2Sscw 	sc->sc_bust = &pxa2x0_bs_tag;
200233a67b2Sscw 	sc->sc_dmat = &pxa2x0_bus_dma_tag;
2015e33e792Sbsh 
202c09d4990Snonaka 	aprint_normal(": Onchip Peripheral Bus\n");
203233a67b2Sscw 
204233a67b2Sscw 	if (bus_space_map(sc->sc_bust, PXA2X0_CLKMAN_BASE, PXA2X0_CLKMAN_SIZE,
205233a67b2Sscw 	    0, &sc->sc_bush_clk))
206233a67b2Sscw 		panic("pxaip_attach: failed to map CLKMAN");
2075e33e792Sbsh 
208ac86e555Speter 	if (bus_space_map(sc->sc_bust, PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE,
209ac86e555Speter 	    0, &sc->sc_bush_mem))
210ac86e555Speter 		panic("pxaip_attach: failed to map MEMCTL");
211ac86e555Speter 
2125e33e792Sbsh 	/*
213233a67b2Sscw 	 * Calculate clock speed
214233a67b2Sscw 	 * This takes 2 secs at most.
2155e33e792Sbsh 	 */
216233a67b2Sscw 	cpuclock = pxaip_measure_cpuclock(sc) / 1000;
217cbab9cadSchs 	printf("%s: CPU clock = %d.%03d MHz\n", device_xname(self),
218233a67b2Sscw 	    cpuclock/1000, cpuclock%1000 );
2195e33e792Sbsh 
220c61364bfSbsh 	aprint_normal("%s: kernel is configured for " SUPPORTED_CPU
221c61364bfSbsh 		      ", cpu type is %s\n",
222cbab9cadSchs 		      device_xname(self),
223c61364bfSbsh 		      __CPU_IS_PXA270 ? "PXA270" : "PXA250");
224c61364bfSbsh 
225233a67b2Sscw 	/*
226233a67b2Sscw 	 * Attach critical devices
227233a67b2Sscw 	 */
228233a67b2Sscw 	pxaip_attach_critical(sc);
2295e33e792Sbsh 
230233a67b2Sscw 	/*
231233a67b2Sscw 	 * Attach all other devices
232233a67b2Sscw 	 */
2332685996bSthorpej 	config_search(self, NULL,
234*c7fb772bSthorpej 	    CFARGS(.search = pxaip_search));
2355e33e792Sbsh }
2365e33e792Sbsh 
237233a67b2Sscw static int
pxaip_search(device_t parent,cfdata_t cf,const int * ldesc,void * aux)238c09d4990Snonaka pxaip_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
2395e33e792Sbsh {
2402685996bSthorpej 	struct pxaip_softc *sc = device_private(parent);
241233a67b2Sscw 	struct pxaip_attach_args aa;
2425e33e792Sbsh 
243233a67b2Sscw 	aa.pxa_iot = sc->sc_bust;
244233a67b2Sscw 	aa.pxa_dmat = sc->sc_dmat;
245eb34e284Skiyohara 	aa.pxa_name = cf->cf_name;
2465e33e792Sbsh 	aa.pxa_addr = cf->cf_loc[PXAIPCF_ADDR];
2475e33e792Sbsh 	aa.pxa_size = cf->cf_loc[PXAIPCF_SIZE];
2485e33e792Sbsh 	aa.pxa_index = cf->cf_loc[PXAIPCF_INDEX];
2495e33e792Sbsh 	aa.pxa_intr = cf->cf_loc[PXAIPCF_INTR];
2505e33e792Sbsh 
2512685996bSthorpej 	if (config_probe(parent, cf, &aa))
252*c7fb772bSthorpej 		config_attach(parent, cf, &aa, pxaip_print, CFARGS_NONE);
2535e33e792Sbsh 
2545e33e792Sbsh 	return 0;
2555e33e792Sbsh }
2565e33e792Sbsh 
257233a67b2Sscw static void
pxaip_attach_critical(struct pxaip_softc * sc)258233a67b2Sscw pxaip_attach_critical(struct pxaip_softc *sc)
259233a67b2Sscw {
260233a67b2Sscw 	struct pxaip_attach_args aa;
261233a67b2Sscw 
262233a67b2Sscw 	aa.pxa_iot = sc->sc_bust;
263233a67b2Sscw 	aa.pxa_dmat = sc->sc_dmat;
264eb34e284Skiyohara 	aa.pxa_name = "pxaintc";
265233a67b2Sscw 	aa.pxa_addr = PXA2X0_INTCTL_BASE;
266233a67b2Sscw 	aa.pxa_size = PXA2X0_INTCTL_SIZE;
267233a67b2Sscw 	aa.pxa_intr = PXAIPCF_INTR_DEFAULT;
268*c7fb772bSthorpej 	if (config_found(sc->sc_dev, &aa, pxaip_print, CFARGS_NONE) == NULL)
269233a67b2Sscw 		panic("pxaip_attach_critical: failed to attach INTC!");
270233a67b2Sscw 
271233a67b2Sscw #if NPXAGPIO > 0
272233a67b2Sscw 	aa.pxa_iot = sc->sc_bust;
273233a67b2Sscw 	aa.pxa_dmat = sc->sc_dmat;
274eb34e284Skiyohara 	aa.pxa_name = "pxagpio";
275233a67b2Sscw 	aa.pxa_addr = PXA2X0_GPIO_BASE;
276233a67b2Sscw 	aa.pxa_size = PXA2X0_GPIO_SIZE;
277233a67b2Sscw 	aa.pxa_intr = PXAIPCF_INTR_DEFAULT;
278*c7fb772bSthorpej 	if (config_found(sc->sc_dev, &aa, pxaip_print, CFARGS_NONE) == NULL)
279233a67b2Sscw 		panic("pxaip_attach_critical: failed to attach GPIO!");
280233a67b2Sscw #endif
281233a67b2Sscw 
282233a67b2Sscw #if NPXADMAC > 0
283233a67b2Sscw 	aa.pxa_iot = sc->sc_bust;
284233a67b2Sscw 	aa.pxa_dmat = sc->sc_dmat;
285eb34e284Skiyohara 	aa.pxa_name = "pxaidmac";
286233a67b2Sscw 	aa.pxa_addr = PXA2X0_DMAC_BASE;
287233a67b2Sscw 	aa.pxa_size = PXA2X0_DMAC_SIZE;
288233a67b2Sscw 	aa.pxa_intr = PXA2X0_INT_DMA;
289*c7fb772bSthorpej 	if (config_found(sc->sc_dev, &aa, pxaip_print, CFARGS_NONE) == NULL)
290233a67b2Sscw 		panic("pxaip_attach_critical: failed to attach DMAC!");
291233a67b2Sscw #endif
292233a67b2Sscw }
293233a67b2Sscw 
294233a67b2Sscw static int
pxaip_print(void * aux,const char * name)295233a67b2Sscw pxaip_print(void *aux, const char *name)
296233a67b2Sscw {
297233a67b2Sscw 	struct pxaip_attach_args *sa = (struct pxaip_attach_args *)aux;
298233a67b2Sscw 
299233a67b2Sscw 	if (sa->pxa_addr != PXAIPCF_ADDR_DEFAULT) {
300233a67b2Sscw 		aprint_normal(" addr 0x%lx", sa->pxa_addr);
301233a67b2Sscw 		if (sa->pxa_size > PXAIPCF_SIZE_DEFAULT)
302233a67b2Sscw 			aprint_normal("-0x%lx", sa->pxa_addr + sa->pxa_size-1);
303233a67b2Sscw 	}
304233a67b2Sscw 	if (sa->pxa_intr != PXAIPCF_INTR_DEFAULT)
305233a67b2Sscw 		aprint_normal(" intr %d", sa->pxa_intr);
306233a67b2Sscw 
307233a67b2Sscw 	return (UNCONF);
308233a67b2Sscw }
309233a67b2Sscw 
3105e33e792Sbsh static inline uint32_t
read_clock_counter_xsc1(void)311c61364bfSbsh read_clock_counter_xsc1(void)
3125e33e792Sbsh {
3135e33e792Sbsh 	uint32_t x;
3145f1c88d7Sperry 	__asm volatile("mrc	p14, 0, %0, c1, c0, 0" : "=r" (x) );
3155e33e792Sbsh 
3165e33e792Sbsh 	return x;
3175e33e792Sbsh }
3185e33e792Sbsh 
319c61364bfSbsh static inline uint32_t
read_clock_counter_xsc2(void)320c61364bfSbsh read_clock_counter_xsc2(void)
321c61364bfSbsh {
322c61364bfSbsh 	uint32_t x;
3235f1c88d7Sperry 	__asm volatile("mrc	p14, 0, %0, c1, c1, 0" : "=r" (x) );
324c61364bfSbsh 
325c61364bfSbsh 	return x;
326c61364bfSbsh }
327c61364bfSbsh 
328233a67b2Sscw static int
pxaip_measure_cpuclock(struct pxaip_softc * sc)329233a67b2Sscw pxaip_measure_cpuclock(struct pxaip_softc *sc)
3305e33e792Sbsh {
3315e33e792Sbsh 	uint32_t rtc0, rtc1, start, end;
3325e33e792Sbsh 	uint32_t pmcr_save;
333233a67b2Sscw 	bus_space_handle_t ioh;
334233a67b2Sscw 	int irq;
335c61364bfSbsh 	int is_xsc2 = CPU_IS_PXA270;
336c61364bfSbsh #define	read_clock_counter()	(is_xsc2 ? read_clock_counter_xsc2() : \
337c61364bfSbsh 					read_clock_counter_xsc1())
338233a67b2Sscw 
339233a67b2Sscw 	if (bus_space_map(sc->sc_bust, PXA2X0_RTC_BASE, PXA2X0_RTC_SIZE, 0,
340233a67b2Sscw 	    &ioh))
341233a67b2Sscw 		panic("pxaip_measure_cpuclock: can't map RTC");
342233a67b2Sscw 
343233a67b2Sscw 	irq = disable_interrupts(I32_bit|F32_bit);
3445e33e792Sbsh 
345c61364bfSbsh 	if (is_xsc2) {
3465f1c88d7Sperry 		__asm volatile(
347c61364bfSbsh 			"mrc p14, 0, %0, c0, c1, 0" : "=r" (pmcr_save));
3485e33e792Sbsh 		/* Enable clock counter */
3495f1c88d7Sperry 		__asm volatile(
350c61364bfSbsh 			"mcr p14, 0, %0, c0, c1, 0" : : "r" (PMNC_E|PMNC_C));
351c61364bfSbsh 	}
352c61364bfSbsh 	else {
3535f1c88d7Sperry 		__asm volatile(
354c61364bfSbsh 			"mrc p14, 0, %0, c0, c0, 0" : "=r" (pmcr_save));
355c61364bfSbsh 		/* Enable clock counter */
3565f1c88d7Sperry 		__asm volatile(
357c61364bfSbsh 			"mcr p14, 0, %0, c0, c0, 0" : : "r" (PMNC_E|PMNC_C));
358c61364bfSbsh 	}
3595e33e792Sbsh 
360233a67b2Sscw 	rtc0 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR);
3615e33e792Sbsh 	/* Wait for next second starts */
362233a67b2Sscw 	while ((rtc1 = bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR)) == rtc0)
3635e33e792Sbsh 		;
3645e33e792Sbsh 	start = read_clock_counter();
365233a67b2Sscw 	while(rtc1 == bus_space_read_4(sc->sc_bust, ioh, RTC_RCNR))
3665e33e792Sbsh 		;		/* Wait for 1sec */
3675e33e792Sbsh 	end = read_clock_counter();
3685e33e792Sbsh 
369c61364bfSbsh 	if (is_xsc2)
3705f1c88d7Sperry 		__asm volatile(
371c61364bfSbsh 			"mcr p14, 0, %0, c0, c1, 0" : : "r" (pmcr_save));
372c61364bfSbsh 	else
3735f1c88d7Sperry 		__asm volatile(
374c61364bfSbsh 			"mcr p14, 0, %0, c0, c0, 0" : : "r" (pmcr_save));
3755e33e792Sbsh 	restore_interrupts(irq);
3765e33e792Sbsh 
377233a67b2Sscw 	bus_space_unmap(sc->sc_bust, ioh, PXA2X0_RTC_SIZE);
378233a67b2Sscw 
3795e33e792Sbsh 	return end - start;
3805e33e792Sbsh }
3815e33e792Sbsh 
3825e33e792Sbsh void
pxa2x0_turbo_mode(int f)3835e33e792Sbsh pxa2x0_turbo_mode(int f)
3845e33e792Sbsh {
3855f1c88d7Sperry 	__asm volatile("mcr p14, 0, %0, c6, c0, 0" : : "r" (f));
3865e33e792Sbsh }
387233a67b2Sscw 
388233a67b2Sscw void
pxa2x0_probe_sdram(vaddr_t memctl_va,paddr_t * start,paddr_t * size)389233a67b2Sscw pxa2x0_probe_sdram(vaddr_t memctl_va, paddr_t *start, paddr_t *size)
390233a67b2Sscw {
39108a4aba7Sskrll 	uint32_t mdcnfg, dwid, dcac, drac, dnb;
392233a67b2Sscw 	int i;
393233a67b2Sscw 
39408a4aba7Sskrll 	mdcnfg = *((volatile uint32_t *)(memctl_va + MEMCTL_MDCNFG));
395233a67b2Sscw 
396233a67b2Sscw 	/*
397233a67b2Sscw 	 * Scan all 4 SDRAM banks
398233a67b2Sscw 	 */
399233a67b2Sscw 	for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
400233a67b2Sscw 		start[i] = 0;
401233a67b2Sscw 		size[i] = 0;
402233a67b2Sscw 
403233a67b2Sscw 		switch (i) {
404233a67b2Sscw 		case 0:
405233a67b2Sscw 		case 1:
406233a67b2Sscw 			if ((i == 0 && (mdcnfg & MDCNFG_DE0) == 0) ||
407233a67b2Sscw 			    (i == 1 && (mdcnfg & MDCNFG_DE1) == 0))
408233a67b2Sscw 				continue;
409233a67b2Sscw 			dwid = mdcnfg >> MDCNFD_DWID01_SHIFT;
410233a67b2Sscw 			dcac = mdcnfg >> MDCNFD_DCAC01_SHIFT;
411233a67b2Sscw 			drac = mdcnfg >> MDCNFD_DRAC01_SHIFT;
412233a67b2Sscw 			dnb = mdcnfg >> MDCNFD_DNB01_SHIFT;
413233a67b2Sscw 			break;
414233a67b2Sscw 
415233a67b2Sscw 		case 2:
416233a67b2Sscw 		case 3:
417233a67b2Sscw 			if ((i == 2 && (mdcnfg & MDCNFG_DE2) == 0) ||
418233a67b2Sscw 			    (i == 3 && (mdcnfg & MDCNFG_DE3) == 0))
419233a67b2Sscw 				continue;
420233a67b2Sscw 			dwid = mdcnfg >> MDCNFD_DWID23_SHIFT;
421233a67b2Sscw 			dcac = mdcnfg >> MDCNFD_DCAC23_SHIFT;
422233a67b2Sscw 			drac = mdcnfg >> MDCNFD_DRAC23_SHIFT;
423233a67b2Sscw 			dnb = mdcnfg >> MDCNFD_DNB23_SHIFT;
424233a67b2Sscw 			break;
425648d5e6eSthorpej 		default:
426648d5e6eSthorpej 			panic("pxa2x0_probe_sdram: impossible");
427233a67b2Sscw 		}
428233a67b2Sscw 
429233a67b2Sscw 		dwid = 2 << (1 - (dwid & MDCNFD_DWID_MASK));  /* 16/32 width */
430233a67b2Sscw 		dcac = 1 << ((dcac & MDCNFD_DCAC_MASK) + 8);  /* 8-11 columns */
431233a67b2Sscw 		drac = 1 << ((drac & MDCNFD_DRAC_MASK) + 11); /* 11-13 rows */
432233a67b2Sscw 		dnb = 2 << (dnb & MDCNFD_DNB_MASK);	      /* # of banks */
433233a67b2Sscw 
434233a67b2Sscw 		size[i] = (paddr_t)(dwid * dcac * drac * dnb);
435233a67b2Sscw 		start[i] = PXA2X0_SDRAM0_START + (i * PXA2X0_SDRAM_BANK_SIZE);
436233a67b2Sscw 	}
437233a67b2Sscw }
438233a67b2Sscw 
439233a67b2Sscw void
pxa2x0_memctl_bootstrap(vaddr_t va)440ac86e555Speter pxa2x0_memctl_bootstrap(vaddr_t va)
441ac86e555Speter {
442ac86e555Speter 
443ac86e555Speter 	pxamemctl_regs = va;
444ac86e555Speter }
445ac86e555Speter 
446ac86e555Speter uint32_t
pxa2x0_memctl_read(int reg)447ac86e555Speter pxa2x0_memctl_read(int reg)
448ac86e555Speter {
449ac86e555Speter 	struct pxaip_softc *sc;
450ac86e555Speter 	bus_space_tag_t iot;
451ac86e555Speter 	bus_space_handle_t ioh;
452ac86e555Speter 
453ac86e555Speter 	if (__predict_true(pxaip_sc != NULL)) {
454ac86e555Speter 		sc = pxaip_sc;
455ac86e555Speter 		iot = sc->sc_bust;
456ac86e555Speter 		ioh = sc->sc_bush_mem;
457ac86e555Speter 		return (bus_space_read_4(iot, ioh, reg));
458ac86e555Speter 	} else if (__predict_true(pxamemctl_regs != 0)) {
459ac86e555Speter 		return (MEMCTL_BOOTSTRAP_REG(reg));
460ac86e555Speter 	}
461ac86e555Speter 	panic("pxa2x0_memctl_read: not bootstrapped");
462ac86e555Speter 	/*NOTREACHED*/
463ac86e555Speter }
464ac86e555Speter 
465ac86e555Speter void
pxa2x0_memctl_write(int reg,uint32_t val)466ac86e555Speter pxa2x0_memctl_write(int reg, uint32_t val)
467ac86e555Speter {
468ac86e555Speter 	struct pxaip_softc *sc;
469ac86e555Speter 	bus_space_tag_t iot;
470ac86e555Speter 	bus_space_handle_t ioh;
471ac86e555Speter 
472ac86e555Speter 	if (__predict_true(pxaip_sc != NULL)) {
473ac86e555Speter 		sc = pxaip_sc;
474ac86e555Speter 		iot = sc->sc_bust;
475ac86e555Speter 		ioh = sc->sc_bush_mem;
476ac86e555Speter 		bus_space_write_4(iot, ioh, reg, val);
477ac86e555Speter 	} else if (__predict_true(pxamemctl_regs != 0)) {
478ac86e555Speter 		MEMCTL_BOOTSTRAP_REG(reg) = val;
479ac86e555Speter 	} else {
480ac86e555Speter 		panic("pxa2x0_memctl_write: not bootstrapped");
481ac86e555Speter 	}
482ac86e555Speter 	return;
483ac86e555Speter }
484ac86e555Speter 
485ac86e555Speter void
pxa2x0_clkman_bootstrap(vaddr_t va)486dbc112baSober pxa2x0_clkman_bootstrap(vaddr_t va)
487dbc112baSober {
488dbc112baSober 
489dbc112baSober 	pxaclkman_regs = va;
490dbc112baSober }
491dbc112baSober 
492dbc112baSober void
pxa2x0_clkman_config(u_int clk,bool enable)493712239e3Sthorpej pxa2x0_clkman_config(u_int clk, bool enable)
494233a67b2Sscw {
495233a67b2Sscw 	struct pxaip_softc *sc;
496dbc112baSober 	bus_space_tag_t iot;
497dbc112baSober 	bus_space_handle_t ioh;
498dbc112baSober 	uint32_t rv;
499233a67b2Sscw 
500dbc112baSober 	if (__predict_true(pxaip_sc != NULL)) {
501233a67b2Sscw 		sc = pxaip_sc;
502dbc112baSober 		iot = sc->sc_bust;
503dbc112baSober 		ioh = sc->sc_bush_clk;
504233a67b2Sscw 
505dbc112baSober 		rv = bus_space_read_4(iot, ioh, CLKMAN_CKEN);
506233a67b2Sscw 		rv &= ~clk;
507233a67b2Sscw 		if (enable)
508233a67b2Sscw 			rv |= clk;
509dbc112baSober 		bus_space_write_4(iot, ioh, CLKMAN_CKEN, rv);
510dbc112baSober 		return;
511ac86e555Speter 	} else if (__predict_true(pxaclkman_regs != 0)) {
512dbc112baSober 		rv = CLKMAN_BOOTSTRAP_REG(CLKMAN_CKEN);
513dbc112baSober 		rv &= ~clk;
514dbc112baSober 		if (enable)
515dbc112baSober 			rv |= clk;
516dbc112baSober 		CLKMAN_BOOTSTRAP_REG(CLKMAN_CKEN) = rv;
517dbc112baSober 		return;
518dbc112baSober 	}
519dbc112baSober 	panic("pxa2x0_clkman_config: not bootstrapped");
520233a67b2Sscw }
521