Home | History | Annotate | Line # | Download | only in xscale
pxa2x0_apm.c revision 1.7
      1 /*	$NetBSD: pxa2x0_apm.c,v 1.7 2021/09/26 16:36:18 thorpej Exp $	*/
      2 /*	$OpenBSD: pxa2x0_apm.c,v 1.28 2007/03/29 18:42:38 uwe Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 2001 Alexander Guy.  All rights reserved.
      6  * Copyright (c) 1998-2001 Michael Shalayeff. All rights reserved.
      7  * Copyright (c) 1995 John T. Kohl.  All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/kthread.h>
     43 #include <sys/lock.h>
     44 #include <sys/mount.h>		/* for vfs_syncwait() */
     45 #include <sys/proc.h>
     46 #include <sys/device.h>
     47 #include <sys/fcntl.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/event.h>
     50 
     51 #include <machine/cpu.h>
     52 #include <machine/apmvar.h>
     53 
     54 #include <arm/xscale/pxa2x0reg.h>
     55 #include <arm/xscale/pxa2x0var.h>
     56 #include <arm/xscale/pxa2x0_apm.h>
     57 #include <arm/xscale/pxa2x0_gpio.h>
     58 
     59 #if defined(APMDEBUG)
     60 #define DPRINTF(x)	printf x
     61 #else
     62 #define	DPRINTF(x)	/**/
     63 #endif
     64 
     65 #define APM_LOCK(sc)    lockmgr(&(sc)->sc_lock, LK_EXCLUSIVE, NULL)
     66 #define APM_UNLOCK(sc)  lockmgr(&(sc)->sc_lock, LK_RELEASE, NULL)
     67 
     68 #define	APMUNIT(dev)	(minor(dev)&0xf0)
     69 #define	APMDEV(dev)	(minor(dev)&0x0f)
     70 #define APMDEV_NORMAL	0
     71 #define APMDEV_CTL	8
     72 
     73 int	apm_userstandbys;
     74 int	apm_suspends;
     75 int	apm_battlow;
     76 
     77 extern struct cfdriver zapm_cd;
     78 
     79 /* battery percentage at which we get verbose in our warnings.  This
     80    value can be changed using sysctl(8), value machdep.apmwarn.
     81    Setting it to zero kills all warnings */
     82 int	cpu_apmwarn = 10;
     83 
     84 void	apm_power_print(struct pxa2x0_apm_softc *, struct apm_power_info *);
     85 void	apm_power_info(struct pxa2x0_apm_softc *, struct apm_power_info *);
     86 void	apm_suspend(struct pxa2x0_apm_softc *);
     87 void	apm_resume(struct pxa2x0_apm_softc *);
     88 int	apm_get_event(struct pxa2x0_apm_softc *, u_int *);
     89 int	apm_handle_event(struct pxa2x0_apm_softc *, u_int);
     90 void	apm_thread_create(void *);
     91 void	apm_thread(void *);
     92 
     93 #if 0
     94 extern int perflevel;
     95 #endif
     96 
     97 int	freq;
     98 void	pxa2x0_setperf(int speed);
     99 int	pxa2x0_cpuspeed(int *speed);
    100 
    101 int	apm_record_event(struct pxa2x0_apm_softc *, u_int);
    102 #if 0
    103 void	filt_apmrdetach(struct knote *kn);
    104 int	filt_apmread(struct knote *kn, long hint);
    105 int	apmkqfilter(dev_t dev, struct knote *kn);
    106 
    107 static const struct filterops apmread_filtops = {
    108 	.f_flags = FILTEROP_ISFD,
    109 	.f_attach = NULL,
    110 	.f_detach = filt_apmrdetach,
    111 	.f_event = filt_apmread,
    112 };
    113 #endif
    114 
    115 /*
    116  * Flags to control kernel display
    117  *	SCFLAG_NOPRINT:		do not output APM power messages due to
    118  *				a power change event.
    119  *
    120  *	SCFLAG_PCTPRINT:	do not output APM power messages due to
    121  *				to a power change event unless the battery
    122  *				percentage changes.
    123  */
    124 
    125 #define SCFLAG_NOPRINT	0x0008000
    126 #define SCFLAG_PCTPRINT	0x0004000
    127 #define SCFLAG_PRINT	(SCFLAG_NOPRINT|SCFLAG_PCTPRINT)
    128 
    129 #define	SCFLAG_OREAD 	(1 << 0)
    130 #define	SCFLAG_OWRITE	(1 << 1)
    131 #define	SCFLAG_OPEN	(SCFLAG_OREAD|SCFLAG_OWRITE)
    132 
    133 /* This structure must be kept in sync with pxa2x0_apm_asm.S. */
    134 struct pxa2x0_memcfg {
    135 	/* SDRAM refresh */
    136 	uint32_t mdrefr_high;		/* 0x00 */
    137 	uint32_t mdrefr_low;		/* 0x04 */
    138 	uint32_t mdrefr_low2;		/* 0x08 */
    139 	/* Synchronous, static, or VLIO interfaces */
    140 	uint32_t msc_high[3];		/* 0x0c */
    141 	uint32_t msc_low[3];		/* 0x18 */
    142 	/* XXX move up */
    143 	uint32_t mdrefr_91;		/* 0x24 */
    144 };
    145 
    146 /* XXX */
    147 #define MDREFR_C3000	(MDREFR_K0DB2 | MDREFR_E1PIN | MDREFR_K1RUN |	\
    148 	    MDREFR_K1DB2 | MDREFR_K2DB2 | MDREFR_APD)
    149 #define MSC0_HIGH							\
    150 	( 7 << MSC_RRR_SHIFT << 16) |					\
    151 	(15 << MSC_RDN_SHIFT << 16) |					\
    152 	(15 << MSC_RDF_SHIFT << 16) |					\
    153 	(MSC_RT_NONBURST     << 16) |					\
    154 	( 2 << MSC_RRR_SHIFT)       |					\
    155 	(13 << MSC_RDN_SHIFT)       |					\
    156 	(13 << MSC_RDF_SHIFT)       |					\
    157 	MSC_RBW	/* PXA271 */        |					\
    158 	MSC_RT_NONBURST
    159 #define MSC1_HIGH							\
    160 	( 7 << MSC_RRR_SHIFT << 16) |					\
    161 	(15 << MSC_RDN_SHIFT << 16) |					\
    162 	(15 << MSC_RDF_SHIFT << 16) |					\
    163 	(MSC_RT_VLIO         << 16) |					\
    164 	( 3 << MSC_RRR_SHIFT)       |					\
    165 	( 4 << MSC_RDN_SHIFT)       |					\
    166 	(13 << MSC_RDF_SHIFT)       |					\
    167 	MSC_RT_VLIO
    168 #define MSC2_HIGH							\
    169 	( 7 << MSC_RRR_SHIFT << 16) |					\
    170 	(15 << MSC_RDN_SHIFT << 16) |					\
    171 	(15 << MSC_RDF_SHIFT << 16) |					\
    172 	(MSC_RT_NONBURST     << 16) |					\
    173 	( 3 << MSC_RRR_SHIFT)       |					\
    174 	( 4 << MSC_RDN_SHIFT)       |					\
    175 	(13 << MSC_RDF_SHIFT)       |					\
    176 	MSC_RT_VLIO
    177 #define MSC0_LOW							\
    178 	( 7 << MSC_RRR_SHIFT << 16) |					\
    179 	(15 << MSC_RDN_SHIFT << 16) |					\
    180 	(15 << MSC_RDF_SHIFT << 16) |					\
    181 	(MSC_RT_NONBURST     << 16) |					\
    182 	( 1 << MSC_RRR_SHIFT)       |					\
    183 	( 8 << MSC_RDN_SHIFT)       |					\
    184 	( 8 << MSC_RDF_SHIFT)       |					\
    185 	MSC_RBW	/* PXA271 */        |					\
    186 	MSC_RT_NONBURST
    187 #define MSC1_LOW							\
    188 	( 7 << MSC_RRR_SHIFT << 16) |					\
    189 	(15 << MSC_RDN_SHIFT << 16) |					\
    190 	(15 << MSC_RDF_SHIFT << 16) |					\
    191 	(MSC_RT_VLIO         << 16) |					\
    192 	( 1 << MSC_RRR_SHIFT)       |					\
    193 	( 2 << MSC_RDN_SHIFT)       |					\
    194 	( 6 << MSC_RDF_SHIFT)       |					\
    195 	MSC_RT_VLIO
    196 #define MSC2_LOW							\
    197 	( 7 << MSC_RRR_SHIFT << 16) |					\
    198 	(15 << MSC_RDN_SHIFT << 16) |					\
    199 	(15 << MSC_RDF_SHIFT << 16) |					\
    200 	(MSC_RT_NONBURST     << 16) |					\
    201 	( 1 << MSC_RRR_SHIFT)       |					\
    202 	( 2 << MSC_RDN_SHIFT)       |					\
    203 	( 6 << MSC_RDF_SHIFT)       |					\
    204 	MSC_RT_VLIO
    205 struct pxa2x0_memcfg pxa2x0_memcfg = {
    206 	(MDREFR_C3000 | 0x030),
    207 		(MDREFR_C3000 | 0x00b),
    208 		(MDREFR_C3000 | 0x017),
    209 	{ MSC0_HIGH, MSC1_HIGH, MSC2_HIGH },
    210 	{ MSC1_LOW, MSC1_LOW, MSC2_LOW },
    211 		(MDREFR_C3000 | 0x013)
    212 };
    213 
    214 #define PI2C_RETRY_COUNT	10
    215 /* XXX varies depending on voltage regulator IC. */
    216 #define PI2C_VOLTAGE_LOW	0x13	/* 1.00V */
    217 #define PI2C_VOLTAGE_HIGH	0x1a	/* 1.35V */
    218 
    219 void	pxa2x0_pi2c_open(bus_space_tag_t, bus_space_handle_t);
    220 void	pxa2x0_pi2c_close(bus_space_tag_t, bus_space_handle_t);
    221 int	pxa2x0_pi2c_read(bus_space_tag_t, bus_space_handle_t, u_char, u_char *);
    222 int	pxa2x0_pi2c_write(bus_space_tag_t, bus_space_handle_t, u_char, u_char);
    223 int	pxa2x0_pi2c_getvoltage(bus_space_tag_t, bus_space_handle_t, u_char *);
    224 int	pxa2x0_pi2c_setvoltage(bus_space_tag_t, bus_space_handle_t, u_char);
    225 #if 0
    226 void	pxa2x0_pi2c_print(struct pxa2x0_apm_softc *);
    227 #endif
    228 
    229 /* XXX used in pxa2x0_apm_asm.S */
    230 bus_space_handle_t pxa2x0_gpio_ioh;
    231 bus_space_handle_t pxa2x0_clkman_ioh;
    232 bus_space_handle_t pxa2x0_memctl_ioh;
    233 
    234 /* pxa2x0_apm_asm.S */
    235 void	pxa27x_run_mode(void);
    236 void	pxa27x_fastbus_run_mode(int, uint32_t);
    237 void	pxa27x_frequency_change(int, int, struct pxa2x0_memcfg *);
    238 void	pxa2x0_cpu_suspend(void);
    239 void	pxa2x0_cpu_resume(void);
    240 void	pxa27x_cpu_speed_high(void);
    241 void	pxa27x_cpu_speed_low(void);
    242 void	pxa27x_cpu_speed_91(void);
    243 void	pxa27x_cpu_speed_208(void);
    244 
    245 void
    246 apm_power_print(struct pxa2x0_apm_softc *sc, struct apm_power_info *powerp)
    247 {
    248 
    249 	if (powerp->battery_life != APM_BATT_LIFE_UNKNOWN)
    250 		printf("%s: battery life expectancy %d%%\n",
    251 		    device_xname(sc->sc_dev), powerp->battery_life);
    252 
    253 	printf("%s: AC ", device_xname(sc->sc_dev));
    254 	switch (powerp->ac_state) {
    255 	case APM_AC_OFF:
    256 		printf("off,");
    257 		break;
    258 	case APM_AC_ON:
    259 		printf("on,");
    260 		break;
    261 	case APM_AC_BACKUP:
    262 		printf("backup power,");
    263 		break;
    264 	default:
    265 	case APM_AC_UNKNOWN:
    266 		printf("unknown,");
    267 		break;
    268 	}
    269 
    270 	printf(" battery is ");
    271 	switch (powerp->battery_state) {
    272 	case APM_BATT_HIGH:
    273 		printf("high");
    274 		break;
    275 	case APM_BATT_LOW:
    276 		printf("low");
    277 		break;
    278 	case APM_BATT_CRITICAL:
    279 		printf("CRITICAL");
    280 		break;
    281 	case APM_BATT_CHARGING:
    282 		printf("charging");
    283 		break;
    284 	case APM_BATT_UNKNOWN:
    285 		printf("unknown");
    286 		break;
    287 	default:
    288 		printf("undecoded (%x)", powerp->battery_state);
    289 		break;
    290 	}
    291 
    292 	printf("\n");
    293 }
    294 
    295 void
    296 apm_power_info(struct pxa2x0_apm_softc *sc,
    297     struct apm_power_info *power)
    298 {
    299 
    300 	power->ac_state = APM_AC_UNKNOWN;
    301 	power->battery_state = APM_BATT_UNKNOWN;
    302 	power->battery_life = 0 /* APM_BATT_LIFE_UNKNOWN */;
    303 	power->minutes_left = 0;
    304 
    305 	if (sc->sc_power_info != NULL)
    306 		sc->sc_power_info(sc, power);
    307 }
    308 
    309 void
    310 apm_suspend(struct pxa2x0_apm_softc *sc)
    311 {
    312 
    313 	resettodr();
    314 
    315 	dopowerhooks(PWR_SUSPEND);
    316 
    317 #if 0
    318 	if (cold)
    319 		vfs_syncwait(0);
    320 #endif
    321 
    322 	if (sc->sc_suspend == NULL)
    323 		pxa2x0_wakeup_config(PXA2X0_WAKEUP_ALL, 1);
    324 	else
    325 		sc->sc_suspend(sc);
    326 
    327 	pxa2x0_apm_sleep(sc);
    328 }
    329 
    330 void
    331 apm_resume(struct pxa2x0_apm_softc *sc)
    332 {
    333 
    334 	dopowerhooks(PWR_RESUME);
    335 
    336 	inittodr(0);
    337 
    338 	/*
    339 	 * Clear the OTG Peripheral hold after running the pxaudc and pxaohci
    340 	 * powerhooks to re-enable their operation. See 3.8.1.2
    341 	 */
    342 	/* XXX ifdef NPXAUDC > 0 */
    343 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PSSR, PSSR_OTGPH);
    344 }
    345 
    346 #if 0
    347 int
    348 apm_get_event(struct pxa2x0_apm_softc *sc, u_int *typep)
    349 {
    350 
    351 	if (sc->sc_get_event != NULL)
    352 		return (sc->sc_get_event(sc, typep));
    353 
    354 	*typep = APM_NOEVENT;
    355 	return (1);
    356 }
    357 
    358 int
    359 apm_handle_event(struct pxa2x0_apm_softc *sc, u_int type)
    360 {
    361 	struct	apm_power_info power;
    362 	int	ret = 0;
    363 
    364 	switch (type) {
    365 	case APM_NOEVENT:
    366 		ret = 1;
    367 		break;
    368 	case APM_CRIT_SUSPEND_REQ:
    369 		DPRINTF(("suspend required immediately\n"));
    370 #if 0
    371 		/* XXX apmd would make us suspend again after resume. */
    372 		(void)apm_record_event(sc, type);
    373 #endif
    374 		/*
    375 		 * We ignore APM_CRIT_RESUME and just suspend here as usual
    376 		 * to simplify the actual apm_get_event() implementation.
    377 		 */
    378 		apm_suspends++;
    379 		ret = 1;
    380 		break;
    381 	case APM_USER_SUSPEND_REQ:
    382 	case APM_SUSPEND_REQ:
    383 		DPRINTF(("suspend requested\n"));
    384 		if (apm_record_event(sc, type)) {
    385 			DPRINTF(("suspend ourselves\n"));
    386 			apm_suspends++;
    387 		}
    388 		break;
    389 	case APM_POWER_CHANGE:
    390 		DPRINTF(("power status change\n"));
    391 		apm_power_info(sc, &power);
    392 		if (power.battery_life != APM_BATT_LIFE_UNKNOWN &&
    393 		    power.battery_life < cpu_apmwarn &&
    394 		    (sc->sc_flags & SCFLAG_PRINT) != SCFLAG_NOPRINT &&
    395 		    ((sc->sc_flags & SCFLAG_PRINT) != SCFLAG_PCTPRINT ||
    396 			sc->sc_batt_life != power.battery_life)) {
    397 			sc->sc_batt_life = power.battery_life;
    398 			apm_power_print(sc, &power);
    399 		}
    400 		apm_record_event(sc, type);
    401 		break;
    402 	case APM_BATTERY_LOW:
    403 		DPRINTF(("Battery low!\n"));
    404 		apm_battlow++;
    405 		apm_record_event(sc, type);
    406 		break;
    407 	default:
    408 		DPRINTF(("apm_handle_event: unsupported event, code %d\n",
    409 			type));
    410 	}
    411 
    412 	return (ret);
    413 }
    414 
    415 void
    416 apm_thread_create(void *v)
    417 {
    418 	struct pxa2x0_apm_softc *sc = v;
    419 
    420 	if (kthread_create(apm_thread, sc, &sc->sc_thread,
    421 		"%s", device_xname(sc->sc_dev))) {
    422 		/* apm_disconnect(sc); */
    423 		printf("%s: failed to create kernel thread, disabled",
    424 		    device_xname(sc->sc_dev));
    425 	}
    426 }
    427 
    428 void
    429 apm_thread(void *v)
    430 {
    431 	struct pxa2x0_apm_softc *sc = v;
    432 	u_int	type;
    433 
    434 	for (;;) {
    435 		APM_LOCK(sc);
    436 
    437 		while (1) {
    438 			if (apm_get_event(sc, &type) != 0)
    439 				break;
    440 			if (apm_handle_event(sc, type) != 0)
    441 				break;
    442 		}
    443 		if (apm_suspends || apm_userstandbys /* || apm_battlow*/) {
    444 			apm_suspend(sc);
    445 			apm_resume(sc);
    446 		}
    447 		apm_battlow = apm_suspends = apm_userstandbys = 0;
    448 
    449 		APM_UNLOCK(sc);
    450 		kpause("apmev", false, hz, NULL);
    451 	}
    452 }
    453 
    454 int
    455 apmopen(dev_t dev, int flag, int mode, struct proc *p)
    456 {
    457 	struct pxa2x0_apm_softc *sc;
    458 	int error = 0;
    459 
    460 	/* apm0 only */
    461 	if (!zapm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    462 	    !(sc = zapm_cd.cd_devs[APMUNIT(dev)]))
    463 		return (ENXIO);
    464 
    465 	DPRINTF(("apmopen: dev %d pid %d flag %x mode %x\n",
    466 		APMDEV(dev), p->p_pid, flag, mode));
    467 
    468 	switch (APMDEV(dev)) {
    469 	case APMDEV_CTL:
    470 		if (!(flag & FWRITE)) {
    471 			error = EINVAL;
    472 			break;
    473 		}
    474 		if (sc->sc_flags & SCFLAG_OWRITE) {
    475 			error = EBUSY;
    476 			break;
    477 		}
    478 		sc->sc_flags |= SCFLAG_OWRITE;
    479 		break;
    480 	case APMDEV_NORMAL:
    481 		if (!(flag & FREAD) || (flag & FWRITE)) {
    482 			error = EINVAL;
    483 			break;
    484 		}
    485 		sc->sc_flags |= SCFLAG_OREAD;
    486 		break;
    487 	default:
    488 		error = ENXIO;
    489 		break;
    490 	}
    491 	return (error);
    492 }
    493 
    494 int
    495 apmclose(dev_t dev, int flag, int mode, struct proc *p)
    496 {
    497 	struct pxa2x0_apm_softc *sc;
    498 
    499 	/* apm0 only */
    500 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    501 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    502 		return (ENXIO);
    503 
    504 	DPRINTF(("apmclose: pid %d flag %x mode %x\n", p->p_pid, flag, mode));
    505 
    506 	switch (APMDEV(dev)) {
    507 	case APMDEV_CTL:
    508 		sc->sc_flags &= ~SCFLAG_OWRITE;
    509 		break;
    510 	case APMDEV_NORMAL:
    511 		sc->sc_flags &= ~SCFLAG_OREAD;
    512 		break;
    513 	}
    514 	return (0);
    515 }
    516 
    517 int
    518 apmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    519 {
    520 	struct pxa2x0_apm_softc *sc;
    521 	struct apm_power_info *power;
    522 	int error = 0;
    523 
    524 	/* apm0 only */
    525 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    526 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    527 		return (ENXIO);
    528 
    529 	switch (cmd) {
    530 		/* some ioctl names from linux */
    531 	case APM_IOC_STANDBY:
    532 		if ((flag & FWRITE) == 0)
    533 			error = EBADF;
    534 		else
    535 			apm_userstandbys++;
    536 		break;
    537 	case APM_IOC_SUSPEND:
    538 		if ((flag & FWRITE) == 0)
    539 			error = EBADF;
    540 		else
    541 			apm_suspends++;	/* XXX */
    542 		break;
    543 	case APM_IOC_PRN_CTL:
    544 		if ((flag & FWRITE) == 0)
    545 			error = EBADF;
    546 		else {
    547 			int flag = *(int *)data;
    548 			DPRINTF(( "APM_IOC_PRN_CTL: %d\n", flag ));
    549 			switch (flag) {
    550 			case APM_PRINT_ON:	/* enable printing */
    551 				sc->sc_flags &= ~SCFLAG_PRINT;
    552 				break;
    553 			case APM_PRINT_OFF: /* disable printing */
    554 				sc->sc_flags &= ~SCFLAG_PRINT;
    555 				sc->sc_flags |= SCFLAG_NOPRINT;
    556 				break;
    557 			case APM_PRINT_PCT: /* disable some printing */
    558 				sc->sc_flags &= ~SCFLAG_PRINT;
    559 				sc->sc_flags |= SCFLAG_PCTPRINT;
    560 				break;
    561 			default:
    562 				error = EINVAL;
    563 				break;
    564 			}
    565 		}
    566 		break;
    567 	case APM_IOC_DEV_CTL:
    568 		if ((flag & FWRITE) == 0)
    569 			error = EBADF;
    570 		break;
    571 	case APM_IOC_GETPOWER:
    572 	        power = (struct apm_power_info *)data;
    573 		apm_power_info(sc, power);
    574 		break;
    575 
    576 	default:
    577 		error = ENOTTY;
    578 	}
    579 
    580 	return (error);
    581 }
    582 
    583 int
    584 apm_record_event(struct pxa2x0_apm_softc *sc, u_int type)
    585 {
    586 	static int apm_evindex;
    587 
    588 	/* skip if no user waiting */
    589 	if ((sc->sc_flags & SCFLAG_OPEN) == 0)
    590 		return (1);
    591 
    592 	apm_evindex++;
    593 	KNOTE(&sc->sc_note, APM_EVENT_COMPOSE(type, apm_evindex));
    594 
    595 	return (0);
    596 }
    597 
    598 void
    599 filt_apmrdetach(struct knote *kn)
    600 {
    601 	struct pxa2x0_apm_softc *sc =
    602 	    (struct pxa2x0_apm_softc *)kn->kn_hook;
    603 
    604 	SLIST_REMOVE(&sc->sc_note, kn, knote, kn_selnext);
    605 }
    606 
    607 int
    608 filt_apmread(struct knote *kn, long hint)
    609 {
    610 	/* XXX weird kqueue_scan() semantics */
    611 	if (hint && !kn->kn_data)
    612 		kn->kn_data = (int)hint;
    613 
    614 	return (1);
    615 }
    616 
    617 int
    618 apmkqfilter(dev_t dev, struct knote *kn)
    619 {
    620 	struct pxa2x0_apm_softc *sc;
    621 
    622 	/* apm0 only */
    623 	if (!apm_cd.cd_ndevs || APMUNIT(dev) != 0 ||
    624 	    !(sc = apm_cd.cd_devs[APMUNIT(dev)]))
    625 		return (ENXIO);
    626 
    627 	switch (kn->kn_filter) {
    628 	case EVFILT_READ:
    629 		kn->kn_fop = &apmread_filtops;
    630 		break;
    631 	default:
    632 		return (EINVAL);
    633 	}
    634 
    635 	kn->kn_hook = (caddr_t)sc;
    636 	SLIST_INSERT_HEAD(&sc->sc_note, kn, kn_selnext);
    637 
    638 	return (0);
    639 }
    640 
    641 void
    642 pxa2x0_apm_attach_sub(struct pxa2x0_apm_softc *sc)
    643 {
    644 
    645 	sc->sc_iot = &pxa2x0_bs_tag;
    646 
    647 	if (bus_space_map(sc->sc_iot, PXA2X0_POWMAN_BASE,
    648 		PXA2X0_POWMAN_SIZE, 0, &sc->sc_pm_ioh)) {
    649 		printf("pxa2x0_apm_attach_sub: failed to map POWMAN\n");
    650 		return;
    651 	}
    652 
    653 	lockinit(&sc->sc_lock, PWAIT, "apmlk", 0, 0);
    654 
    655 	kthread_create_deferred(apm_thread_create, sc);
    656 
    657 	printf("\n");
    658 
    659 	if (bus_space_map(sc->sc_iot, PXA2X0_CLKMAN_BASE, PXA2X0_CLKMAN_SIZE,
    660 		0, &pxa2x0_clkman_ioh)) {
    661 		printf("%s: failed to map CLKMAN\n", device_xname(sc->sc_dev));
    662 		return;
    663 	}
    664 
    665 	if (bus_space_map(sc->sc_iot, PXA2X0_MEMCTL_BASE, PXA2X0_MEMCTL_SIZE,
    666 		0, &pxa2x0_memctl_ioh)) {
    667 		printf("%s: failed to map MEMCTL\n", device_xname(sc->sc_dev));
    668 		return;
    669 	}
    670 	sc->sc_memctl_ioh = pxa2x0_memctl_ioh;
    671 
    672 	if (bus_space_map(sc->sc_iot, PXA2X0_GPIO_BASE, PXA2X0_GPIO_SIZE,
    673 		0, &pxa2x0_gpio_ioh)) {
    674 		printf("%s: can't map GPIO\n", device_xname(sc->sc_dev));
    675 		return;
    676 	}
    677 
    678 	/* Clear all reset status flags. */
    679 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_RCSR,
    680 	    RCSR_GPR | RCSR_SMR | RCSR_WDR | RCSR_HWR);
    681 }
    682 #endif /* 0 */
    683 
    684 void
    685 pxa2x0_wakeup_config(u_int wsrc, int enable)
    686 {
    687 	struct pxa2x0_apm_softc *sc;
    688 	uint32_t prer;
    689 	uint32_t pfer;
    690 	uint32_t pkwr;
    691 
    692 	if (zapm_cd.cd_ndevs < 1 || zapm_cd.cd_devs[0] == NULL)
    693 		return;
    694 	sc = device_private(zapm_cd.cd_devs[0]);
    695 
    696 	prer = pfer = pkwr = 0;
    697 
    698 	if ((wsrc & PXA2X0_WAKEUP_POWERON) != 0) {
    699 		prer |= (1<<0);
    700 		pfer |= (1<<0);
    701 		pkwr |= (1<<12); /* XXX */
    702 	}
    703 
    704 	if ((wsrc & PXA2X0_WAKEUP_GPIORST) != 0)
    705 		pfer |= (1<<1);
    706 	if ((wsrc & PXA2X0_WAKEUP_SD) != 0)
    707 		prer |= (1<<9);
    708 	if ((wsrc & PXA2X0_WAKEUP_RC) != 0)
    709 		prer |= (1<<13);
    710 	if ((wsrc & PXA2X0_WAKEUP_SYNC) != 0)
    711 		pkwr |= (1<<1);
    712 	if ((wsrc & PXA2X0_WAKEUP_KEYNS0) != 0)
    713 		prer |= (1<<12);
    714 	if ((wsrc & PXA2X0_WAKEUP_KEYNS1) != 0)
    715 		pkwr |= (1<<2);
    716 	if ((wsrc & PXA2X0_WAKEUP_KEYNS2) != 0)
    717 		pkwr |= (1<<9);
    718 	if ((wsrc & PXA2X0_WAKEUP_KEYNS3) != 0)
    719 		pkwr |= (1<<3);
    720 	if ((wsrc & PXA2X0_WAKEUP_KEYNS4) != 0)
    721 		pkwr |= (1<<4);
    722 	if ((wsrc & PXA2X0_WAKEUP_KEYNS5) != 0)
    723 		pkwr |= (1<<6);
    724 	if ((wsrc & PXA2X0_WAKEUP_KEYNS6) != 0)
    725 		pkwr |= (1<<7);
    726 	if ((wsrc & PXA2X0_WAKEUP_CF0) != 0)
    727 		pkwr |= (1<<11);
    728 	if ((wsrc & PXA2X0_WAKEUP_CF1) != 0)
    729 		pkwr |= (1<<10);
    730 	if ((wsrc & PXA2X0_WAKEUP_USBD) != 0)
    731 		prer |= (1<<24);
    732 
    733 	if ((wsrc & PXA2X0_WAKEUP_LOCKSW) != 0) {
    734 		prer |= (1<<15);
    735 		pfer |= (1<<15);
    736 	}
    737 
    738 	if ((wsrc & PXA2X0_WAKEUP_JACKIN) != 0) {
    739 		prer |= (1<<23);
    740 		pfer |= (1<<23);
    741 	}
    742 
    743 	if ((wsrc & PXA2X0_WAKEUP_CHRGFULL) != 0)
    744 		pkwr |= (1<<18);
    745 	if ((wsrc & PXA2X0_WAKEUP_RTC) != 0)
    746 		prer |= (1<<31);
    747 
    748 	if (enable) {
    749 		sc->sc_wakeon |= wsrc;
    750 		prer |= bus_space_read_4(sc->sc_iot, sc->sc_pm_ioh,
    751 		    POWMAN_PRER);
    752 		pfer |= bus_space_read_4(sc->sc_iot, sc->sc_pm_ioh,
    753 		    POWMAN_PFER);
    754 		pkwr |= bus_space_read_4(sc->sc_iot, sc->sc_pm_ioh,
    755 		    POWMAN_PKWR);
    756 	} else {
    757 		sc->sc_wakeon &= ~wsrc;
    758 		prer = bus_space_read_4(sc->sc_iot, sc->sc_pm_ioh,
    759 		    POWMAN_PRER) & ~prer;
    760 		pfer = bus_space_read_4(sc->sc_iot, sc->sc_pm_ioh,
    761 		    POWMAN_PFER) & ~pfer;
    762 		pkwr = bus_space_read_4(sc->sc_iot, sc->sc_pm_ioh,
    763 		    POWMAN_PKWR) & ~pkwr;
    764 	}
    765 
    766 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PKWR, pkwr);
    767 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PRER, prer);
    768 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PFER, pfer);
    769 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PWER,
    770 	    prer | pfer);
    771 }
    772 
    773 u_int
    774 pxa2x0_wakeup_status(void)
    775 {
    776 	struct pxa2x0_apm_softc *sc;
    777 	uint32_t rv;
    778 	u_int	wsrc;
    779 
    780 	if (zapm_cd.cd_ndevs < 1 || zapm_cd.cd_devs[0] == NULL)
    781 		return (0);
    782 
    783 	sc = device_private(zapm_cd.cd_devs[0]);
    784 	wsrc = 0;
    785 
    786 	rv = bus_space_read_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PEDR);
    787 	if ((rv & (1<<0)) != 0)
    788 		wsrc |= PXA2X0_WAKEUP_POWERON;
    789 	if ((rv & (1<<1)) != 0)
    790 		wsrc |= PXA2X0_WAKEUP_GPIORST;
    791 	if ((rv & (1<<9)) != 0)
    792 		wsrc |= PXA2X0_WAKEUP_SD;
    793 	if ((rv & (1<<12)) != 0)
    794 		wsrc |= PXA2X0_WAKEUP_KEYNS0;
    795 	if ((rv & (1<<13)) != 0)
    796 		wsrc |= PXA2X0_WAKEUP_RC;
    797 	if ((rv & (1<<15)) != 0)
    798 		wsrc |= PXA2X0_WAKEUP_LOCKSW;
    799 	if ((rv & (1<<23)) != 0)
    800 		wsrc |= PXA2X0_WAKEUP_JACKIN;
    801 	if ((rv & (1<<24)) != 0)
    802 		wsrc |= PXA2X0_WAKEUP_USBD;
    803 	if ((rv & (1<<31)) != 0)
    804 		wsrc |= PXA2X0_WAKEUP_RTC;
    805 
    806 	rv = bus_space_read_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PKSR);
    807 	if ((rv & (1<<1)) != 0)
    808 		wsrc |= PXA2X0_WAKEUP_SYNC;
    809 	if ((rv & (1<<2)) != 0)
    810 		wsrc |= PXA2X0_WAKEUP_KEYNS1;
    811 	if ((rv & (1<<9)) != 0)
    812 		wsrc |= PXA2X0_WAKEUP_KEYNS2;
    813 	if ((rv & (1<<3)) != 0)
    814 		wsrc |= PXA2X0_WAKEUP_KEYNS3;
    815 	if ((rv & (1<<4)) != 0)
    816 		wsrc |= PXA2X0_WAKEUP_KEYNS4;
    817 	if ((rv & (1<<6)) != 0)
    818 		wsrc |= PXA2X0_WAKEUP_KEYNS5;
    819 	if ((rv & (1<<7)) != 0)
    820 		wsrc |= PXA2X0_WAKEUP_KEYNS6;
    821 	if ((rv & (1<<10)) != 0)
    822 		wsrc |= PXA2X0_WAKEUP_CF1;
    823 	if ((rv & (1<<11)) != 0)
    824 		wsrc |= PXA2X0_WAKEUP_CF0;
    825 	if ((rv & (1<<12)) != 0)
    826 		wsrc |= PXA2X0_WAKEUP_POWERON;
    827 	if ((rv & (1<<18)) != 0)
    828 		wsrc |= PXA2X0_WAKEUP_CHRGFULL;
    829 
    830 	return (wsrc);
    831 }
    832 
    833 struct pxa2x0_sleep_data {
    834 	/* OS timer registers */
    835 	uint32_t sd_osmr0, sd_osmr1, sd_osmr2, sd_osmr3;
    836 	uint32_t sd_oscr0;
    837 	uint32_t sd_osmr4, sd_osmr5;
    838 	uint32_t sd_oscr4;
    839 	uint32_t sd_omcr4, sd_omcr5;
    840 	uint32_t sd_oier;
    841 	/* GPIO registers */
    842 	uint32_t sd_gpdr0, sd_gpdr1, sd_gpdr2, sd_gpdr3;
    843 	uint32_t sd_grer0, sd_grer1, sd_grer2, sd_grer3;
    844 	uint32_t sd_gfer0, sd_gfer1, sd_gfer2, sd_gfer3;
    845 	uint32_t sd_gafr0_l, sd_gafr1_l, sd_gafr2_l, sd_gafr3_l;
    846 	uint32_t sd_gafr0_u, sd_gafr1_u, sd_gafr2_u, sd_gafr3_u;
    847 	uint32_t sd_gplr0, sd_gplr1, sd_gplr2, sd_gplr3;
    848 	/* Interrupt controller registers */
    849 	uint32_t sd_iclr;
    850 	uint32_t sd_icmr;
    851 	uint32_t sd_iccr;
    852 	/* Memory controller registers */
    853 	uint32_t sd_mecr;
    854 	uint32_t sd_mcmem0, sd_mcmem1;
    855 	uint32_t sd_mcatt0, sd_mcatt1;
    856 	uint32_t sd_mcio0, sd_mcio1;
    857 	/* Clocks manager registers */
    858 	uint32_t sd_cken;
    859 };
    860 
    861 void
    862 pxa2x0_apm_sleep(struct pxa2x0_apm_softc *sc)
    863 {
    864 	struct pxa2x0_sleep_data sd;
    865 	bus_space_handle_t ost_ioh;
    866 	int save;
    867 	uint32_t rv;
    868 
    869 	ost_ioh = (bus_space_handle_t)0;
    870 	if (bus_space_map(sc->sc_iot, PXA2X0_OST_BASE, PXA2X0_OST_SIZE, 0,
    871 		&ost_ioh)) {
    872 		printf("pxa2x0_apm_sleep: can't map OST\n");
    873 		goto out;
    874 	}
    875 
    876 	save = disable_interrupts(I32_bit|F32_bit);
    877 
    878 	sd.sd_oscr0 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OSCR0);
    879 	sd.sd_oscr4 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OSCR4);
    880 	sd.sd_omcr4 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OMCR4);
    881 	sd.sd_omcr5 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OMCR5);
    882 	sd.sd_osmr0 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OSMR0);
    883 	sd.sd_osmr1 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OSMR1);
    884 	sd.sd_osmr2 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OSMR2);
    885 	sd.sd_osmr3 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OSMR3);
    886 	sd.sd_osmr4 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OSMR4);
    887 	sd.sd_osmr5 = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OSMR5);
    888 	sd.sd_oier = bus_space_read_4(sc->sc_iot, ost_ioh, OST_OIER);
    889 
    890 	/* Bring the PXA27x into 416MHz turbo mode. */
    891         if ((cputype & ~CPU_ID_XSCALE_COREREV_MASK) == CPU_ID_PXA27X &&
    892 	    bus_space_read_4(sc->sc_iot, pxa2x0_clkman_ioh, CLKMAN_CCCR) !=
    893 	    (CCCR_A | CCCR_TURBO_X2 | CCCR_RUN_X16)) {
    894 #if 0
    895 		pxa27x_cpu_speed_high();
    896 #else
    897 #define CLKCFG_T		(1<<0)	/* turbo */
    898 #define CLKCFG_F		(1<<1)	/* frequency change */
    899 #define CLKCFG_B		(1<<3)	/* fast-bus */
    900 		pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X2 |
    901 		    CCCR_RUN_X16, CLKCFG_B | CLKCFG_F | CLKCFG_T,
    902 		    &pxa2x0_memcfg);
    903 #endif
    904 		delay(500000); /* XXX */
    905 	}
    906 
    907 suspend_again:
    908 	/* Clear wake-up status. */
    909 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PEDR,
    910 	    0xffffffff);
    911 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PKSR,
    912 	    0xffffffff);
    913 
    914 	/* XXX control battery charging in sleep mode. */
    915 
    916 	/* XXX schedule RTC alarm to check the battery, or schedule
    917 	   XXX wake-up shortly before an already programmed alarm? */
    918 
    919 	pxa27x_run_mode();
    920 #define MDREFR_LOW		(MDREFR_C3000 | 0x00b)
    921 	pxa27x_fastbus_run_mode(0, MDREFR_LOW);
    922 	delay(1);
    923 #if 1
    924 	pxa27x_cpu_speed_91();
    925 #else
    926 	pxa27x_frequency_change(CCCR_TURBO_X1 | CCCR_RUN_X7, CLKCFG_F,
    927 	    &pxa2x0_memcfg);
    928 #endif
    929 	pxa2x0_pi2c_setvoltage(sc->sc_iot, sc->sc_pm_ioh, PI2C_VOLTAGE_LOW);
    930 
    931 	sd.sd_gpdr0 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR0);
    932 	sd.sd_gpdr1 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR1);
    933 	sd.sd_gpdr2 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR2);
    934 	sd.sd_gpdr3 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR3);
    935 
    936 	sd.sd_grer0 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GRER0);
    937 	sd.sd_grer1 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GRER1);
    938 	sd.sd_grer2 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GRER2);
    939 	sd.sd_grer3 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GRER3);
    940 
    941 	sd.sd_gfer0 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GFER0);
    942 	sd.sd_gfer1 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GFER1);
    943 	sd.sd_gfer2 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GFER2);
    944 	sd.sd_gfer3 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GFER3);
    945 
    946 	sd.sd_gafr0_l = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR0_L);
    947 	sd.sd_gafr1_l = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR1_L);
    948 	sd.sd_gafr2_l = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR2_L);
    949 	sd.sd_gafr3_l = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR3_L);
    950 
    951 	sd.sd_gafr0_u = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR0_U);
    952 	sd.sd_gafr1_u = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR1_U);
    953 	sd.sd_gafr2_u = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR2_U);
    954 	sd.sd_gafr3_u = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR3_U);
    955 
    956 	sd.sd_gplr0 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPLR0);
    957 	sd.sd_gplr1 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPLR1);
    958 	sd.sd_gplr2 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPLR2);
    959 	sd.sd_gplr3 = bus_space_read_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPLR3);
    960 
    961 	sd.sd_iclr = read_icu(INTCTL_ICLR);
    962 	sd.sd_icmr = read_icu(INTCTL_ICMR);
    963 	sd.sd_iccr = read_icu(INTCTL_ICCR);
    964 	write_icu(INTCTL_ICMR, 0);
    965 
    966 	sd.sd_mecr = bus_space_read_4(sc->sc_iot, pxa2x0_memctl_ioh,
    967 	    MEMCTL_MECR);
    968 	sd.sd_mcmem0 = bus_space_read_4(sc->sc_iot, pxa2x0_memctl_ioh,
    969 	    MEMCTL_MCMEM(0));
    970 	sd.sd_mcmem1 = bus_space_read_4(sc->sc_iot, pxa2x0_memctl_ioh,
    971 	    MEMCTL_MCMEM(1));
    972 	sd.sd_mcatt0 = bus_space_read_4(sc->sc_iot, pxa2x0_memctl_ioh,
    973 	    MEMCTL_MCATT(0));
    974 	sd.sd_mcatt1 = bus_space_read_4(sc->sc_iot, pxa2x0_memctl_ioh,
    975 	    MEMCTL_MCATT(1));
    976 	sd.sd_mcio0 = bus_space_read_4(sc->sc_iot, pxa2x0_memctl_ioh,
    977 	    MEMCTL_MCIO(0));
    978 	sd.sd_mcio1 = bus_space_read_4(sc->sc_iot, pxa2x0_memctl_ioh,
    979 	    MEMCTL_MCIO(1));
    980 
    981 	sd.sd_cken = bus_space_read_4(sc->sc_iot, pxa2x0_clkman_ioh,
    982 	    CLKMAN_CKEN);
    983 
    984 	/*
    985 	 * Stop clocks to all units except to the memory controller, and
    986 	 * to the keypad controller if it is enabled as a wake-up source.
    987 	 */
    988 	rv = CKEN_MEM;
    989 	if ((sc->sc_wakeon & PXA2X0_WAKEUP_KEYNS_ALL) != 0)
    990 		rv |= CKEN_KEY;
    991 	bus_space_write_4(sc->sc_iot, pxa2x0_clkman_ioh, CLKMAN_CKEN, rv);
    992 
    993 	/* Disable nRESET_OUT. */
    994 	rv = bus_space_read_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PSLR);
    995 #define  PSLR_SL_ROD	(1<<20)
    996 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PSLR,
    997 	    rv | PSLR_SL_ROD);
    998 
    999 	/* Clear all reset status flags. */
   1000 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_RCSR,
   1001 	    RCSR_GPR | RCSR_SMR | RCSR_WDR | RCSR_HWR);
   1002 
   1003 	/* Stop 3/13MHz oscillator; do not float PCMCIA and chip-selects. */
   1004 	rv = PCFR_OPDE;
   1005         if ((cputype & ~CPU_ID_XSCALE_COREREV_MASK) == CPU_ID_PXA27X)
   1006 		/* Enable nRESET_GPIO as a GPIO reset input. */
   1007 		rv |= PCFR_GPR_EN;
   1008 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PCFR, rv);
   1009 
   1010 	/* XXX C3000 */
   1011 #define	GPIO_G0_STROBE_BIT		0x0f800000
   1012 #define	GPIO_G1_STROBE_BIT		0x00100000
   1013 #define	GPIO_G2_STROBE_BIT		0x01000000
   1014 #define	GPIO_G3_STROBE_BIT		0x00041880
   1015 #define	GPIO_KEY_STROBE0		88
   1016 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PGSR0,
   1017 	    0x00144018);
   1018 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PGSR1,
   1019 	    0x00ef0000);
   1020 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PGSR2,
   1021 	    0x0121c000);
   1022 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PGSR3,
   1023 	    0x00600000);
   1024 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PGSR0,
   1025 	    0x00144018 & ~GPIO_G0_STROBE_BIT);
   1026 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PGSR1,
   1027 	    0x00ef0000 & ~GPIO_G1_STROBE_BIT);
   1028 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PGSR2,
   1029 	    0x0121c000 & ~GPIO_G2_STROBE_BIT);
   1030 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PGSR3,
   1031 	    0x00600000 & ~GPIO_G3_STROBE_BIT);
   1032 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PGSR2,
   1033 	    (0x0121c000 & ~GPIO_G2_STROBE_BIT) |
   1034 	    GPIO_BIT(GPIO_KEY_STROBE0));
   1035 
   1036 	/* C3000 */
   1037 #define GPIO_EXT_BUS_READY	18
   1038 	pxa2x0_gpio_set_function(GPIO_EXT_BUS_READY, GPIO_SET | GPIO_OUT);
   1039 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR0, 0xd01c4418);
   1040 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR1, 0xfcefbd21);
   1041 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR2, 0x13a5ffff);
   1042 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR3, 0x01e3e10c);
   1043 
   1044 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PSPR,
   1045 	    (uint32_t)&pxa2x0_cpu_resume - 0xc0200000 + 0xa0200000);
   1046 
   1047 	pxa2x0_cpu_suspend();
   1048 
   1049 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PSPR, 0);
   1050 
   1051 	pxa2x0_clkman_config(CKEN_SSP|CKEN_PWM0|CKEN_PWM1, 1);
   1052 	pxa2x0_clkman_config(CKEN_KEY, 0);
   1053 
   1054 #if 1
   1055 	/* Clear all GPIO interrupt sources. */
   1056 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GEDR0, 0xffffffff);
   1057 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GEDR1, 0xffffffff);
   1058 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GEDR2, 0xffffffff);
   1059 #endif
   1060 
   1061 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR0, sd.sd_gpdr0);
   1062 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR1, sd.sd_gpdr1);
   1063 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR2, sd.sd_gpdr2);
   1064 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GRER0, sd.sd_grer0);
   1065 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GRER1, sd.sd_grer1);
   1066 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GRER2, sd.sd_grer2);
   1067 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GFER0, sd.sd_gfer0);
   1068 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GFER1, sd.sd_gfer1);
   1069 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GFER2, sd.sd_gfer2);
   1070 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR0_L, sd.sd_gafr0_l);
   1071 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR1_L, sd.sd_gafr1_l);
   1072 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR2_L, sd.sd_gafr2_l);
   1073 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR0_U, sd.sd_gafr0_u);
   1074 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR1_U, sd.sd_gafr1_u);
   1075 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR2_U, sd.sd_gafr2_u);
   1076 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPSR0, sd.sd_gplr0 &
   1077 	    sd.sd_gpdr0);
   1078 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPSR1, sd.sd_gplr1 &
   1079 	    sd.sd_gpdr1);
   1080 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPSR2, sd.sd_gplr2 &
   1081 	    sd.sd_gpdr2);
   1082 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPCR0, ~sd.sd_gplr0 &
   1083 	    sd.sd_gpdr0);
   1084 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPCR1, ~sd.sd_gplr1 &
   1085 	    sd.sd_gpdr1);
   1086 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPCR2, ~sd.sd_gplr2 &
   1087 	    sd.sd_gpdr2);
   1088 
   1089 	/* PXA27x */
   1090 #if 0
   1091 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GEDR3, 0xffffffff);
   1092 #endif
   1093 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPDR3, sd.sd_gpdr3);
   1094 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GRER3, sd.sd_grer3);
   1095 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GFER3, sd.sd_gfer3);
   1096 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR3_L, sd.sd_gafr3_l);
   1097 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GAFR3_U, sd.sd_gafr3_u);
   1098 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPSR3, sd.sd_gplr3 &
   1099 	    sd.sd_gpdr3);
   1100 	bus_space_write_4(sc->sc_iot, pxa2x0_gpio_ioh, GPIO_GPCR3, ~sd.sd_gplr3 &
   1101 	    sd.sd_gpdr3);
   1102 
   1103 	bus_space_write_4(sc->sc_iot, pxa2x0_memctl_ioh, MEMCTL_MECR,
   1104 	    sd.sd_mecr);
   1105 	bus_space_write_4(sc->sc_iot, pxa2x0_memctl_ioh, MEMCTL_MCMEM(0),
   1106 	    sd.sd_mcmem0);
   1107 	bus_space_write_4(sc->sc_iot, pxa2x0_memctl_ioh, MEMCTL_MCMEM(1),
   1108 	    sd.sd_mcmem1);
   1109 	bus_space_write_4(sc->sc_iot, pxa2x0_memctl_ioh, MEMCTL_MCATT(0),
   1110 	    sd.sd_mcatt0);
   1111 	bus_space_write_4(sc->sc_iot, pxa2x0_memctl_ioh, MEMCTL_MCATT(1),
   1112 	    sd.sd_mcatt1);
   1113 	bus_space_write_4(sc->sc_iot, pxa2x0_memctl_ioh, MEMCTL_MCIO(0),
   1114 	    sd.sd_mcio0);
   1115 	bus_space_write_4(sc->sc_iot, pxa2x0_memctl_ioh, MEMCTL_MCIO(1),
   1116 	    sd.sd_mcio1);
   1117 
   1118 	bus_space_write_4(sc->sc_iot, pxa2x0_clkman_ioh, CLKMAN_CKEN,
   1119 	    sd.sd_cken);
   1120 
   1121 	write_icu(INTCTL_ICLR, sd.sd_iclr);
   1122 	write_icu(INTCTL_ICCR, sd.sd_iccr);
   1123 	write_icu(INTCTL_ICMR, sd.sd_icmr);
   1124 
   1125 	if ((read_icu(INTCTL_ICIP) & 0x1) != 0)
   1126 		bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PEDR, 0x1);
   1127 
   1128 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OSMR0, sd.sd_osmr0);
   1129 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OSMR1, sd.sd_osmr1);
   1130 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OSMR2, sd.sd_osmr2);
   1131 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OSMR3, sd.sd_osmr3);
   1132 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OSMR4, sd.sd_osmr4);
   1133 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OSMR5, sd.sd_osmr5);
   1134 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OMCR4, sd.sd_omcr4);
   1135 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OMCR5, sd.sd_omcr5);
   1136 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OSCR0, sd.sd_oscr0);
   1137 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OSCR4, sd.sd_oscr4);
   1138 	bus_space_write_4(sc->sc_iot, ost_ioh, OST_OIER, sd.sd_oier);
   1139 
   1140 	pxa2x0_pi2c_setvoltage(sc->sc_iot, sc->sc_pm_ioh, PI2C_VOLTAGE_HIGH);
   1141 
   1142 	/* Change to 208MHz run mode with fast-bus still disabled. */
   1143 	pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X2 | CCCR_RUN_X16,
   1144 	    CLKCFG_F, &pxa2x0_memcfg);
   1145 	delay(1); /* XXX is the delay long enough, and necessary at all? */
   1146 	pxa27x_fastbus_run_mode(1, pxa2x0_memcfg.mdrefr_high);
   1147 
   1148 	/* Change to 416MHz turbo mode with fast-bus enabled. */
   1149 	pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X2 | CCCR_RUN_X16,
   1150 	    CLKCFG_B | CLKCFG_F | CLKCFG_T, &pxa2x0_memcfg);
   1151 
   1152 	if (sc->sc_resume != NULL) {
   1153 		if (!sc->sc_resume(sc))
   1154 			goto suspend_again;
   1155 	}
   1156 
   1157 	/*
   1158 	 * Allow immediate entry into deep-sleep mode if power fails.
   1159 	 * Resume from immediate deep-sleep is not implemented yet.
   1160 	 */
   1161 	bus_space_write_4(sc->sc_iot, sc->sc_pm_ioh, POWMAN_PMCR, 0);
   1162 
   1163 
   1164 	restore_interrupts(save);
   1165 
   1166 #if 0
   1167 	pxa2x0_setperf(perflevel);
   1168 #endif
   1169 
   1170 out:
   1171 	if (ost_ioh != (bus_space_handle_t)0)
   1172 		bus_space_unmap(sc->sc_iot, ost_ioh, PXA2X0_OST_SIZE);
   1173 }
   1174 
   1175 void
   1176 pxa2x0_pi2c_open(bus_space_tag_t iot, bus_space_handle_t ioh)
   1177 {
   1178 	uint32_t rv;
   1179 
   1180 	/* Enable the I2C unit, and disable automatic voltage change. */
   1181 	rv = bus_space_read_4(iot, ioh, POWMAN_PCFR);
   1182 	bus_space_write_4(iot, ioh, POWMAN_PCFR, rv | PCFR_PI2C_EN);
   1183 	rv = bus_space_read_4(iot, ioh, POWMAN_PCFR);
   1184 	bus_space_write_4(iot, ioh, POWMAN_PCFR, rv & ~PCFR_FVC);
   1185 	delay(1);
   1186 
   1187 	/* Enable the clock to the power manager I2C unit. */
   1188 	pxa2x0_clkman_config(CKEN_PI2C, 1);
   1189 	delay(1);
   1190 }
   1191 
   1192 void
   1193 pxa2x0_pi2c_close(bus_space_tag_t iot, bus_space_handle_t ioh)
   1194 {
   1195 	uint32_t rv;
   1196 
   1197 	bus_space_write_4(iot, ioh, POWMAN_PICR, PICR_UR);
   1198 	bus_space_write_4(iot, ioh, POWMAN_PISAR, 0);
   1199 	delay(1);
   1200 
   1201 	/* Disable the clock to the power manager I2C unit. */
   1202 	pxa2x0_clkman_config(CKEN_PI2C, 0);
   1203 	delay(1);
   1204 
   1205 	/* Disable the I2C unit, and disable automatic voltage change. */
   1206 	rv = bus_space_read_4(iot, ioh, POWMAN_PCFR);
   1207 	bus_space_write_4(iot, ioh, POWMAN_PCFR,
   1208 	    rv & ~(PCFR_PI2C_EN | PCFR_FVC));
   1209 	delay(1);
   1210 }
   1211 
   1212 int
   1213 pxa2x0_pi2c_read(bus_space_tag_t iot, bus_space_handle_t ioh,
   1214     u_char slave, u_char *valuep)
   1215 {
   1216 	uint32_t rv;
   1217 	int timeout;
   1218 	int tries = PI2C_RETRY_COUNT;
   1219 
   1220 retry:
   1221 
   1222 	bus_space_write_4(iot, ioh, POWMAN_PICR, PICR_UR);
   1223 	bus_space_write_4(iot, ioh, POWMAN_PISAR, 0x00);
   1224 	delay(1);
   1225 	bus_space_write_4(iot, ioh, POWMAN_PICR, PICR_IUE | PICR_SCLE);
   1226 
   1227 	/* Write slave device address. */
   1228 	bus_space_write_4(iot, ioh, POWMAN_PIDBR, (slave<<1) | 0x1);
   1229 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1230 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv | PICR_START);
   1231 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1232 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv & ~PICR_STOP);
   1233 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1234 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv | PICR_TB);
   1235 
   1236 	timeout = 10000;
   1237 	while ((bus_space_read_4(iot, ioh, POWMAN_PISR) & PISR_ITE) == 0) {
   1238 		if (timeout-- == 0) {
   1239 			bus_space_write_4(iot, ioh, POWMAN_PISR, PISR_ITE);
   1240 			goto err;
   1241 		}
   1242 		delay(1);
   1243 	}
   1244 
   1245 	bus_space_write_4(iot, ioh, POWMAN_PISR, PISR_ITE);
   1246 
   1247 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1248 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv & ~PICR_START);
   1249 
   1250 	/* Read data value. */
   1251 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1252 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv |
   1253 	    (PICR_STOP | PICR_ACKNAK));
   1254 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1255 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv | PICR_TB);
   1256 
   1257 	timeout = 10000;
   1258 	while ((bus_space_read_4(iot, ioh, POWMAN_PISR) & PISR_IRF) == 0) {
   1259 		if (timeout-- == 0) {
   1260 			bus_space_write_4(iot, ioh, POWMAN_PISR, PISR_IRF);
   1261 			goto err;
   1262 		}
   1263 		delay(1);
   1264 	}
   1265 
   1266 	bus_space_write_4(iot, ioh, POWMAN_PISR, PISR_IRF);
   1267 	rv = bus_space_read_4(iot, ioh, POWMAN_PIDBR);
   1268 	*valuep = (u_char)rv;
   1269 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1270 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv &
   1271 	    ~(PICR_STOP | PICR_ACKNAK));
   1272 
   1273 	return (0);
   1274 err:
   1275 	if (tries-- >= 0)
   1276 		goto retry;
   1277 
   1278 	bus_space_write_4(iot, ioh, POWMAN_PICR, PICR_UR);
   1279 	bus_space_write_4(iot, ioh, POWMAN_PISAR, 0x00);
   1280 	bus_space_write_4(iot, ioh, POWMAN_PICR, PICR_IUE | PICR_SCLE);
   1281 
   1282 	return (-EIO);
   1283 }
   1284 
   1285 int
   1286 pxa2x0_pi2c_write(bus_space_tag_t iot, bus_space_handle_t ioh,
   1287     u_char slave, u_char value)
   1288 {
   1289 	uint32_t rv;
   1290 	int timeout;
   1291 	int tries = PI2C_RETRY_COUNT;
   1292 
   1293 retry:
   1294 
   1295 	bus_space_write_4(iot, ioh, POWMAN_PICR, PICR_UR);
   1296 	bus_space_write_4(iot, ioh, POWMAN_PISAR, 0x00);
   1297 	delay(1);
   1298 	bus_space_write_4(iot, ioh, POWMAN_PICR, PICR_IUE | PICR_SCLE);
   1299 
   1300 	/* Write slave device address. */
   1301 	bus_space_write_4(iot, ioh, POWMAN_PIDBR, (slave<<1));
   1302 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1303 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv | PICR_START);
   1304 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1305 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv & ~PICR_STOP);
   1306 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1307 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv | PICR_TB);
   1308 
   1309 	timeout = 10000;
   1310 	while ((bus_space_read_4(iot, ioh, POWMAN_PISR) & PISR_ITE) == 0) {
   1311 		if (timeout-- == 0) {
   1312 			bus_space_write_4(iot, ioh, POWMAN_PISR, PISR_ITE);
   1313 			goto err;
   1314 		}
   1315 		delay(1);
   1316 	}
   1317 	if ((bus_space_read_4(iot, ioh, POWMAN_PISR) & PISR_ACKNAK) != 0)
   1318 		goto err;
   1319 	bus_space_write_4(iot, ioh, POWMAN_PISR, PISR_ITE);
   1320 
   1321 	/* Write data. */
   1322 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1323 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv & ~PICR_START);
   1324 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1325 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv | PICR_STOP);
   1326 	bus_space_write_4(iot, ioh, POWMAN_PIDBR, value);
   1327 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1328 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv | PICR_TB);
   1329 
   1330 	timeout = 10000;
   1331 	while ((bus_space_read_4(iot, ioh, POWMAN_PISR) & PISR_ITE) == 0) {
   1332 		if (timeout-- == 0) {
   1333 #if 0
   1334 			bus_space_write_4(iot, ioh, POWMAN_PISR, PISR_ITE);
   1335 #endif
   1336 			goto err;
   1337 		}
   1338 		delay(1);
   1339 	}
   1340 	if ((bus_space_read_4(iot, ioh, POWMAN_PISR) & PISR_ACKNAK) != 0)
   1341 		goto err;
   1342 	bus_space_write_4(iot, ioh, POWMAN_PISR, PISR_ITE);
   1343 
   1344 	rv = bus_space_read_4(iot, ioh, POWMAN_PICR);
   1345 	bus_space_write_4(iot, ioh, POWMAN_PICR, rv & ~PICR_STOP);
   1346 
   1347 	return (0);
   1348 err:
   1349 	bus_space_write_4(iot, ioh, POWMAN_PISR, PISR_ITE);
   1350 	if (tries-- >= 0)
   1351 		goto retry;
   1352 
   1353 	bus_space_write_4(iot, ioh, POWMAN_PICR, PICR_UR);
   1354 	bus_space_write_4(iot, ioh, POWMAN_PISAR, 0x00);
   1355 	bus_space_write_4(iot, ioh, POWMAN_PICR, PICR_IUE | PICR_SCLE);
   1356 
   1357 	return (-EIO);
   1358 }
   1359 
   1360 int
   1361 pxa2x0_pi2c_getvoltage(bus_space_tag_t iot, bus_space_handle_t ioh,
   1362     u_char *valuep)
   1363 {
   1364 	int res;
   1365 
   1366 	pxa2x0_pi2c_open(iot, ioh);
   1367 	res = pxa2x0_pi2c_read(iot, ioh, 0x0c, valuep);
   1368 	pxa2x0_pi2c_close(iot, ioh);
   1369 	return (res);
   1370 }
   1371 
   1372 int
   1373 pxa2x0_pi2c_setvoltage(bus_space_tag_t iot, bus_space_handle_t ioh,
   1374     u_char value)
   1375 {
   1376 	int res;
   1377 
   1378 	pxa2x0_pi2c_open(iot, ioh);
   1379 	res = pxa2x0_pi2c_write(iot, ioh, 0x0c, value);
   1380 	pxa2x0_pi2c_close(iot, ioh);
   1381 	return (res);
   1382 }
   1383 
   1384 #if 0
   1385 void
   1386 pxa2x0_pi2c_print(struct pxa2x0_apm_softc *sc)
   1387 {
   1388 	u_char value = 0;
   1389 
   1390 	(void)pxa2x0_pi2c_getvoltage(sc->sc_iot, sc->sc_pm_ioh, &value);
   1391 	printf("xscale core voltage: %s\n", value == PI2C_VOLTAGE_HIGH ?
   1392 	    "high" : (value == PI2C_VOLTAGE_LOW ? "low" : "unknown"));
   1393 }
   1394 #endif
   1395 
   1396 struct {
   1397 	int maxspeed;
   1398 	int numspeeds;
   1399 	int hz [6];
   1400 	int rate [6]; /* could this be simplfied by not having 100% in table? */
   1401 }
   1402 	speedtables[] = {
   1403 		{ 91, 1, { 91 }, { 100 }},
   1404 		{ 208, 2, { 91, 208}, {50, 100}},
   1405 		{ 416, 3, { 91, 208, 416}, {25, 50, 100}},
   1406 		{ 520, 4, { 91, 208, 416, 520}, {18, 40 ,80, 100}},
   1407 		{ 624, 5, { 91, 208, 416, 520, 624}, {15, 34, 67, 82, 100}},
   1408 		{ 0 }
   1409 	};
   1410 int xscale_maxspeed = 416; /* XXX */
   1411 
   1412 int speed_to_freq(int speed);
   1413 
   1414 int
   1415 speed_to_freq(int speed)
   1416 {
   1417 	int i, j;
   1418 	int newspeed = 0;
   1419 	int numspeeds;
   1420 	for (i = 0; speedtables[i].maxspeed != 0; i++) {
   1421 		if (speedtables[i].maxspeed != xscale_maxspeed)
   1422 			continue;
   1423 
   1424 		if (speed <= speedtables[i].rate[0]) {
   1425 			return speedtables[i].hz[0];
   1426 
   1427 		}
   1428 		numspeeds = speedtables[i].numspeeds;
   1429 		if (speed == speedtables[i].rate[numspeeds-1]) {
   1430 			return speedtables[i].hz[numspeeds-1];
   1431 		}
   1432 		for (j = 1; j < numspeeds; j++) {
   1433 			if (speed < speedtables[i].rate[j]) {
   1434 				return speedtables[i].hz[j-1];
   1435 			}
   1436 		}
   1437 	}
   1438 	return newspeed;
   1439 }
   1440 
   1441 
   1442 void
   1443 pxa2x0_setperf(int speed)
   1444 {
   1445 	struct pxa2x0_apm_softc *sc;
   1446 	int s;
   1447 	int newfreq;
   1448 
   1449 	sc = device_private(zapm_cd.cd_devs[0]);
   1450 
   1451 	newfreq = speed_to_freq(speed);
   1452 
   1453 	if (newfreq == 0) {
   1454 		printf("bogus new frequency 0 for rate %d maxclock %d\n",
   1455 		    speed, xscale_maxspeed);
   1456 	}
   1457 
   1458 	DPRINTF(("setperf speed %d newfreq %d, maxfreq %d\n",
   1459 		speed, newfreq, xscale_maxspeed));
   1460 
   1461 	s = disable_interrupts(I32_bit|F32_bit);
   1462 
   1463 	if (newfreq == 91) {
   1464 		if (freq > 91) {
   1465 			pxa27x_run_mode();
   1466 			pxa27x_fastbus_run_mode(0, MDREFR_LOW);
   1467 			pxa27x_cpu_speed_91();
   1468 			pxa2x0_pi2c_setvoltage(sc->sc_iot, sc->sc_pm_ioh,
   1469 			    PI2C_VOLTAGE_LOW);
   1470 			freq = 91;
   1471 		}
   1472 	} else if (newfreq == 208) {
   1473 		if (freq < 208)
   1474 			pxa2x0_pi2c_setvoltage(sc->sc_iot, sc->sc_pm_ioh,
   1475 			    PI2C_VOLTAGE_HIGH);
   1476 		if (freq != 208) {
   1477 			pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X2 |
   1478 			    CCCR_RUN_X16, CLKCFG_F, &pxa2x0_memcfg);
   1479 			pxa27x_fastbus_run_mode(1, pxa2x0_memcfg.mdrefr_high);
   1480 			freq = 208;
   1481 		}
   1482 	} else if (newfreq == 416) {
   1483 		if (freq < 208) {
   1484 			pxa2x0_pi2c_setvoltage(sc->sc_iot, sc->sc_pm_ioh,
   1485 			    PI2C_VOLTAGE_HIGH);
   1486 			pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X2 |
   1487 			    CCCR_RUN_X16, CLKCFG_F, &pxa2x0_memcfg);
   1488 			pxa27x_fastbus_run_mode(1, pxa2x0_memcfg.mdrefr_high);
   1489 		}
   1490 		if (freq != 416) {
   1491 			pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X2 |
   1492 			    CCCR_RUN_X16, CLKCFG_B | CLKCFG_F | CLKCFG_T,
   1493 			    &pxa2x0_memcfg);
   1494 			freq = 416;
   1495 		}
   1496 	} else if (newfreq == 520) {
   1497 		if (freq < 208) {
   1498 			pxa2x0_pi2c_setvoltage(sc->sc_iot, sc->sc_pm_ioh,
   1499 			    PI2C_VOLTAGE_HIGH);
   1500 			pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X2 |
   1501 			    CCCR_RUN_X16, CLKCFG_F, &pxa2x0_memcfg);
   1502 			pxa27x_fastbus_run_mode(1, pxa2x0_memcfg.mdrefr_high);
   1503 		}
   1504 		if (freq != 520) {
   1505 			pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X25 |
   1506 			    CCCR_RUN_X16, CLKCFG_B | CLKCFG_F | CLKCFG_T,
   1507 			    &pxa2x0_memcfg);
   1508 			freq = 520;
   1509 		}
   1510 	} else if (newfreq == 624) {
   1511 		if (freq < 208) {
   1512 			pxa2x0_pi2c_setvoltage(sc->sc_iot, sc->sc_pm_ioh,
   1513 			    PI2C_VOLTAGE_HIGH);
   1514 			pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X2 |
   1515 			    CCCR_RUN_X16, CLKCFG_F, &pxa2x0_memcfg);
   1516 			pxa27x_fastbus_run_mode(1, pxa2x0_memcfg.mdrefr_high);
   1517 		}
   1518 		if (freq != 624) {
   1519 			pxa27x_frequency_change(CCCR_A | CCCR_TURBO_X3 |
   1520 			    CCCR_RUN_X16, CLKCFG_B | CLKCFG_F | CLKCFG_T,
   1521 			    &pxa2x0_memcfg);
   1522 			freq = 624;
   1523 		}
   1524 	}
   1525 
   1526 	restore_interrupts(s);
   1527 }
   1528 
   1529 int
   1530 pxa2x0_cpuspeed(int *freqp)
   1531 {
   1532 	*freqp = freq;
   1533 	return 0;
   1534 }
   1535 
   1536 void pxa2x0_maxspeed(int *speedp);
   1537 
   1538 void
   1539 pxa2x0_maxspeed(int *speedp)
   1540 {
   1541 	/* XXX assumes a pxa270 */
   1542 
   1543 	if (*speedp < 207) {
   1544 		*speedp = 91;
   1545 	} else if (*speedp < 415) {
   1546 		*speedp = 208;
   1547 	} else if (*speedp < 519) {
   1548 		*speedp = 416;
   1549 	} else if (*speedp < 624) {
   1550 		*speedp = 520;
   1551 #if 0
   1552 	} else if (*speedp < 651) {
   1553 		*speedp = 624;
   1554 #endif
   1555 	} else {
   1556 		*speedp = 520; /* hope this is safe. */
   1557 	}
   1558 	xscale_maxspeed = *speedp;
   1559 #if 0
   1560 	pxa2x0_setperf(perflevel);
   1561 #endif
   1562 }
   1563